首页 > 代码库 > Leetcode006 ZigZag Conversion

Leetcode006 ZigZag Conversion

 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 parts12      {13          for(int i=0;i<numRows;i++)     //simple row full storage14          {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 storaged19          {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