重囗另类BBWSeⅹHD,av狼论坛,精品一卡2卡三卡4卡乱码理论,体育生gv老师浪小辉3p警察

LeetCode 6 ZigZag Conversion(Z型轉(zhuǎn)換) -電腦資料

電腦資料 時(shí)間:2019-01-01 我要投稿
【www.ipr-jzsc.com - 電腦資料】

    翻譯

<code class="hljs" mathematica="">字符串“PAYPALISHIRING”通過(guò)一個(gè)給定的行數(shù)寫(xiě)成如下這種Z型模式:P   A   H   NA P L S I I GY   I   R然后一行一行的讀取:“PAHNAPLSIIGYIR”寫(xiě)代碼讀入一個(gè)字符串并通過(guò)給定的行數(shù)做這個(gè)轉(zhuǎn)換:string convert(string text, int nRows);調(diào)用convert(PAYPALISHIRING, 3),應(yīng)該返回PAHNAPLSIIGYIR,

LeetCode 6 ZigZag Conversion(Z型轉(zhuǎn)換)

。</code>

    原文

<code applescript="" class="hljs">The string PAYPALISHIRING is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P   A   H   NA P L S I I GY   I   RAnd then read line by line: PAHNAPLSIIGYIRWrite the code that will take a string and make this conversion given a number of rows:string convert(string text, int nRows);convert(PAYPALISHIRING, 3) should return PAHNAPLSIIGYIR.</code>

    如果還是沒(méi)明白題目的意思,看下圖吧……

   

<code class="hljs" cs="">public class Solution{    public string Convert(string s, int numRows)    {        if (numRows == 1)            return s;        StringBuilder strBuilder = new StringBuilder();        int lengthOfGroup = 2 * numRows - 2;        // 如上圖所示,每組的長(zhǎng)度為4             for (int row = 0; row < numRows; row++)      // 按從第0行到numRows-1行的順序遍歷          {            if (row == 0 || row == numRows - 1)        // 此處負(fù)責(zé)第0行和numRows-1行            {                for (int j = row; j < s.Length; j += lengthOfGroup)                {                    strBuilder.Append(s[j]);                }            }            else                   // 此處負(fù)責(zé)第0行和numRows-1行之間的所有行            {                int currentRow = row;           // 在當(dāng)前行中向右移動(dòng)(看上圖)                bool flag = true;                int childLenOfGroup1 = 2 * (numRows - 1 - row);                //  怎么說(shuō)呢……中間行的各個(gè)索引吧                int childLenOfGroup2 = lengthOfGroup - childLenOfGroup1;                while (currentRow < s.Length)                {                    strBuilder.Append(s[currentRow]);                    if (flag)                        currentRow += childLenOfGroup1;                    else                        currentRow += childLenOfGroup2;                    flag = !flag;                }            }        }        return strBuilder.ToString();    }}</code>

    C++的代碼肯定是有的:

<code class="hljs" cs="">class Solution {public:    string convert(string s, int numRows) {              if(numRows==1)            return s;        string str=;        int lengthOfGroup=2*numRows-2;        for(int row=0;row<numrows;row++){ bool="" childlenofgroup1="2*(numRows-1-row);" childlenofgroup2="lengthOfGroup-childLenOfGroup1;" code="" currentrow="row;currentRow<s.length();currentRow+=lengthOfGroup){" else="" flag="true;" int="" return="" row="=0||row==numRows-1){"></numrows;row++){></code>

    至于Java嘛,當(dāng)然也有……不過(guò)每次我都是直接把C#的代碼拷貝過(guò)去然后改改就好了,

電腦資料

LeetCode 6 ZigZag Conversion(Z型轉(zhuǎn)換)》(http://www.ipr-jzsc.com)。

<code class="hljs" cs=""><code class="hljs" cs="">public class Solution {    public String convert(String s, int numRows) {          if (numRows == 1)              return s;          StringBuilder strBuilder = new StringBuilder();          int lengthOfGroup = 2 * numRows - 2;                 for(int row=0; row < numRows; row++){              if (row == 0 || row == numRows - 1){                  for(int currentRow = row; currentRow < s.length(); currentRow += lengthOfGroup){                      strBuilder.append(s.charAt(currentRow));                  }              }              else{                  int currentRow = row;                          boolean flag = true;                  int childLenOfGroup1 = 2 * (numRows - 1 - row);                             int childLenOfGroup2 = lengthOfGroup - childLenOfGroup1;                  while (currentRow<s.length()){ code="" currentrow="" else="" flag="!flag;" if="" return=""></s.length()){></code></code>

最新文章
主站蜘蛛池模板: 稷山县| 晋州市| 玉田县| 兴文县| 青龙| 谷城县| 彰化市| 闻喜县| 穆棱市| 平果县| 曲靖市| 来宾市| 巧家县| 南宁市| 荔浦县| 蓝山县| 湖口县| 丰县| 伊金霍洛旗| 宁德市| 亳州市| 依安县| 和平区| 英吉沙县| 土默特左旗| 长治市| 麻阳| 防城港市| 天等县| 乌兰浩特市| 奎屯市| 营山县| 永修县| 乐至县| 上杭县| 通海县| 邵武市| 新宾| 上栗县| 莫力| 桐乡市|