【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 ...
随机推荐
- PHP根据array_map、array_reduce和array_column获取二维数组中某个key的集合
http://camnpr.com/php-python/1715.html 巧用array_map()和array_reduce()替代foreach循环
- web中c#纯网站中引用log4net模块,不记录日志
如题,解决如下: 1.log4net.config配置如下: <?xml version="1.0" encoding="utf-8" ?> < ...
- CocoaPod 使用方法
huangyichengdeMacBook-Pro:~ Jack$ pod search AFNetworking/Library/Ruby/Site/2.0.0/rubygems.rb:250:in ...
- XF custom render 各平台实现类
目前的XF还是非常简陋的,所以存在大量的自定义工作.一般情况下我们只是要需要派生原生的XF控件,然后在各平台下修改其呈现方法. 所以了解每个XF控件在不同平台上呈现使用的控件类是有所必须要的.以下别人 ...
- ibatis 轻松入门
1.总中的配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMapConfig ...
- Entity Framework 使用Mysql的配置文件
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSec ...
- UVA - 11235 Frequent values
2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...
- shell脚本重新挂载出问题的卷
#!/bin/bash#卷的全路径示例#pathexample =mount -t cetusfs 127.0.0.1:/cinderv/var/lib/cinder/volumes/0f6a20f4 ...
- HTML DOM 之<textare>标签
对于DOM元素节点textarea的value值,下面举例说一下. 1. <textarea><textarea> 2.<textarea> <textare ...
- 复习(1)【Maven】
终于开始复习旧知识了,有输入必然要有输出.输入和输出之间的内化过程尤为重要,在复习的同时,真正把学到的东西积淀下来,加深理解. Maven项目概念与配置 Maven是一个项目管理和综合工具.Maven ...