Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594

[LeetCode]ZigZag Conversion-2014.01.08

Posted on 2014-01-11 02:17 Uriel 阅读(85) 评论(0)  编辑 收藏 引用 所属分类: LeetCode
水题,但理解题意略纠结,其实就是把| / | / | / |这样排列的字符阵列转化为按行来的,直接模拟就好~

 1 class Solution {
 2 public:
 3     string convert(string s, int nRows) {
 4         if(nRows == 1) return s;
 5         string res;
 6         int i = 0, j = 0, n = s.length();
 7         while(j < s.length()) {
 8             res.push_back(s[j]);
 9             j += 2 * nRows - 2;
10         }
11         for(i = 1; i < nRows - 1; ++i) {
12             j = i;
13             while(j < s.length()) {
14                 res.push_back(s[j]);
15                 j += 2 * nRows - 2 * (i + 1);
16                 if(j < s.length()) {
17                     res.push_back(s[j]);
18                     j += 2 * i;
19                 }
20             }
21         }
22         j = nRows - 1;
23         while(j < s.length()) {
24             res.push_back(s[j]);
25             j += 2 * nRows - 2;
26         }
27         return res;
28     }
29 };

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理