[leedcode 06]ZigZag Conversion

public class Solution {
    public String convert(String s, int numRows) {
        //本题通过画图numRows=4和numRows=5可以得到规则,主线路距离是2*numRows-2;辅助行是(numRows-i-1)*2
        if(numRows<=1)return s;//注意判断
        if(s.length()<=numRows) return s;
        StringBuilder res=new StringBuilder();
        int index=0;
        for(int i=0;i<numRows;i++){//i表示行数,注意范围
            for(index=i;index<s.length();index=index+numRows*2-2){
                res.append(s.charAt(index));
                if(i==0||i==numRows-1)continue;//注意首尾两行
                if(index+(numRows-i-1)*2<s.length()){
                res.append(s.charAt(index+(numRows-i-1)*2));
                }
            }

        }
        return res.toString();
    }
}
时间: 2024-10-31 01:59:44

[leedcode 06]ZigZag Conversion的相关文章

LeetCode 06 ZigZag Conversion

https://leetcode.com/problems/zigzag-conversion/ 水题纯考细心 题目:依照Z字形来把一个字符串写成矩阵,然后逐行输出矩阵. O(n)能够处理掉 记i为行数 第0行和第numRow-1行. ans += str[i+k*(numRows*2-2)], k=0,1,2,... 其它, 每一个Z字形(事实上仅仅是一竖一斜两条线)须要加上两个,下标见代码. 特殊处理:numRows=1的时候numRows*2-2==0 ,会死循环的.另外numRows=1

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

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(); // s

leetCode 6. ZigZag Conversion 字符串

6. ZigZag Conversion