程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> LeetCode 6 ZigZag Conversion(Z型轉換)

LeetCode 6 ZigZag Conversion(Z型轉換)

編輯:關於C++

翻譯

字符串“PAYPALISHIRING”通過一個給定的行數寫成如下這種Z型模式:
P   A   H   N
A P L S I I G
Y   I   R

然後一行一行的讀取:“PAHNAPLSIIGYIR”

寫代碼讀入一個字符串並通過給定的行數做這個轉換:

string convert(string text, int nRows);

調用convert(PAYPALISHIRING, 3),應該返回PAHNAPLSIIGYIR。

原文

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   N
A P L S I I G
Y   I   R

And then read line by line: PAHNAPLSIIGYIR
Write 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.

如果還是沒明白題目的意思,看下圖吧……

這裡寫圖片描述

public class Solution
{
    public string Convert(string s, int numRows)
    {
        if (numRows == 1)
            return s;
        StringBuilder strBuilder = new StringBuilder();
        int lengthOfGroup = 2 * numRows - 2;        // 如上圖所示,每組的長度為4     
        for (int row = 0; row < numRows; row++)      // 按從第0行到numRows-1行的順序遍歷  
        {
            if (row == 0 || row == numRows - 1)        // 此處負責第0行和numRows-1行
            {
                for (int j = row; j < s.Length; j += lengthOfGroup)
                {
                    strBuilder.Append(s[j]);
                }
            }
            else                   // 此處負責第0行和numRows-1行之間的所有行
            {
                int currentRow = row;           // 在當前行中向右移動(看上圖)
                bool flag = true;
                int childLenOfGroup1 = 2 * (numRows - 1 - row);                //  怎麼說呢……中間行的各個索引吧
                int childLenOfGroup2 = lengthOfGroup - childLenOfGroup1;

                while (currentRow < s.Length)
                {
                    strBuilder.Append(s[currentRow]);
                    if (flag)
                        currentRow += childLenOfGroup1;
                    else
                        currentRow += childLenOfGroup2;
                    flag = !flag;
                }
            }
        }
        return strBuilder.ToString();
    }
}

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

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

至於Java嘛,當然也有……不過每次我都是直接把C#的代碼拷貝過去然後改改就好了。

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 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved