[leetcode]ZigZag Conversion

 1 class Solution {
 2 public:
 3     string convert(string s, int numRows) {
 4         if(numRows <= 1 || s.size() == 0)
 5             return s;
 6         int str = s.size();
 7         string sss="";
 8        
 9         for(int i = 0 ; i< str && i < numRows; i++)
10         {
11             int index = i;
12             sss += s[i];
13             for(int j = 1; index < str; j++)
14             {
15                 if(i == 0 || i == numRows -1)
16                 {
17                     index += 2*numRows - 2;
18                 }
19                 
20                 else
21                 {
22                     if(j % 2 == 1)
23                     {
24                         index += 2*(numRows -1 -i);
25                     }
26                     else
27                     {
28                         index += 2*i;
29                     }
30                 }
31                 
32                 if(index < str)
33                 {
34                     sss += s[index];
35                 }
36             }
37         }
38         return sss;
39     }
40 };
代码

 数学公式来做,提高效率。easy题说简单,其实也挺复杂,考虑的全面比较难。

 

posted on 2015-10-20 13:40  Mereyct  阅读(139)  评论(0编辑  收藏  举报

导航