Leetcode 6. ZigZag Conversion The Solution of Python
来源:互联网 发布:淘宝店铺主页 编辑:程序博客网 时间:2024/06/05 23:24
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 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”.
python:
class Solution(object): def convert(self, s, numRows): """ :type s: str :type numRows: int :rtype: str """ if numRows==1 or numRows>=len(s):#判断numRows是否为1或超出字符串长度 return s res=['']*numRows#创建一个具有numRows个字符串的列表 index,step,R=0,1,""#定义list索引号和遍历需要的方向 for n in s:#遍历s中元素 res[index]+=n #将n存入到当前索引字符串 if index==0:#判断是否到上边界 step=1 elif index==numRows-1:#判断是否到下边界 step=-1 index+=step#如果没有到边界,则按上一次的方向前进一步 R=R.join(res) return R
以1为步长依次走遍所有字符的思路,还有一种是以每两列为一组,因为没两列是一个轮回,因此判断每一组的方向,加入到字符串中可得结果。
0 0
- Leetcode 6. ZigZag Conversion The Solution of Python
- 6. ZigZag Conversion Leetcode Python
- [Leetcode]6. ZigZag Conversion @python
- [leetcode: Python]6. ZigZag Conversion
- [Leetcode] ZigZag Conversion @Python
- 【Leetcode】【python】ZigZag Conversion
- LeetCode #6 ZigZag Conversion C# Solution
- 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
- 正则表达式的应用&按钮可执行状态的改变
- 超完整的Chrome浏览器客户端调试大全
- [读书笔记] 重构改善既有代码的设计(3)
- web.xml文件详解
- python random
- Leetcode 6. ZigZag Conversion The Solution of Python
- Leetcode 3(Java)
- 查找的方式
- errno值为0时的取值
- 2017 C4天梯 L2-020. 功夫传人
- JAVA多线程之中断机制(stop()、interrupted()、isInterrupted())
- 算法训练 字符串逆序
- SHA1摘要算法
- paho.mqtt.javascript 断开问题