leetcode-006 ZigZag Conversion

[TOC]

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

思路分析

5行
6行

最終結(jié)果就是將上圖中每一行的內(nèi)容去掉中間間隔連接起來(lái)

所以最直觀的方法就是:

  • 生成類似上圖的結(jié)構(gòu)(二維數(shù)組???)
  • 去掉間隔
  • 連接每一行

另外:

若使用二維數(shù)組,最終連接的時(shí)候還得去掉空白。所以可以使用特殊的二維數(shù)組--一維字符串?dāng)?shù)組來(lái)保存上述結(jié)構(gòu)的同時(shí)去掉間隔。

代碼

java

import java.util.Arrays;
public class Solution006 {
    public String convert(String s, int numRows) {
        if (s == null || s.length() == 0 || numRows <= 1)
            return s;
        String rows[] = new String[numRows];
        Arrays.fill(rows, "");
        boolean down = true;
        int row = 0;

        for (int i = 0; i < s.length(); i++) {
            rows[row] += s.charAt(i);

            if (down) {
                row++;
            } else {
                row--;
            }

            if (row >= numRows) {
            //不是減一,因?yàn)榈谝恍行凶詈笠恍卸际且粋€(gè)元素
                row = numRows - 2;
                down = false;
            }

            if (row < 0) {
            //不是零,因?yàn)榈谝恍行凶詈笠恍卸际且粋€(gè)元素
                row = 1;
                down = true;
            }

        }

        StringBuilder sb = new StringBuilder();
        for (String r : rows) {
            sb.append(r);
        }
        return sb.toString();
    }
}

python

class Solution006(object):
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if not s or len(s) == 0 or numRows <= 1:return s
        
        rows = [""] * numRows
        
        down = True;row = 0;
        
        for e in s:
            rows[row] += e
            
            if down:
                row += 1
            else:
                row -= 1
            
            
            if row >= numRows:
                row = numRows - 2
                down = False

            if row < 0:
                row = 1
                down = True
        
        # end for
        
        ret = ""
        for e in rows:
            ret += e
        
        return ret

此處有個(gè)更牛逼的解法:http://www.cnblogs.com/sanghai/p/3632528.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Problem description The string "PAYPALISHIRING" is writte...
    QyQiaoo閱讀 254評(píng)論 0 0
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,890評(píng)論 0 33
  • 拾荒老人“章楷”的故事感動(dòng)的無(wú)數(shù)網(wǎng)友。 這個(gè)被大家認(rèn)為目不識(shí)丁的拾荒老人,真名叫韋思浩。其實(shí)是畢業(yè)于浙江大學(xué)的高材...
    若愚123閱讀 279評(píng)論 0 0
  • 以下均從資料收集提供大家參考使用,如有侵權(quán)請(qǐng)聯(lián)系刪除。 1.CocoaPods CocoaPods是OS X和iO...
    握了根草閱讀 1,118評(píng)論 0 0

友情鏈接更多精彩內(nèi)容