6. ZigZag Conversion

    /*
     * 6. ZigZag Conversion
     * 2016-8-26 by Mingyang
     * 觉得这个题目面试的必要性不大,有规律,第一行和最后一行每两个相差2n-2
     * 中间的每行除了2n-2有外,中间的一个也有2 * (n - 1 - i)
     */
       public String convert(String s, int numRows) {
            if (numRows == 1)
                return s;
            StringBuilder sb = new StringBuilder();
            // step
            int step = 2 * numRows - 2;
            for (int i = 0; i < numRows; i++) {
                //first & last rows
                if (i == 0 || i == numRows - 1) {
                    for (int j = i; j < s.length(); j = j + step) {
                        sb.append(s.charAt(j));
                    }
                //middle rows
                } else {
                    int j = i;
                    boolean flag = true;
                    int step1 = 2 * (numRows - 1 - i);
                    int step2 = step - step1;

                    while (j < s.length()) {
                        sb.append(s.charAt(j));
                        if (flag)
                            j = j + step1;
                        else
                            j = j + step2;
                        flag = !flag;
                    }
                }
            }
            return sb.toString();
        }
时间: 2024-10-06 08:14:38

6. ZigZag Conversion的相关文章

No.006 ZigZag Conversion

6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: 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 bette

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】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 --思路图解与java实现

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

64. 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 字符串 (上传费劲)

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 --思路图解与java实现

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 b...http://yuedu.baidu.com/album/view/a9e914106c175f0e7cd