LeetCode 6. ZigZag Conversion(java)

原创 2018年01月13日 08:33:05

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

思路:通过观察这个N型的图案的组成,我们发现可以通过从上到下(0 ~ numRows - 1),再从下到上(numRows - 1, 1),以此为反复,就可以得到想要的排列。所以解决的方法就是创建numRows个StringBuilder,每个StringBuilder都可以储存每行的字母,通过N型的排列来将字母一个个加进对应的行里。

public String convert(String s, int numRows) {
        if (s.length() == 0 || numRows <= 1) return s;
        int length = s.length(), index = 0;
        StringBuilder[] sb = new StringBuilder[numRows];
        for (int i = 0; i < numRows; i++) {
            sb[i] = new StringBuilder();
        }
        while (index < length) {
            for (int i = 0; i < numRows && index < length; i++) {
                System.out.println(sb[i]);
                sb[i].append(s.charAt(index));
                index++;
            }
            for (int j = numRows - 2; j >= 1 && index < length; j--) {
                sb[j].append(s.charAt(index));
                index++;
            }
        }
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            res.append(sb[i]);
        }
        return res.toString();
    }

6.小项目.图片解码播放器

本课程是《朱有鹏老师嵌入式linux核心课程》第6部分,是一个课程后的小项目。用开发板本身自带的硬件完成一个基于linux API开发的图片解码播放器,实现了对BMP、JPG、PNG等格式图片进行解码播放的功能。
  • 2016年12月16日 16:01

[Java]leetcode6 ZigZag Conversion

[Java]leetcode ZigZag Conversion
  • fumier
  • fumier
  • 2015-04-12 11:17:26
  • 690

Leetcode_6_ZigZag Conversion

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41408021 看完这篇文章,你可能会学到到知识如下: ...
  • pistolove
  • pistolove
  • 2014-11-23 09:45:55
  • 2786

[LeetCode][6]ZigZag Conversion解析与特殊解法 -Java实现

Q: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this...
  • u014629433
  • u014629433
  • 2016-06-06 14:29:53
  • 2231

LeetCode-6. ZigZag Conversion (JAVA)(之字形转换)

LeetCode-6. ZigZag Conversion (JAVA)(之字形转换)
  • mine_song
  • mine_song
  • 2017-04-17 09:41:03
  • 443

6. ZigZag Conversion Leetcode Python

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you...
  • hyperbolechi
  • hyperbolechi
  • 2015-01-28 02:21:14
  • 1032

[Leetcode]6. ZigZag Conversion @python

题目The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (y...
  • qian2729
  • qian2729
  • 2016-01-13 00:32:16
  • 848

【python】【leetcode】【算法题目6—ZigZag Conversion】

一、题目描述 题目原文:   The string "PAYPALISHIRING" is written in a zigzag pattern on a given number...
  • u014615155
  • u014615155
  • 2016-11-26 15:44:31
  • 478

LeetCode 6 ZigZag Conversion (C,C++,Java,Python)

Problem: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l...
  • runningtortoises
  • runningtortoises
  • 2015-05-06 22:55:25
  • 1143

【LeetCode】ZigZag Conversion 解题报告

【题目】 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like th...
  • ljiabin
  • ljiabin
  • 2014-10-26 16:49:26
  • 23493
收藏助手
不良信息举报
您举报文章:LeetCode 6. ZigZag Conversion(java)
举报原因:
原因补充:

(最多只允许输入30个字)