6. ZigZag Conversion
2016-07-26 19:33
381 查看
6. ZigZag Conversion
Leetcode link for this questionDiscription:
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=abcdefghijklmnoprow 1:a g m row 2:b f h l n row 3:c e i k o row 4:d j p return row 1 + row 2 + row 3 +row 4 ,i.e. return agm bfhln ceiko djp so,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[p 9897 o]=li[po]+i if po==0: dir=1 return ''.join(li)
Submission Result:
Status: AcceptedRuntime: 112 ms
Ranking: beats 92.15%
相关文章推荐
- Linux-(C)多线程学习(入门)
- 弹性ListView
- 使用Android Studio查找文件中含有中文字符串位置(正则表达式)
- 1. Two Sum
- Android SyncAdapter同步实践
- 小明的求助
- OI总结(垃圾排版就忽略了吧)
- HDU 5761 Rower Bo
- 如何灵活运用转义字符还有玩转常量0726
- 通过KEPWARE ODBC DRIVER和ADVANCED TAG 实现数据库和PLC的双向通信
- 套题 bestcoder 84
- UVA 10603 Fill【BFS】
- ORACLE中Drop table cascade constraints之后果.
- 谈谈关于内存对齐与补齐
- ListView的使用
- 为什么要删除鲁迅?
- c#获取外网IP地址
- 创建一个Notification (通知)
- 理解RxJava:(四)Reactive Android
- 学习C语言的第二天