首页 > 其他 > 详细

Leetcode006 ZigZag Conversion

时间:2016-09-07 22:28:53      阅读:130      评论:0      收藏:0      [点我收藏+]
 1 /* simple simulation algorithm
 2  * we cann`t make sure the size of the string, 
 3  * so it had better to storage in vector.
 4  * show[] to buffer the new order of the string 
 5  */
 6 class Solution {
 7 public:
 8     string convert(string s, int numRows) {
 9      string result;
10      vector<char> show[numRows];
11      for(int index=0;index<s.size();)   //divide into two parts
12      {
13          for(int i=0;i<numRows;i++)     //simple row full storage
14          {
15              show[i].push_back(s[index++]);
16              if(index==s.size())break;
17          }
18          for(int i=1;i<=numRows-2;i++)  //middle row only only one char storaged
19          {
20              for(int j=numRows-1;j>=0;j--)
21              {
22                  if(i+j==numRows-1)show[j].push_back(s[index++]);
23                  if(index==s.size())break;
24              }
25              if(index==s.size())break;
26          }
27      }
28      for(int i=0;i<numRows;i++)
29      {
30          for(int j=0;j<show[i].size();j++)result+=show[i][j];
31      }
32      return result;
33     }
34 };

 

Leetcode006 ZigZag Conversion

原文:http://www.cnblogs.com/zeroArn/p/5851009.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!