# -*- 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的更多相关文章

  1. LeetCode 6. ZigZag Conversion & 字符串

    ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...

  2. Leetcode 6. ZigZag Conversion(找规律,水题)

    6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...

  3. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  4. LeetCode 6 ZigZag Conversion 模拟 难度:0

    https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...

  5. LeetCode 6 ZigZag Conversion(规律)

    题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...

  6. [LeetCode 题解]: ZigZag Conversion

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 The string ...

  7. [LeetCode] 6. ZigZag Conversion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  8. 【leetcode】ZigZag Conversion

    题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

  9. leetcode 6. ZigZag Conversion

    https://leetcode.com/problems/zigzag-conversion/ 题目: 将字符串转化成zigzag模式. 例如 "abcdefghijkmlnpq" ...

随机推荐

  1. Almost Prime

    Description Almost Prime time limit per test: 2 seconds memory limit per test: 256 megabytes input: ...

  2. 论山寨手机与Android联姻 【9】SmartPhone的硬件结构

    如何区别智能手机(SmartPhone)与功能手机(FeaturePhone)? 有一种观点认为,智能手机本质上是功能手机与便携式电脑(Laptop PC)的结合.功能手机的功能受限于制造厂商的预制, ...

  3. 提升进程权限为DEBUG权限

    在网上也看到了一些提升进程令牌的函数但都不怎么好用,最后我还是从一个黑客后门程序的源代码中提取出了一个好的提升进程令牌的函数,不敢独享,跟大家分享下.那个后门真的写的很好... Hysia提示你: 这 ...

  4. 一个使用enum实现多态的例子

    下面的写法与直接 使用静态方法而言,可读性.可维护性更强是不是有DSL的感觉.... 当然enum反编译后,的确就是静态方法. /** * Created by MyWorld on 2016/8/3 ...

  5. Codeforces 325D

    #include <cstdio> #include <algorithm> #include <cstring> #include <cstdlib> ...

  6. UITableViewCell性能优化

    5.UITableViewCell性能优化 > 定义一个循环利用标识 static NSString *ID = @"C1"; > 从缓存池中取出可循环利用的cell ...

  7. c语言结构体排序示例

    设计性实验编程实现对学生成绩表的相关信息排序.实验要求:⑴ 建立一个由n个学生的考试成绩表,每条信息由学号.姓名和分数组成.⑵ 按学号排序,并输出排序结果.⑶ 按分数排序,分数相同的则按学号有序,并输 ...

  8. Math.round(11.5)等于()Math.round(-11.5)等于()

    几天前去面试,这道简单的题目居然做错了,看来基础就是慢慢积累的.并不断使用和复习才会成为高手,假设基础不是那么熟练.恐怕在成为高手的路上会困难重重.所以在做项目的间歇时间.偶尔回顾一下最基础的知识.是 ...

  9. Jackson 框架,轻易转换JSON【转】

    Jackson 框架,轻易转换JSON Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json.xml转换成Java对象. 前面有介绍过json-lib这个框架,在 ...

  10. .net通用权限框架B/S (三)--MODEL层(2)

    接上篇 实体数据模型保存后生成上下文和实体 上下文和实体实际是由根据.tt模版生成的 当实体数据模型.edmx更新保存后,上下文和实体就会根据.tt模版自动更新 生成的上下文继承 DbContext ...