1. 程式人生 > >Leetcode Medium 0 ZigZag Conversion

Leetcode Medium 0 ZigZag Conversion

第一次 196ms 18%:

class Solution:
    def convert(self, s, numRows):
        l = []
        r = ''
        for i in range(numRows):
            l.append([])
        pos = -1
        forword = True
        for i in range(len(s)):
            if forword:
                pos += 1
                if pos >= numRows - 1:
                    forword = False
            else:
                pos -= 1
                if pos <= 0:
                    forword = True
            l[pos].append(s[i])
        for i in range(numRows):
            for j in range(len(l[i])):
                r += l[i][j]
        return r
        

第二次 104ms 95%:

class Solution:
    def convert(self, s, numRows):
        l = []
        for i in range(numRows):
            l.append('')
        pos = -1
        forword = True
        for i in range(len(s)):
            if forword:
                pos += 1
                if pos >= numRows - 1:
                    forword = False
            else:
                pos -= 1
                if pos <= 0:
                    forword = True
            l[pos] += s[i]
        return ''.join(l)

相關推薦

Leetcode Medium 0 ZigZag Conversion

第一次 196ms 18%: class Solution: def convert(self, s, numRows): l = [] r = '' for i in range(numRows):

LeetCode刷題MediumZigzag 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 i

leetcode題解 6.ZigZag Conversion

paypal 所有 lin 特殊 display log () take lsi 6.ZigZag Conversion 題目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu

LeetCode-Algorithms #006 ZigZag Conversion, Database #180 Employees Earning More Than Their Managers

from arr 結果 toc strong 遍歷 har ++ 以及 LeetCode-Algorithms #006 ZigZag Conversion 給定一個字符串, 以及需要輸出的行數, 將字符串按照給定行數進行Z字形排列(效果見上圖), 然後將得到的結果逐行

LeetCode】6.ZigZag Conversion Z字形變換

示例1: Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR" Explanation: P A H N A P L S I I G Y I R 示例2: Input: s =

LeetCode】6. ZigZag Conversion(C++)

題目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this

LeetCode-Algorithms #006 ZigZag Conversion, Database #181 Employees Earning More Than Their Managers

LeetCode-Algorithms #006 ZigZag Conversion 給定一個字串, 以及需要輸出的行數, 將字串按照給定行數進行Z字形排列(效果見上圖), 然後將得到的結果逐行拼接成一個新的字串, 返回這個新串 我的思路: 通過給定的行數計算每一個字元在Z形排列中位於哪一行並不困難, 如

[LeetCode] 6. ZigZag Conversion (Medium)

原題連結 把字串按照 ↓↗↓……的順序,排列成一個 Z 形,返回 從左到右,按行讀得的字串。 思路: 建立一個二維陣列來按行儲存字串。 按照 ↓↗↓……的方向進行對每一行加入字元。 太慢了這個解法,Runtime: 96 ms, faster than 3.61% of C++。 class So

LeetCode ---6.ZigZag Conversion

nbsp [] zigzag for convert pub tor int version 1 public String convert(String s, int numRows) { 2 if(numRows == 1) return s

LeetCode 006 ZigZag Conversion - Java

mil cnblogs isp 字符串 lsi tex version wan app The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like th

Leetcode:ZigZag Conversion

end lap eat ++ 有效 ges ide -1 pen 問題的大意就是將字符串中的字符按鋸齒狀(倒N形)垂直由上向下放置,最後水平從左向右讀取。比如 ABCDEFGHIJKLMN,4表示 A G M B F H L

LeetCode - 6 - ZigZag Conversion

時間 pen src bind 數量 中間 lee zigzag [] 題目 URL:https://leetcode.com/problems/zigzag-conversion 解法 一、程序員 對於一個程序員來說,肯定是想模擬 ZigZag 的數組,再根據 Zi

LeetCode 6. ZigZag Conversion

for turn ext plsi play lin lis style isp The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this:

LeetCode.6 - ZigZag Conversion

算法 color font may better 真的是 true and 別人   The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this

LeetCode-ZigZag Conversion

static urn stringbu args 鋸齒形 規律 lee ati string 鋸齒形排列 其實就是找到規律就行。 代碼如下: package com.leetcode.study; public class Main { private int

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

LeetCode】#6Z字形變換(ZigZag Conversion)

【LeetCode】#6Z字形變換(ZigZag Conversion) 題目描述 將一個給定字串根據給定的行數,以從上往下、從左到右進行 Z 字形排列。 比如輸入字串為 “LEETCODEISHIRING” 行數為 3 時,排列如下: L C I R E T O E S I I G

#Leetcode# 6. ZigZag Conversion

https ant paypal LISHI better convert tco hrp lis https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt

ZigZag Conversion——LeetCode進階路⑥

原題連結https://leetcode.com/problems/zigzag-conversion/ 沒開始看題目時,小陌發現這道題似乎備受嫌棄,被n多人踩了,還有點小同情 題目描述 The string "PAYPALISHIRING" is written in a zigza

Leetcode 6 ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you