鍍金池/ 教程/ Java/ Reverse Integer(翻轉(zhuǎn)整數(shù))
ZigZag Conversion(Z型轉(zhuǎn)換)
String to Integer (atoi)(轉(zhuǎn)換到整型)
Generate Parentheses(生成括號)
Longest Common Prefix(最長公共前綴)
Remove Duplicates from Sorted Array(從已排序數(shù)組中移除重復(fù)元素)
Swap Nodes in Pairs(交換序列中的結(jié)點)
Valid Parentheses(有效的括號)
4 Sum(4 個數(shù)的和)
3 Sum(3 個數(shù)的和)
3 Sum Closest(最接近的 3 個數(shù)的和)
Reverse Nodes in k-Group(在K組鏈表中反轉(zhuǎn)結(jié)點)
Regular Expression Matching (正則表達(dá)式匹配)
Two Sum
Container With Most Water(最大水容器)
Merge Two Sorted Lists(合并兩個已排序的數(shù)組)
Remove Nth Node From End of List(從列表尾部刪除第 N 個結(jié)點)
String to Integer (atoi)(轉(zhuǎn)換到整型)
Palindrome Number(回文數(shù))
Longest Substring Without Repeating Characters
Roman to Integer(羅馬數(shù)到整型數(shù))
Integer to Roman(整型數(shù)到羅馬數(shù))
Reverse Integer(翻轉(zhuǎn)整數(shù))
Merge k Sorted Lists(合并 K 個已排序鏈表)
Implement strStr()(實現(xiàn) strStr() 函數(shù))
Longest Palindromic Substring(最大回文子字符串)
Add Two Numbers
Letter Combinations of a Phone Number(電話號碼的字母組合)

Reverse Integer(翻轉(zhuǎn)整數(shù))

翻譯

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

然后一行一行的讀?。骸癙AHNAPLSIIGYIR”

寫代碼讀入一個字符串并通過給定的行數(shù)做這個轉(zhuǎn)換:

string convert(string text, int nRows);

調(diào)用 convert("PAYPALISHIRING", 3),應(yīng)該返回"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".

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

http://wiki.jikexueyuan.com/project/leetcode-book/images/1.png" alt="" />

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)        // 此處負(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)前行中向右移動(看上圖)
                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<numRows;row++){
            if(row==0||row==numRows-1){
                for(int currentRow=row;currentRow<s.length();currentRow+=lengthOfGroup){
                    str+=s[currentRow];
                }
            }
            else{
                int currentRow=row;
                bool flag=true;
                int childLenOfGroup1=2*(numRows-1-row);
                int childLenOfGroup2=lengthOfGroup-childLenOfGroup1;
                while(currentRow<s.length()){
                    str+=s[currentRow];
                    if(flag)
                        currentRow+=childLenOfGroup1;
                    else
                        currentRow+=childLenOfGroup2;
                    flag=!flag;
                }
            }
        }
        return str;   
    }
};

至于 Java 嘛,當(dāng)然也有……不過每次我都是直接把 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 <s.length()){
                      strBuilder.append(s.charAt(currentRow));
                      if (flag)
                          currentRow += childLenOfGroup1;
                      else
                          currentRow += childLenOfGroup2;
                      flag = !flag;
                  }
              }
          }
          return strBuilder.toString();
      }
}