Leetcode_實現zigzag的轉換_20161228

來源:互聯網
上載者:User

標籤:app   resize   nbsp   bsp   har   turn   轉換   names   實現   

#include<iostream>

#include<vector>

#include<string>

void main()

{  

using namespace std;  

class Solution

{  public:   string convert(string s, int numRows)  

 {   

 int h2 = s.length();    

int lie1 = h2/ numRows;  //得到排列的列數   

 if (h2%numRows != 0)    

 lie1++;    

vector<vector<char>>ret(numRows);  //定義二維向量   

  for (int i = 0; i < numRows;i++)   

  ret[i].resize(lie1*2); //得到 numRows*lie1的矩陣   

 int i = 0;   

 int j = 0;  

  int i0 = 0;   

 for (int count = 0; (count < (numRows*lie1 * 2))&&(i0<h2); count++)    

{    

 if (i == 0)    

 {      //如果行數等於列數就往下加     

 for (int ii = 0; (ii < numRows) && (i0<h2); ii++)     

 {      

 ret[ii][j] = s[i0];

      i0++;     

 }     

 i = numRows - 1;      

j++;

    }  

   if (i == (numRows - 1))    

 {     

 for (int ii = numRows - 2; (ii >= 1) && (i0<h2); ii--)     

 {     

  ret[ii][j] = s[i0];   

    j++;     

  i0++;    

  }     

 i = 0;    

 }   

 }  //將字串賦值給向量ret     

string s1;    

 char *c;   

 for (int i1 = 0; i1 < numRows; i1++)   

 {    

 for (int j1 = 0; j1 < (lie1 * 2); j1++)    

 {     

 if (ret[i1][j1]!=‘\0‘)    

  {     

  c = &(ret[i1][j1]);     

  s1.append(c,1); //每次都將值賦給字串,這裡必須加上1,不然會加其他的東西在s1裡面

  }     // cout << ret[i1][j1];   

  }         

}   

 return s1;   

}

 };  

Solution A;

 string a1 = "ABC";

 cout << a1 << endl;

 string b;

 b = A.convert(a1, 2);  

int h1 = b.length();

 cout << b<<endl;

 system("pause");

}

Leetcode_實現zigzag的轉換_20161228

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.