[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" ...
随机推荐
- scrollTop,offset().top
1.scrollTop是指滚动条滚动的距离 如果没有出现滚动条,则距离为0 css: <style type="text/css"> *{ margin: 0; pad ...
- jquery EsayUi 里一个小弹框
网站后台大多的数据展示就都用和此插件有着密切的关系: 来用一下这个小弹框吧: 一个Html里面的代码 <link rel='stylesheet' type='text/css' href='c ...
- UIButton基础知识
基本属性 1.frame;坐标:title:titlecolor:字体颜色:titleShadowColor:字体阴影:image:图片: backgroundImage:背景图片: 2.forsta ...
- UML-类图,包图
UML构造设计模型 一.类图 二.包图 三.组件图 四.部署图 一.类图 1.类:类由三格表示:类名,类的属性,类的操作 类名: 首字母大学 ...
- 如何使用for循环连续的实例化多个对象!
Test类import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Test ...
- jQuery验证控件(转载)
转自:http://www.cnblogs.com/hejunrex/archive/2011/11/17/2252193.html 官网地址:http://bassistance.de/jquery ...
- 使用正则表达式限制TextBox输入
/// <summary> /// 使用正则表达式限制输入 /// </summary> public class TextBoxRegex : TextBox { // 正则 ...
- 为每个页面加上Session判断 转
首先新建一个类,继承自System.Web.UI.Page,然后重写OnInit,如下: using System; using System.Data; using System.Configu ...
- Android 为应用添加数字角标
今天在论坛上看到了一个帖子,终于搞清了我很久以来的一个困惑,android到底能不能实现ios的角标效果,QQ是怎么实现的.看了这个帖子顿时终于解除了我的困惑. 先说一个下大概的思路: 大家都知道an ...
- Linux下设置开机自启动Tomcat
方法一: linux 下tomcat开机自启动修改Tomcat/bin/startup.sh 为: export JAVA_HOME=/usr/java/j2sdk1..2_08 export CLA ...