首页 > 代码库 > leetcode 6

leetcode 6

感觉leetcode上面的题用来联系c++语法确实不错

题解:string的运用

 1 class Solution { 2 public: 3     string convert(string s, int numRows) { 4         if(s.size()==0)return ""; 5         if(numRows==1)return s; 6          int i,k; 7      string* Thestr; 8       Thestr=new string[numRows]; 9      i=0;k=0;10      Thestr[i].push_back(s[k]);11      while(k+1<s.size())12      {13          if(k%(numRows*2-2)<numRows-1)14          {15             i++;16          }17          else i--;18          k++;19          Thestr[i].push_back(s[k]);20      }21      string answer="";22      for(i=0;i<numRows;i++)23          answer+=Thestr[i];24      delete []Thestr;25      return answer;26     }27 };

 

leetcode 6