LeetCode(6) - ZigZag Conversion

这个题的要求是给你一个字符串,和一个行数,例如(s = "mysisteristhemostlovelygirl" , row = 4),每一行一个字符串,但是s却得按照zigzag的方式重排序到这4行的字符串里,什么意思呢? 看例子大概就懂了:

      m    e     e      o      i

      y  t  r  h  m  l  v  g  r

      s  s  i  t  o   t  e  y  l

      i      s     s      l

  第一列开始往下走,走到第四行就往上走(第二列),走回第一行就重新往下走,如此类推,最后的输出就是每一行的叠加,也就是:

      "meeoiytrhmlvgrssitoteylissl"

  思路其实很简单,就是用一个index来表示走到哪一行,用一个flag来表示往下还是往上走,当index>4和index < 0的时候,改变flag就可以了。代码如下:

  

public class Solution {
    public String convert(String s, int numRows) {
        if (numRows == 1) return s;
        String[] stringArray = new String[numRows];
        //初始化字符串。
        for (int i = 0; i < numRows; i++) {
            stringArray[i] = "";
        }
        //index指向哪一行需要添加字母。
        int index = 0;
        //flag==1代表往下走(初始),flag==0代表往下走。
        int flag = 1;
        for (int i = 0; i < s.length(); i++) {
            if (flag == 1) {
                stringArray[index++] += s.charAt(i);
                //当index越界时
                if (index == numRows) {
                    //-2是因为边界在index-1的时候已经写过一遍了
                    index = index - 2;
                    flag = 0;
                }
            }
            else {
                stringArray[index--] += s.charAt(i);
                //与上面同理
                if (index < 0) {
                    index = index + 2;
                    flag = 1;
                }
            }
        }
        StringBuilder sb = new StringBuilder(stringArray[0]);
        //把每一行加起来,选择用StringBuilder而不用String是因为StringBuilder要快一些
        //为什么?读者可以查阅相关资料就能够了解string往后面添加字符串是一个什么样的机制。
        for (int i = 1; i < numRows; i++) {
            sb.append(stringArray[i]);
        }
        return sb.toString();

    }
}
时间: 2025-01-17 18:34:18

LeetCode(6) - ZigZag Conversion的相关文章

【LeetCode】006 ZigZag Conversion

题目:LeetCode 006 ZigZag Conversion 题意:给一个字符串"PAYPALISHIRING" 按照ZigZag的形式,即按照下面的顺序排列,然后 在转换成一行一行的读取方式,得到"PAHNAPLSIIGYIR".其中行数不定. 思路:肯定是不能去开一个二维数组的,必须要找出规律来直接输出.发现间隔是和总行数numRows有关,另外,第一行和最后一行需要单独考虑,每两个字符间隔一致,为2*(numRows-1):其余行有两种间隔,设当前为第L

leetcode题解 6.ZigZag Conversion

6.ZigZag Conversion 题目: 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 b

【LeetCode】6 - ZigZag Conversion

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: "PAHNAPLSII

leetcode笔记:ZigZag Conversion

一. 题目描述 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: "PA

leetcode problem 6 ZigZag Conversion

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: "PAHNAPLSII

LeetCode ZigZag Conversion(将字符串排成z字型)

1 class Solution { 2 public: 3 string convert(string s, int nRows) { 4 string a=""; 5 int len=s.length(); 6 if(len<=nRows||nRows==1) return s; //只有n个,不足一个周期||排成一行 7 int teams=len/(nRows*2-2); //teams个完整的周期 8 int rest=len%(nRows*2-2); //最后一个不足

leetcode -day16 ZigZag Conversion

1.  ZigZag Conversion 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 b

leetCode 6. ZigZag Conversion 字符串 (上传费劲)

6. ZigZag Conversion 题目:https://leetcode.com/problems/zigzag-conversion/ string convert2(string s, int numRows) { if (s.length() < 2 || numRows < 2) return s; int cycle = 2 * numRows - 2; string tmp; string result; for (int i = 0; i < numRows; i+

ZigZag Conversion leetcode java

题目: 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: "PAHNAP