字符串z字变换

题目描述 (字符串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)) 
    原文作者:Z字形编排问题
    原文地址: https://blog.csdn.net/qq_32806793/article/details/82970678
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞