LeetCode #6 ZigZag Conversion (E)

[Problem]

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

[Analysis]

这题比较简单,大概有两类思路:一是找到规律按一定的间隔直接将字符存入buffer,二是分开每行分别建立字符串最后将所有字符串连接起来。前者规律要稍微复杂一点,但内存方面稍微优化。一开始我采用的是将例子写在纸上找规律的暴力方法,只要将规律列清楚,一样可以求解。

[Solution]

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

        StringBuffer buf = new StringBuffer("");

        for (int i = 0; i < nRows; i++) {
            int interval1 = (nRows - i - 1) * 2;
            int interval2 = i * 2;
            if (interval2 == 0 || interval1 == interval2) {
                for (int j = i; j < s.length(); j += interval1) {
                    buf.append(s.charAt(j));
                }
            } else if (interval1 == 0) {
                for (int j = i; j < s.length(); j += interval2) {
                    buf.append(s.charAt(j));
                }
            } else {
                buf.append(s.charAt(i));
                for (int j = i + interval1; j < s.length(); j += interval1) {
                    buf.append(s.charAt(j));
                    j += interval2;
                    if (j < s.length()) {
                        buf.append(s.charAt(j));
                    }
                }
            }
        }

        return buf.toString();
    }
}
时间: 2024-10-07 03:53:39

LeetCode #6 ZigZag Conversion (E)的相关文章

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+

[LeetCode][JavaScript]ZigZag Conversion

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

【LeetCode】ZigZag Conversion

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

leetCode 6. ZigZag Conversion 字符串

6. ZigZag Conversion

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

LeetCode 6 ZigZag Conversion 模拟 难度:0

https://leetcode.com/problems/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

LeetCode 6 ZigZag Conversion(规律)

题目来源:https://leetcode.com/problems/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

[LeetCode][Python]ZigZag Conversion

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/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

[LeetCode]7. ZigZag Conversion ZigZag转换

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 NA P L S I I GY I R And then read line by line: "PAHNAPLSIIGY