6. ZigZag Conversion Leetcode Python
来源:互联网 发布:淘宝百度云盘会员出租 编辑:程序博客网 时间:2024/06/05 15:29
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);
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 "PAHNAPL
SIIGYIR".
这题的主要特点是要观察出新序列的位置为 2*nRows-2
代码如下
class Solution: # @return a string def convert(self, s, nRows): if nRows==1: return s if len(s)<=1: return s n=nRows size=2*n-2 result=[] for i in range(n): j=i while j<len(s): result.append(s[j]) if i!=0 and i!=n-1 and j+size-2*i<len(s): result.append(s[j+size-2*i]) j+=size return "".join(result)
0 0
- 6. ZigZag Conversion Leetcode Python
- [Leetcode]6. ZigZag Conversion @python
- [leetcode: Python]6. ZigZag Conversion
- [Leetcode] ZigZag Conversion @Python
- 【Leetcode】【python】ZigZag Conversion
- Leetcode题解(Python): 6. ZigZag Conversion
- LeetCode 6. ZigZag Conversion(Python)兼翻译
- LeetCode 6. ZigZag Conversion
- LeetCode --- 6. ZigZag Conversion
- LeetCode 6.ZigZag Conversion
- [Leetcode] 6. ZigZag Conversion
- 【leetcode】6. ZigZag Conversion
- [LeetCode] 6.ZigZag Conversion
- LeetCode - 6. ZigZag Conversion
- [leetcode] 6. ZigZag Conversion
- Leetcode 6. ZigZag Conversion
- leetcode 6. ZigZag Conversion
- leetcode 6. ZigZag Conversion
- hdu1171 多重背包
- poj1742 多重背包单调队列
- 服务器tomcat无法发布web项目
- NYOJ 168 房间安排
- 周二先mark一下
- 6. ZigZag Conversion Leetcode Python
- 【three.js】创建一个场景
- Hibernate 多对多 中间表含有其他字段 注解方式实现
- 关于解决windows7下cmd窗口运行不了helloworld的解决办法
- sort
- 排序sort
- Android RelativeLayout
- Dijkstra算法——C#实现版
- ECS Ubuntu14.04 搭建gitlab 记录