LeetCode6. Z字形变换
描述
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 s, int numRows);
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
思路
如果稍微考虑的数学一点,那么s中的第i个字符(下标从第0个开始),如果按照zigzag书写方式会出现在的行数为(行数为0到numRows-1行):
- i % (2 * numRows - 2), if i % (2 * numRows - 2) < numRows
- 2 * numRows - 2 - (i % (2 * numRows - 2)), if i % (2 * numRows - 2) >= numRows
有了这个结果,对于任意一个位置的字符我们都知道它应该在第几行。
class Solution:
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
# 考虑到特殊情况,即行数小于等于1或者行数大于等于字符串长度
if numRows <= 1 or numRows >= len(s):
return s
arr = [''] * numRows
for i in range(len(s)):
tmp = i % (numRows + numRows - 2)
if tmp < numRows:
arr[tmp] += s[i]
else:
arr[numRows + numRows - 2 - tmp] += s[i]
return ''.join(arr)
GitHub地址:https://github.com/protea-ban/LeetCode
LeetCode6. Z字形变换的更多相关文章
- [Swift]LeetCode6. Z字形变换 | ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode6.Z字形变换 JavaScript
将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L C I R E T ...
- Leetcode6. Z 字形变换
> 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,按下右上下右上排列后输出字符串的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- Leetcode题库——6.Z字形变换
@author: ZZQ @software: PyCharm @file: convert.py @time: 2018/9/20 20:12 要求: Z字形变换 将字符串 "PAYPAL ...
- LeetCode Golang 6. Z 字形变换
6. Z 字形变换 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L ...
- Z 字形变换 C++实现 java实现 leetcode系列(六)
Z 字形变换 java实现 C++实现 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 ...
- Leetcode(6)Z字形变换
Leetcode(6)Z字形变换 [题目表述]: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ...
- Java实现 LeetCode 6 Z字形变换
6. Z 字形变换 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L ...
随机推荐
- Gym 101128A :Promotions (Southwestern Europe Regional Contest )
题意 一个公司里有E个员工P个上下级关系.这个公司有一种晋升制度.如果要晋升员工a,那么必须要先晋升a的所有领导.给出一个区间[A,B],如果要晋升A个员工,有哪些员工是一定会被晋升的?如果要晋升B个 ...
- 【bzoj2818】Gcd
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4344 Solved: 1912[Submit][Status][Discuss ...
- 图解KMP算法
- 跨平台的图形软件Dia
一款非常不错的软件Dia,软件很小,免费.好用.跨平台(linux.windows.mac).可导出多种格式图片,除了流程图.UML建模图,还可以绘制其他很多图. ubuntu下可以直接通过命令行su ...
- 利用 Aspose.Words 组件,在不依赖与 Office 组件的情况下把 Word 文件转换成 HTML 代码。
首先利用 Nuget 获取 Aspose.Words.dll public ActionResult AsposeWordsDemo() { string srcFileName = Server.M ...
- 树形DP-----HDU4003 Find Metal Mineral
Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Other ...
- 激光样式——第九届蓝桥杯C语言B组(国赛)第二题
原创 标题:激光样式x星球的盛大节日为增加气氛,用30台机光器一字排开,向太空中打出光柱.安装调试的时候才发现,不知什么原因,相邻的两台激光器不能同时打开!国王很想知道,在目前这种bug存在的情况下, ...
- Ubuntu配置ip和dns后还是不能访问外网
https://blog.csdn.net/WFping518/article/details/81011722
- FileTracker:error FTK1011编译错误的原因和解决办法
原因: 今天创建好项目名字完成关了VS,后感觉文件夹名字不太对改了一下,后来程序就调试不了出现FileTracker:error FTK1011编译错误0.0,经过网络查询应该是路径问题 解决方法: ...
- 使用VS Code开发C++
1. 参考/转载 vs code进行c/c++开发 VSCode 的第一个C++程序(windows)[更新2018.10.28] 2. C++开发相关插件(扩展商店中直接搜索) 至少要装C/C++. ...