6. ZigZag Conversion
来源:互联网 发布:淘宝域名是什么 编辑:程序博客网 时间:2024/06/06 14:27
6. ZigZag Conversion
Leetcode link for this question
Discription:
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"
. Analyze:
If nRows=4, text=abcdefghijklmnop
row 1:a g mrow 2:b f h l nrow 3:c e i k orow 4:d j p return row 1 + row 2 + row 3 +row 4 ,i.e. return agm bfhln ceiko djpso,assigned order is:row 1, row 2, row 3, row 4, row 3, row 2, row 1,row 2, row 3 etc.the ith char in text will assigned to the row which NO. is the value of the ith index of the above list.
Code 1:
class Solution(object): def convert(self, s, numRows): """ :type s: str :type numRows: int :rtype: str """ if numRows==1: return s li=['']*numRows po=-1 dir=1 for i in s: if dir: po+=1 li[po]=li[po]+i if po==numRows-1: dir=0 else: po-=1 li[po]=li[po]+i if po==0: dir=1 return ''.join(li)
Submission Result:
Status: Accepted
Runtime: 112 ms
Ranking: beats 92.15%
0 0
- LeetCode 6. ZigZag Conversion
- LeetCode --- 6. ZigZag Conversion
- LeetCode 6.ZigZag Conversion
- [Leetcode] 6. ZigZag Conversion
- 6.ZigZag Conversion
- 6. ZigZag Conversion
- 6. ZigZag Conversion
- 【leetcode】6. ZigZag Conversion
- 6.ZigZag Conversion
- 6. ZigZag Conversion
- 6.ZigZag Conversion
- [LeetCode] 6.ZigZag Conversion
- LeetCode - 6. ZigZag Conversion
- 6. ZigZag Conversion
- 6. ZigZag Conversion
- 6.ZigZag Conversion(93.10%)
- 6. ZigZag Conversion
- [leetcode] 6. ZigZag Conversion
- UVA 10603 Fill【BFS】
- Android SyncAdapter同步实践
- 1. Two Sum
- 弹性ListView
- 使用Android Studio查找文件中含有中文字符串位置(正则表达式)
- 6. ZigZag Conversion
- [SCU4437] Carries [2015 Sichuan Province Contest Final B]
- 7. Reverse Integer
- drop table中cascade的含义及用法
- 类与类:接口与接口:类与接口
- 阅一道简单程序,了解多线程编程;
- UVALive - 2701 Find The Multiple
- 13. Roman to Integer
- Material Design的其他使用