字串z字變換
阿新 • • 發佈:2018-11-10
題目描述 (字串z字變換 leetcode上的題目)
將字串 "PAYPALISHIRING" 以Z字形排列成給定的行數: P A H N A P L S I I G Y I R 之後從左往右,逐行讀取字元:"PAHNAPLSIIGYIR" 實現一個將字串進行指定行數變換的函式: string convert(string s, int numRows); 示例 1: 輸入: s = "PAYPALISHIRING", numRows = 3 輸出: "PAHNAPLSIIGYIR" 示例 2: 輸入: s = "PAYPALISHIRING", numRows = 4 輸出: "PINALSIGYAHRPI" 解釋: P I N A L S I G Y A H R P I
解題思路(本題 我的思路時間複雜度太高,沒有參考價值.所以這裡直接給給出官方的解題思路)
我們可以使用 \text{min}( \text{numRows}, \text{len}(s))min(numRows,len(s)) 個列表來表示 Z 字形圖案中的非空行。
從左到右迭代 ss,將每個字元新增到合適的行。可以使用當前行和當前方向這兩個變數對合適的行進行跟蹤.
只有當我們向上移動到最上面的行或向下移動到最下面的行時,當前方向才會發生改變。
python 實現(官方思路)
class Solution:
def convert(self, s, numRows) :
"""
:type s: str
:type numRows: int
:rtype: str
"""
index,flag = -1,1
num_row = min(len(s),numRows)
# print(num_row)
list_all=[[] for i in range(num_row)]
# print(list_all)
if s is None or len(s)==0:
return ""
elif numRows==1:
return s
else:
for i in range(len(s)):
char = s[i]
# print(char)
index = index+flag
list_all[index].append(char)
if index == num_row-1:
flag = -1
elif index == 0:
flag = 1
#遍歷所有的列表 打印出來
res_str=""
for i in range(num_row):
res_str+="".join(list_all[i])
return res_str
solution=Solution()
s = "ABC"
numRows = 1
print(solution.convert(s, numRows))
python 實現(我的思路 brueforce,建議不要看 沒什麼營養,不過是對的)
import numpy as np
class Solution(object):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
if s=="":
return ""
len_s=len(s)
if numRows==1:
return s
flag=2*numRows-2
# print(flag)
cloum=int((len_s-1)/flag)+1
# print(cloum)
if (cloum-1)*flag+numRows-1<(len_s-1):
cloum =cloum+ (cloum-1)*(numRows-2)+(len_s-1-((cloum-1)*flag+numRows-1))
else:
cloum = cloum + (cloum - 1) * (numRows - 2)
# print(cloum)
matrix = [["xhm" for i in range(cloum)] for i in range(numRows)]
# matrix=np.zeros((numRows,cloum))
j=0
i=0
cloum_index=-1
index=0
while index<len_s:
cloum_index+=1
max_index = cloum_index * flag + numRows - 1
while index<=max_index and index<len_s:
matrix[i][j] = s[index]
i+=1
index+=1
if index<len_s:
# cloum_index+=1
# j += 1
i -= 1
j += 1
i -= 1
while i>=1 and index<len_s:
matrix[i][j] = s[index]
j += 1
i -= 1
index+=1
convert_str=""
for i in range(numRows):
convert_str+="".join(matrix[i])
return convert_str.replace("xhm","")
# PINALSIGYAHRPI
# PINALSIGYAHR
if __name__=='__main__':
solution=Solution()
print(solution.convert("wjkakhxhsglmmhstrwgulfztwhhjlbihmviwehfwntibadvubdomiphgxpsiscsexccbjhazakadnvxqanelemtbdlmvoezlgbprkpqlbtqpqphrcmcgyvkbhwyvcxikazbkquxsnpjdeqwicyrcwbfdzdabcklcmmpciouvedbiwxryyidulizkmblonwtzkkcvayqectpariyrqdldmmnynaoawjaivedwcwcgrrgibhbtkmwwyjwnjnohyqsuuxqwvufnmlxnszhfnfbmpabaprknhchdzzaxufkishxngeswkvkbvlbkdlamphqrhsodzylrhieqpymbuwcrhfemtezklpbuhrxgpkzzvgpkedlyzpqiwuvrywelnfguxfcosdpnjexohkoiberzaotymxmzeuvdbzutcjimqhcxrqiuxbwxrpydokcsgxwhwqdazloptqpmjzjgafftwdwkpacxzafxqkxsvmjqeadpbmvbtbupgsbysdvtecqwmqqiecaicdyervhkyebhwcfricmofdmttddxfehjhhnbdxnbbpiztpsdufrzkeudjycqcjzltpmwmczprkqmblqvqjwcnrfypjotuoenftlrvlioxycylsubcqfrhksyvgrqwyfbtruqecgbdibodvshoxaxksyhbrxxrfbkyvccaifftgtwendulfrxyrebjeaajbljzplzyseryzpenuyazszxldyujzvucidbxqcxiiqjifnxbozbiyatdzqpaljevpisfksovkxfqmctcdumdviiwyxwljcgykadvsrsdqxvfbojelwjgercerapacvypxdmqxevpbsucieitctbikdmdfdfkydzvjlngpkvqcsunyeiaxkijnwnvzsfzyewhpkpewmwbeqocwwetgmcwkrrjkwikahtrtivpurqbjgffdkalwcjjuasgydqamjrftmupfnqqtwxyixmgavp",621))