【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 ...
随机推荐
- centos 单独安装apachebench
这两天在测试php性能优化方法. 为了做压力测试可观察效果,就选择了ApacheBench来作为压力测试工具.其实就是大家常说的ab. 但是这个工具是安装apache web server的时候自带的 ...
- 我的第一个wcf
vs2012中新建一个解决方案 新建WCF项目RestApi 添加实体类 [DataContract] public class Employee { private Guid id; private ...
- iOS组件化思路 <转>
随着应用需求逐步迭代,应用的代码体积将会越来越大,为了更好的管理应用工程,我们开始借助CocoaPods版本管理工具对原有应用工程进行拆分.但是仅仅完成代码拆分还不足以解决业务之间的代码耦合,为了更好 ...
- MySQL主从复制实现
上回提到了用ThinkPHP框架来实现数据库的读写分离,现在就来简单说说MySQL的主从复制. 形式 一主一从(也就是这里要实现的形式) 主主复制 一主多从 多主一从(MySQL5.7开始支持) 联级 ...
- EBS 中经常用到的一些值集
名称 值集 ...
- Asp.Net Core--发布到IIS
翻译如下: 支持的操作系统 Windows 7及更高版本 Windows Server 2008 R2及更高版本 概念上,本文档中描述的IIS配置也适用于在Nano Server IIS上托管ASP. ...
- PHP代码获取客户端IP地址经纬度及所在城市
echo $_SERVER['HTTP_HOST'];//echo $_SERVER['REQUEST_URI'];$getIp=$_SERVER["REMOTE_ADDR"];e ...
- Android下LayoutInflater的使用
在我们想XML布局文件转换为View对象的时候.我们都会使用LayoutInflate对象.顾名思义咋一眼就能看出来他是布局填充器.那么接下来看看LayoutInfalte的使用 总体分为 Layou ...
- Android中Button的五种监听事件
简单聊一下Android中Button的五种监听事件: 1.在布局文件中为button添加onClick属性,Activity实现其方法2.匿名内部类作为事件监听器类3.内部类作为监听器4.Activ ...
- 触发bfd 的条件
满足下列条件之一就可触发BFC [1]根元素,即HTML元素 [2]float的值不为none [3]overflow的值不为visible [4]display的值为inline-block.tab ...