LeetCode 6 ZigZag Conversion(Z型转换)

简介:

翻译

字符串“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"
AI 代码解读

原文

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".
AI 代码解读

如果还是没明白题目的意思,看下图吧……

这里写图片描述

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();
    }
}
AI 代码解读

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;   
    }
};
AI 代码解读

至于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 <s.length()){
                      strBuilder.append(s.charAt(currentRow));
                      if (flag)
                          currentRow += childLenOfGroup1;
                      else
                          currentRow += childLenOfGroup2;
                      flag = !flag;
                  }
              }
          }
          return strBuilder.toString();
      }
}
AI 代码解读
目录
打赏
0
2
2
0
51
分享
相关文章
Leetcode 6.ZigZag Conversion
如上所示,这就是26个小写字母表的5行曲折变换。 其中在做这道题的时候把不需要我们构造出这样五行字符,然后拼接。其实你把字母换成1-n的数字,很容易找到每个位置的字母在原字符串中的位置。
70 0
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 1317. 将整数转换为两个无零整数的和
LeetCode 1317. 将整数转换为两个无零整数的和
LeetCode 103. BTree Zigzag Level Order Traversal
给定二叉树,返回其节点值的Z字形级别遍历。 (即,从左到右,然后从右到左进行下一级别并在之间交替)。
100 0
LeetCode 103. BTree Zigzag Level Order Traversal
LeetCode每日一题——1331. 数组序号转换
给你一个整数数组 arr ,请你将数组中的每个元素替换为它们排序后的序号。
111 0
LeetCode(数据库)- 转换日期格式
LeetCode(数据库)- 转换日期格式
144 0
Leetcode-Medium 6. ZigZag Conversion
Leetcode-Medium 6. ZigZag Conversion
104 0
Leetcode-Medium 6. ZigZag Conversion
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等