描述

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字形变换的更多相关文章

  1. [Swift]LeetCode6. Z字形变换 | ZigZag Conversion

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

  2. LeetCode6.Z字形变换 JavaScript

    将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L C I R E T ...

  3. Leetcode6. Z 字形变换

    > 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,按下右上下右上排列后输出字符串![在这里插入图片描述](https://img-blog.csdnimg.cn/4578280a7c1848c ...

  4. C#版[击败100.00%的提交] - Leetcode 6. Z字形变换 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  5. Leetcode题库——6.Z字形变换

    @author: ZZQ @software: PyCharm @file: convert.py @time: 2018/9/20 20:12 要求: Z字形变换 将字符串 "PAYPAL ...

  6. LeetCode Golang 6. Z 字形变换

    6. Z 字形变换 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L ...

  7. Z 字形变换 C++实现 java实现 leetcode系列(六)

    Z 字形变换  java实现 C++实现  将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 ...

  8. Leetcode(6)Z字形变换

    Leetcode(6)Z字形变换 [题目表述]: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ...

  9. Java实现 LeetCode 6 Z字形变换

    6. Z 字形变换 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L ...

随机推荐

  1. Python中sort与sorted函数

    python中列表的内置函数sort()可以对列表中的元素进行排序,而全局性的sorted()函数则对所有可迭代的序列都是适用的: 并且sort()函数是内置函数,会改变当前对象,而sorted()函 ...

  2. 【转】Provisional headers are shown

    在chrome开发者工具的 Network 面板中,某些请求头后面会跟着下面这行文字: Provisional headers are shown 这种请求实际上根本没有产生,对应的请求头当然也不应该 ...

  3. 新装的Ubuntu没有ipconfig和ping命令

    新装的Ubuntu或者Docker pull的Ubuntu镜像创建的容器没有ipconfig和ping命令 解决办法: 1.apt-get update 2.安装和ifconfig有关的工具 apt ...

  4. C语言实践 输出100以内的素数

    int main() { int isprime = 1; for (int i = 2; i < 101; i++) { isprime = 1;//要确保每次循环都要把这个值设置为1,不然上 ...

  5. Luogu 4159 [SCOI2009]迷路

    BZOJ 1297 应当是简单题. 发现边权的数量很小,所以我们暴力把一个点拆成$9$个点,然后把$(x, i)$到$(x, i + 1)$连边,代表转移一次之后可以走回来:对于每一条存在的边$(i, ...

  6. PS插件开发plugin

    Photoshop插件开发 VC++制作Photoshop自动化插件:http://blog.sina.com.cn/s/blog_73c52fda0101c7hw.html Photoshop 的扩 ...

  7. poj2299 Ultra-QuickSort(线段树求逆序对)

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  8. 再解炸弹人——BFS

    原创 之前用了枚举法解炸弹人,题目详情请看我之前的博客:https://www.cnblogs.com/chiweiming/p/9295262.html 利用枚举法是无视地图布局的,枚举法直接全局搜 ...

  9. js任意位数求和

    <script> //任意位数求和 function sum(){ if(arguments.length==1) { console.log(arguments[0]) return; ...

  10. 21天学通C++学习笔记(二):C++程序的组成部分

    1. 预处理器编译指令 预处理器是一个在编译前运行的工具. 预处理器编译指令是向预处理器发出的命令,总是以磅字符#大头. #include让预处理器获取指定文件并将它们放在编译指令所处的位置.#inc ...