题目简述

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

  1. 【LeetCode】ZigZag Conversion(Z 字形变换)

    这道题是LeetCode里的第6道题. 题目要求: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ...

  2. 【Leetcode】【Easy】ZigZag Conversion

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

  3. 【Leetcode-easy】ZigZag Conversion

    思路1:String[numRow]行字符串数组.读取原始字符串每一个字符,设置行变量 nrow和行标志位flag(向下一行为1或向上一行为-1).将该字符连接到数组中对应的行字符串,同时nrow+= ...

  4. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  5. 【LeetCode】设计题 design(共38题)

    链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. web api9

  2. Digester组件

    刚认识Digester,记录一下: Digester随着Struts的发展以及其的公用性而被提到commons中独自立项,是apache的一个组件 apache commons-digester.ja ...

  3. centos6.5 mysql-server 5.1.73启动失败

    yum install mysql-server 安装mysql服务端会把相应的客户端也装上 service mysqld start  启动mysql服务 解决办法: 1.chomod 777  / ...

  4. 用递归调用实现字符串反转(java版)

    写一个函数,输入int型,返回整数逆序后的字符串.如:输入123,返回“321”. 要求必须用递归,不能用全局变量,输入必须是一个参数,必须返回字符串. public static String re ...

  5. robotframework,selenium启动不了打不开浏览器的问题访问不了网页

    由于最近发现咨询火狐浏览器打不开的问题比较多,现罗列几点解决办法. 1,由于selenium更新3.0的原因导致不在默认支持火狐浏览器,且支持的火狐浏览器大概在45以上的版本,所以很多都由于这个原因导 ...

  6. Sql Server函数全解<一>字符串函数

    阅读目录 1.ASCII()函数 2.CHAR()函数 3.LEFT()函数 4.RIGHT()函数 5.LTRIM()函数 6.RTRIM()函数 7.STR()函数 8.字符串逆序的函数REVER ...

  7. Maven pom.xml 元素配置说明(一)

    部分来源: Maven中 dependencies 节点和 dependencyManagement 节点的区别 dependencies与dependencyManagement的区别 maven ...

  8. php检测文件内容编码的方法

    核心用到的是mb_convert_encoding函数,示例代码如下: <?php header("Content-type: text/html; charset=utf-8&quo ...

  9. Xcode LLDB 调试Tips

    1.  bt -- 显示当前线程调用堆栈 2.  e -- 执行表达式,动态修改当前线程变量的值 3.  frame variable -- 打印当前堆栈所有变量值 4.  expr -O --lan ...

  10. Redis启动多端口、运行多实例

    默认Redis程序安装在/usr/local/redis目录下: 配置文件:/usr/local/redis/redis.conf,该配置文件中配置的端口为默认端口:6379: Redis的启动命令路 ...