`

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

[分析] 找规律类型的题目,给出三种实现,实现1和2均是按找到的规律计算每一行各列元素在原串中的下标,实现3是按照原串的顺序构造出zigzag图,然后从上到下逐行遍历,时间和空间使用上均不如前两种好,但是更直观易懂。

[ref]
http://blog.unieagle.net/2012/11/08/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Azigzag-conversion/

    // method 3
    public String convert(String s, int numRows) {
        if (s == null || s.length() <= 1 || numRows == 1) 
            return s;
        // build zigzag map
        ArrayList<ArrayList<Character>> map = new ArrayList<ArrayList<Character>>(numRows);
        for (int i = 0; i < numRows; i++)
            map.add(new ArrayList<Character>());
        boolean down = true;
        int n = s.length();
        int i = 0, row = 0;
        while (i < n) {
            if (row < numRows && down) {
                map.get(row++).add(s.charAt(i++));
            } else if (row >= 0 && !down) {
                map.get(row--).add(s.charAt(i++));
            } else {//ajust direction
                row += down ? -2 : 2;
                down = !down;
            }
        }
        // read map row by row
        StringBuilder ret = new StringBuilder();
        for (ArrayList<Character> line : map) {
            for (Character c : line) {
                ret.append(c);
            }
        }
        return ret.toString();
     }
    // method 2
    public String convert2(String s, int numRows) {
        if (s == null || s.length() <= 1 || numRows == 1) 
            return s;
        int n = s.length();
        int delta = 2 * numRows - 2, diff1 = 0;
        StringBuilder ret = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            int j = i;
            diff1 = delta - 2 * i;
            while (j < n) {
                if (diff1 > 0)
                    ret.append(s.charAt(j));
                j += diff1;
                diff1 = delta - diff1;
            }
        }
        return ret.toString();
    }
    // method 1
    public String convert1(String s, int numRows) {
        if (s == null || s.length() <= 1 || numRows == 1) 
            return s;
        int n = s.length();
        if (n < numRows) 
            numRows = n;
        int delta = 2 * numRows - 2;
        StringBuilder ret = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            int j = i;
            while (j < n) {
                ret.append(s.charAt(j));
                int next = j + delta - 2 * i;
                if (i != 0 && i != numRows - 1 && next < n) {
                    ret.append(s.charAt(next));
                }
                j += delta;
            }
        }
        return ret.toString();
    }
分享到:
评论

相关推荐

    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 0006-zigzag-conversion.zip

    c c语言_leetcode 0006_zigzag_conversion.zip

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

    在“C语言-leetcode题解之06-zigzag-conversion.c”这一文章中,作者将详细剖析LeetCode平台上编号为6的题目,也就是“ZigZag Conversion(之字形转换)”问题,并提供一个用C语言实现的解决方案。 ZigZag ...

    java-leetcode题解之006-ZigZag-Conversion

    java入门 java_leetcode题解之006_ZigZag_Conversion

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

    本篇将要讨论的题目是LeetCode的第6题“ZigZag Conversion”。这是一个字符串操作问题,其主要目的是将给定的字符串按照一种特殊的“之”字形排列方式进行转换。具体来说,例如输入字符串“PAYPALISHIRING”和数字3...

    _leetcode-python.pdf

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

    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

    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 最多水...

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

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

    leetcode实现strstr-leetcode-js:js刷leetcode

    leetcode实现strstr leetcode-js 记录刷leetcode分析过程,...Conversion 逻辑 2019/09/13 0007 Reverse Integer 逻辑 2019/09/13 0008 String To Integer 2019/09/14 0009 Palindrome Number 双指针 2019/09/17 0010

    分割数组求最大差值leetcode-Leetcode-Road:LeetCode刷题记录

    Conversion 45.6% 中等 7 Reverse Integer 33.2% 简单 8 String to Integer (atoi) 18.5% 中等 9 Palindrome Number 56.7% 简单 10 Regular Expression Matching 25.3% 困难 11 Container With Most Water 59.3% ...

    lrucacheleetcode-leetcode-1:leetcode-1

    Conversion 观察规律 7. Reverse Integer 翻转整数 8. String to Integer 解析字符串 9. Palindrome Number 回文数字 10. Regular Expression Matching 动态规划,列出转换方程即可,注意初值 记T[i][j] = 是否S[0:i...

    八皇后leetcode-Data-Structure-and-Algorithm:数据结构与算法+实践

    八皇后 leetcode DSA(Data Structures and Algorithm) + Practice ...Conversion Algorithm Medium 0007 Reverse Integer Algorithm Easy 0011 Container With Most Water Algorithm Medium 0015 3Sum A

    leetcode分类-LeetCodeSrc:力扣

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

    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分类-LeetCode:LeetCode在线裁判C++

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

    leetcode题库-LeetCode:LeetCode刷题

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

    leetcode知乎-leetcode:leetcode解决方案

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

    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_note:用于记录leetcode题目的解析

    Conversion 7.Reverse Integer 8.String To Integer 9.Palindrome Number 10.String To Integer 11.Container With Most Water 12.Integer To Roman 13.Roman To Integer 289 347 380 442 457 Circular Array Loop ...

Global site tag (gtag.js) - Google Analytics