【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: "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".

【解析】

第一次看到这个题目的人,可能不知道ZigZag是什么意思,简单解释一下,就是把字符串原顺序012345……按下图所示排列:

比较直观的解法是,用一个字符串数组 string[rows] 来存储每一行,最后一拼接就是最终结果。

用一个delta表示正向还是反向,即上图中从第一行到最后一行还是最后一行到第一行。

代码如下所示:

public class Solution {
    public String convert(String s, int nRows) {
        int len = s.length();
        if (len == 0 || nRows <= 1) return s;

        String[] ans = new String[nRows];
        Arrays.fill(ans, "");
        int row = 0, delta = 1;
        for (int i = 0; i < len; i++) {
            ans[row] += s.charAt(i);
            row += delta;
            if (row >= nRows) {
                row = nRows-2;
                delta = -1;
            }
            if (row < 0) {
                row = 1;
                delta = 1;
            }
        }

        String ret = "";
        for (int i = 0; i < nRows; i++) {
            ret += ans[i];
        }
        return ret;
    }
}

【网上解法】

如 http://blog.csdn.net/cshaxu/article/details/12507201 说的最为简洁:

发现所有行的重复周期都是 2 * nRows - 2

对于首行和末行之间的行,还会额外重复一次,重复的这一次距离本周期起始字符的距离是 2 * nRows - 2 - 2 * i

代码如下所示:

public class Solution {
    public String convert(String s, int nRows) {
        int len = s.length();
        if (len == 0 || nRows < 2) return s;

        String ret = "";
        int lag = 2*nRows - 2; //循环周期
        for (int i = 0; i < nRows; i++) {
            for (int j = i; j < len; j += lag) {
                ret += s.charAt(j);

                //非首行和末行时还要加一个
                if (i > 0 && i < nRows-1) {
                    int t = j + lag - 2*i;
                    if (t < len) {
                        ret += s.charAt(t);
                    }
                }
            }
        }
        return ret;
    }
}

【总结】

这道题属于简单题,找规律即可,循环周期可能比较容易找,周期中间的规律 2 * nRows - 2 - 2 * i 可能不大好找。

时间: 2024-08-26 12:31:06

【LeetCode】ZigZag Conversion 解题报告的相关文章

LeetCode ZigZag Conversion 解题报告

对输入字符串,做蛇形变化,然后按行输出. https://oj.leetcode.com/problems/zigzag-conversion/ 例如:The string "PAYPALISHIRING"  的蛇形变化如下: P        A           H        N A   P   L    S     I     I   G Y         I            R 最后要求输出的就是:"PAHNAPLSIIGYIR" Write

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The

[LeetCode]LRU Cache, 解题报告

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

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

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

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.ZigZag Conversion ,Container With Most Water

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 lin

LeetCode ZigZag Conversion C++ 解题思路

一个难度为Easy的题,看了好多人的题解都没想明白,最后使劲想使劲想,才想的差不多..太弱了,要找不到工作了.. 题目描述: 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

[LeetCode] ZigZag Conversion [9]

称号 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: "PAHNAPL