代码改变世界

leetcode - ZigZag Conversion

2013-12-09 15:06  张汉生  阅读(153)  评论(0编辑  收藏  举报

 

 1 class Solution {
 2 public:
 3     string convert(string s, int nRows) {
 4     int len = s.length();
 5     if (nRows<=1 || len<=2)
 6         return s;
 7     string * strs = new string[nRows];
 8     for (int i=0; i<len; i++){
 9         int offset = i % (2*nRows-2);
10         if (offset<nRows)
11         strs[offset] += s.at(i);
12         else strs[nRows-1 - (offset-nRows)-1]+=s.at(i);
13     }
14     string rlt="";
15     for (int i=0; i<nRows; i++)
16         rlt += strs[i];
17     return rlt;
18     }
19 };