【leetcode】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".
解题思路:
这个题目有两种做法:第一种是模拟,也就是说我们可以有几行就开几个数组都存起来,最后合到一起就可以了;另一种方法是找规律。因为第一种比较直观,故代码实现了第一种思路:
class Solution:
# @return a string
def convert(self, s, nRows):
if nRows == 1:
return s
gap = nRows - 2
res = []
for i in range(nRows):
res.append([])
i = 0
while i < len(s):
for j in range(nRows):
if i >= len(s):
break
res[j] += s[i]
i += 1
for j in range(gap,0,-1):
if i >= len(s):
break
res[j] += s[i]
i += 1
ress = ""
for i in res:
for j in i:
ress += j
return ress
【leetcode】ZigZag Conversion的更多相关文章
- 【LeetCode】ZigZag Conversion(Z 字形变换)
这道题是LeetCode里的第6道题. 题目要求: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ...
- 【Leetcode】【Easy】ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【Leetcode-easy】ZigZag Conversion
思路1:String[numRow]行字符串数组.读取原始字符串每一个字符,设置行变量 nrow和行标志位flag(向下一行为1或向上一行为-1).将该字符连接到数组中对应的行字符串,同时nrow+= ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【LeetCode】设计题 design(共38题)
链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- fedora自带OpenJDK,所以如果安装官方的JDK的话要先删除OpenJDK
1:rpm -qa|grep jdk 查看当前的jdk情况. 2:yum -y remove java java-1.7.0-openjdk* 卸载openjdk,这个过程中因为依赖原因可能会卸载一些 ...
- SVN 图标消失
问题描述:第一次安装完好,电脑重启后,svn小图标消失不见.查看注册表,发现属于svn小图标的注册表都消失不见. 问题分析:删除杀毒软件(360),因为电脑安装了360,ShellIconOverla ...
- nginx配置杂记
1.一个接口的形式要求是:IP+端口,并且通信协议类型是:https,如何做域名解析: ①设置一个端口.同时在防火墙中打开这个端口,重启防火墙: ②在服务器上/etc/nginx/conf.d的目录下 ...
- 用Model-View-ViewModel构建iOS App
如果你已经开发一段时间的iOS应用,你一定听说过Model-View-Controller,即MVC.MVC是构建iOS App的标准模式.然而,最近我已经越来越厌倦MVC的一些缺点.在本文,我将重温 ...
- C#语言之“中英文混合字符串对齐”的方法
参考自:(1)http://www.cnblogs.com/cnluoke/articles/1213398.html (2)http://www.cnblogs.com/sql4me/archive ...
- 他山之石——vs2013 安装与部署及程序打包
C#打包需要这个:InstallShield 2013 Limited Edition for Visual Studio .下载地址: InstallShield 2013 Limited Edi ...
- 关于JavaScript设计模式(一)
以后都在简书写文章了,所以这个转载我在简书中写的.地址这里 http://www.jianshu.com/p/c7b3c2c148c5
- dom 无法找到 body节点问题
最近在学习html dom节点知识时候,对照代码自己敲了一边,始终获取不到文档中的body对象,代码如下(未修改前): <!doctype html> <html> <h ...
- iOS9新特性 window决定程序的状态栏管理问题
Xcode7升级之后遇到的问题 问题一: 老项目在Xcode6上运行没有任何问题,但在Xcode7上运行直接崩了! 经过一波分析: 发现是因为我顶部状态栏处添加了topWindow,用于处理Tab ...
- android-获得".apk"文件的相关信息。包名、版本号等等
String filePath = "/sdcard/feijiedemo.apk"; PackageManager packageManager = getPackageMana ...