leetcode习题解答:6. ZigZag Conversion
来源:互联网 发布:女生湿 知乎 编辑:程序博客网 时间:2024/06/05 19:35
难度:MEdium
链接
描述:
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 NA P L S I I GY I RAnd 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"
.思路:
要过的话比较简单,按照它的要求把字符放进不同的字符串里,最后拼起来,没什么难度,不知道有没其他解法:
代码:
class Solution: def convert(self, s, numRows): vec = [] for x in range(numRows): vec.append("") total = 0 inner1 = 0 inner2 = numRows - 2 step = 1 for x in s: if step % 2 == 1 or numRows == 2 or numRows == 1: vec[inner1] = vec[inner1]+x if inner1 == numRows-1: step += 1 inner1 = 0 else: inner1 += 1 else: vec[inner2] = vec[inner2]+x inner2 -= 1 if inner2 == 0: step += 1 inner2 = numRows - 2 result = "" for x in vec: result += x return result
阅读全文
0 0
- leetcode习题解答:6. ZigZag Conversion
- [ LeetCode ]- 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
- leetcode 6. ZigZag Conversion
- leetcode 6. ZigZag Conversion
- Leetcode 6. ZigZag Conversion
- [leetcode]6. ZigZag Conversion
- [leetcode] 6. ZigZag Conversion
- LeetCode 6. ZigZag Conversion
- python pass continue
- 数据查找算法总结1
- python 解决中文乱码问题('ascii' codec can't encode characters in )
- Mybatis学习笔记(十四)【延迟加载】
- Ubuntu SMP 16.04.1 个人配置习惯
- leetcode习题解答:6. ZigZag Conversion
- 判断32位整数二进制中1的个数的算法
- spring AOP 领悟
- A NEW DAY
- lintCode627:最长回文串
- eclipse 中安装activiti插件
- 【洛谷2024】食物链 并查集
- 【量化小讲堂-Python&Pandas系列07】数据告诉你:惊人的海龟交易法则
- 不修复软件缺陷的原因