`

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

// P   I   N
// A  LS  IG
// Y A H R
// P   I
public String convert(String s, int nRows) {
    if(nRows <=1) return s;
    StringBuilder sb = new StringBuilder();
    int len = s.length();
    int step = (nRows-1) * 2;
    for(int i=0; i<nRows; i++) {
        int j = i;
        while(j < len) {
            sb.append(s.charAt(j));
            if(!(i==0 || i==nRows-1)) {
                int k = j+ 2*(nRows-i-1);
                if(k<len)
                    sb.append(s.charAt(k));
            }
            j += step;
        }
    }
    return sb.toString();
}

 

重构了下代码:

// P      I      N
// A    L S    I G
// Y  A   H  R
// P      I
public String convert(String s, int numRows) {
    if(numRows <= 1) return s;
    String res = "";
    int n = numRows-1;
    for(int i=0; i<numRows; i++) {
        int j = i;
        while(j < s.length()) {
            res += s.charAt(j);
            j += (n - j%n)*2;
        }
    }
    return res;
}

 

分享到:
评论

相关推荐

    c语言-leetcode 0006-zigzag-conversion.zip

    c c语言_leetcode 0006_zigzag_conversion.zip

    js-leetcode题解之6-zigzag-conversion.js

    js js_leetcode题解之6-zigzag-conversion.js

    LeetCode6 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) Java AC版本

    C语言-leetcode题解之06-zigzag-conversion.c

    c语言入门 C语言_leetcode题解之06-zigzag-conversion.c

    java-leetcode题解之006-ZigZag-Conversion

    java入门 java_leetcode题解之006_ZigZag_Conversion

    leetcode2-zigzag_conversion:代码挑战:ZigZag转换

    leetcode 2 代码挑战 之字形转换 字符串"PAYPALISHIRING"在给定的行数上以锯齿形图案书写,如下所示:(您可能希望以固定字体显示此图案以获得更好的可读性) P A H N A P L S I I G Y I R 然后逐行阅读: ...

    leetcode题库-LeetCode-Go:用GO回答LeetCode

    leetcode题库 LeetCode-Go 理论基础 见Note 脑图 TODO ...Conversion 37.5% Medium 0007 Reverse Integer 25.8% Easy 0008 String to Integer (atoi) 15.5% Medium 0009 Palindrome Number 49.4% Easy

    leetcode316-leetcode_script:leetcode题解更新脚本

    Conversion Easy -&gt; Medium 8 String to Integer (atoi) Easy -&gt; Medium 19 Remove Nth Node From End of List Easy -&gt; Medium 33 Search in Rotated Sorted Array Hard -&gt; Medium 35 Search Insert Position Medium...

    leetcode分类-LeetCodeSrc:力扣

    O(n)时间复杂度就可以解决###LeetCode4AddTwoNumbers简单题,注意空指针的情况和进位###LeetCode5LongestPalindromicSubstringO(N^2)的时间复杂度,不知道有没有更快的###LeetCode6ZigZagConversion边界条件一定要想...

    leetcode分类-LeetCode:LeetCode在线裁判C++

    ZigZag Conversion SingleNumber 其他算法 各种SingleNumber变种 LinkListCycle I II 其他变种 编程之美 Preorder Traversal Inorder Traver sal postorder 非递归 不用栈! 字符串常用操作 字符串 各种转换 Pow(x,n...

    leetcode知乎-leetcode:leetcode解决方案

    leetcode 知乎 此项目介绍 此项目旨在是为leetcode里面的习题提供解析(数据结构相关知识),锻炼了自己,同时也给别人带来了方便。 网站习题链接: ** 作者知乎链接**: ...Conversion Reverse Integer Strin

    leetcode338-LeetCode:LeetCode刷题总结

    6.ZigZag Conversion 7.Reverse Integer 8.String to Integer (atoi) 9.Palindrome Number 10.Regular Expression Matching 11.Container With Most Water 12.Integer to Roman 13.Roman to Integer 14.Longest ...

    leetcode答案-leetcode:leetcode问题解决方案

    leetcode ...Conversion Medium com.bcat.algorithms.medium.ZigZagConversionSol 7 Reverse Integer Easy com.bcat.algorithms.easy.ReverseIntegerSol 8 String to Integer (atoi) Medium com.bc

    leetcode题库-LeetCode:LeetCode刷题

    6. **Z字形变换 (06 Zigzag Conversion)**:此题涉及到字符串处理和模拟。给定一个字符串,按照“Z”字形排列,即奇数位置的字符按顺序排列,偶数位置的字符逆序排列。这个问题可以通过迭代或递归方法实现。 这些...

    leetcode跳跃-leetcode:leetcode解题之路

    leetcode 跳跃 leetcode 按题型标签,记录leetcode 解题之路 Stack 栈 [0020 有效的括号](./Stack/valid-parentheses.md) ...Z字形变换](./String/zigzag-conversion.md) [0030 串联所有单词的子串](./String/substring

    javalruleetcode-leetcode-solutions-java:leetcode-解决方案-java

    Conversion.java) 7 [Java](/Java/007 反向整数.java) 8 [Java](/Java/008 String to Integer (atoi).java) 9 [Java](/Java/009 回文数.java) 10 [Java](/Java/010 正则表达式匹配.java) 11 [Java](/Java/011 最多水...

    leetcode答案-LeetCode:我的力扣解决方案

    Conversion: Find the Regulation. 11. Container With Most Water 问题简述:给出一个list a,找到一组i,j使得**min(a[i],a[j])*(j-i)**最大   思路:设置首位指针h,t 从较小的一段往中间移动,同时更新答案 12. ...

    leetcode跳跃-LeCode:乐科

    Conversion Z字型变换 7. Reverse Integer 整数反转 8. String to Integer (atoi) 字符串转换整数 (atoi) 9. Palindrome Number 回文数 10. Regular Expression Matching 正则表达式匹配 11. Container With Most ...

    leetcode530-algorithm:算法

    Conversion 007 Reverse Integer 008 String to Integer (atoi) 009 Palindrome Number 010 Regular Expression Matching 011 Container With Most Water 012 Integer to Roman 013 Roman to Integer 014 Longest ...

    _leetcode-python.pdf

    - ZigZag Conversion: 将字符串从左到右依次在Z字形排列填充到指定的行数。需要找出转换后的字符串。 - String to Integer (atoi): 将字符串转换为整数,需要处理各种边界情况,例如溢出、非法输入等。 - Reverse ...

Global site tag (gtag.js) - Google Analytics