[LeetCode][Python]ZigZag Conversion
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com'
https://oj.leetcode.com/problems/zigzag-conversion/ 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". ===Comments by Dabay===
用nRows个数组来接受每一个字符。 当nRows等于1时,直接返回s。 当nRows大于等于2时,定义nRows个数组,
2*nRows-2 为一个循环。从第一行到最后一行,再从倒数第二行回到第二行。
用模来确定位置,
当小于等于nRows-1的时候,直接放到模对应的数组里面
当大于nRows-1的时候,这里是数学题了:
mod-(nRows-1)为应该往上数的行数,最后一行的标号为nRow-1,所以这个行标应该是:(nRow-1)-(mod-(nRows-1))=2*nRows-mod-2
''' class Solution:
# @return a string
def convert(self, s, nRows):
if nRows == 1:
return s
array2d = []
for n in range(0, nRows):
array2d.append([])
for i in range(0, len(s)):
mod = i % (2*nRows-2)
if mod <= nRows-1:
array2d[mod].append(s[i])
else:
x = 2*nRows - mod -2
array2d[x].append(s[i])
val = ""
for n in range(0, nRows):
for v in array2d[n]:
val = val + v
return val def main():
s = Solution()
print s.convert("PAYPALISHIRING", 3) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[LeetCode][Python]ZigZag Conversion的更多相关文章
- LeetCode 6. ZigZag Conversion & 字符串
ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...
- Leetcode 6. ZigZag Conversion(找规律,水题)
6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- LeetCode 6 ZigZag Conversion 模拟 难度:0
https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...
- LeetCode 6 ZigZag Conversion(规律)
题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...
- [LeetCode 题解]: ZigZag Conversion
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 The string ...
- [LeetCode] 6. ZigZag Conversion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【leetcode】ZigZag Conversion
题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
- leetcode 6. ZigZag Conversion
https://leetcode.com/problems/zigzag-conversion/ 题目: 将字符串转化成zigzag模式. 例如 "abcdefghijkmlnpq" ...
随机推荐
- EditText 空指针问题
今天在Android中碰到了这样一个问题,其实应该很少人会碰到,因为只有像我这种奇葩才会犯这种错误. 但既然解决了,我就想在这里跟大家分享一下,毕竟它困扰了我一个白天啊...不多说了,看下面... 其 ...
- Spring包的方法WebUtils.getParametersStartingWith(request,String)
举个例子,比如页面上有 <input type="text" name="p_name" value="aileen"> < ...
- block,inline,inline-block
block元素的特点是: 总是在新行上开始: 高度,行高以及顶和底边距都可控制: 宽度缺省是它的容器的100%,除非设定一个宽度 <div>, <p>, <h1>, ...
- Oracle EBS-SQL (WIP-4):检查检查成品标准作业是否勾选"固定"标识.sql
select WE.DESCRIPTION 任务说明, ...
- vim calendar插件配置
近日学习markdown,试着记个日志,安装了vim的知名插件calendar:https://github.com/mattn/calendar-vim. 使用网上配置,发现回车之后日期是昨天的,于 ...
- 如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等
http://blog.chinaunix.net/uid-20662820-id-4023733.html http://www.faqs.org/faqs/snmp-faq/part2/ http ...
- delphi 打开文件夹并定位到一个文件(使用ShellExecute时加一个select参数,原来这么简单!)
strFileName := FcxLV[nIndex].Items.Item[FcxLV[nIndex].ItemIndex].SubItems.Strings[0]; //路径 ShellExe ...
- mysql 插入前 锁表问题
$dbh = DBI->connect("dbi:mysql:database=$db_name;host=$ip;port=3306",$user,$passwd,{ Ra ...
- egret随笔-egret浅入浅出
•不知道有多人跟笔者一样,喜欢学各种技术,但是都不精,但也有一两项算是精的. 自从踏上了egret游戏开发的道路,就不得不学习各种技术了,因为,要精通egret,首先必须要会TypeScript,其次 ...
- 把给定的字符串解析为Date对象
把给定的字符串解析为Date对象: /** * <pre> * 把给定的字符串解析为Date对象 * </pre> * * @param str 要进行解析的字符串 * @pa ...