[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" ... 
随机推荐
- JAVA可变参数实例
			public class Kebiancanshu { public static void main(String[] args) { System.out.println(average(8, 2 ... 
- overload的一点思考
			仅参数类型不同的重载方法,使用过程的一个困惑: 有没有必要使用instanceof方法? package overload.special; public class OverLoadTest { p ... 
- php解决与处理网站高并发 大流量访问的方法
			方法/步骤 首先,确认服务器硬件是否足够支持当前的流量 普通的P4服务器一般最多能支持每天10万独立IP,如果访问量比这个还要大, 那么必须首先配置一台更高性能的专用服务器才能解决问题 ,否则怎么 ... 
- ASP.NET repeater添加序号列的方法
			ASP.NET repeater添加序号列的方法 1.<itemtemplate> <tr><td> <%# Container.ItemIndex + 1% ... 
- iOS多线程编程指南(一)关于多线程编程(转)
			原文:http://www.dreamingwish.com/article/ios-multi-threaded-programming-a-multi-threaded-programming.h ... 
- Eclipse快捷键大全(一)
			Eclipse快捷键大全(一) 常用(系统默认): 1.Format (自动排版) : Ctrl+Shift+F 2.Organize Imports (自动导入) : Ctrl+Shift+O 3. ... 
- [置顶] hdu2815 扩展Baby step,Giant step入门
			题意:求满足a^x=b(mod n)的最小的整数x. 分析:很多地方写到n是素数的时候可以用Baby step,Giant step, 其实研究过Baby step,Giant step算法以后,你会 ... 
- Android提高第十一篇之模拟信号示波器
			上次简单地介绍了AudioRecord和AudioTrack的使用,这次就结合SurfaceView实现一个Android版的手机模拟信号示波器(PS:以前也讲过J2ME版的手机示波器).最近物联网炒 ... 
- jquery.ellipsis.js段落超出省略号插件
			为了实现在段落尾部超出文字替换为省略号,自己写的插件,并作了简单的优化. 下面给出脚本演示页面及注释,在此之前介绍一下插件参数 1.lineNum:数字.限制段落的行数 2.english:布尔.英文 ... 
- JS 去除特定符号(逗号)的方法
			<script language="javascript"> var str="asdfk,asdf345345,345345"; //替换除数字与 ... 
