描述

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. Centos7.2 下搭建LNMP环境(终极版)Yum安装

    PHP7.1+Nginx+MySQL5.7 安装PHP //安装源只要遇到选择的全是Y rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-rele ...

  2. MyGeneration代码生成工具

    使用MyGeneration 生成代码:转自http://www.cnblogs.com/jack-liang/archive/2011/08/18/2144066.html 我们经常用数据访问层和业 ...

  3. opennebula 安装指定参数

    [root@opennebula opennebula-]# ./install.sh -u oneadmin -g oneadmin -k -d /home/oneadmin/ -u 指定用户-g ...

  4. How to install Freemind 1.0.1 to Ubuntu 14

    安装了Freemind0.9后发现不能打开windows的1.0.1保存的*.mm文件,便对版本开始升级. 1. 从http://freemind.sourceforge.net/wiki/index ...

  5. Java生产者消费者问题

    1. package interview.thread; import java.util.LinkedList; import java.util.Queue; import org.apache. ...

  6. loj2512 [BJOI2018]链上二次求和

    传送门 分析 咕咕咕 代码 #include<iostream> #include<cstdio> #include<cstring> #include<st ...

  7. Django框架 之 Form表单和Ajax上传文件

    Django框架 之 Form表单和Ajax上传文件 浏览目录 Form表单上传文件 Ajax上传文件 伪造Ajax上传文件 Form表单上传文件 html 1 2 3 4 5 6 7 <h3& ...

  8. UML类之间的关系

    原文:http://www.cnblogs.com/me115/p/4092632.html 下面详细介绍这六种关系: 类之间的关系 泛化关系(generalization) 类的继承结构表现在UML ...

  9. 深度学习:原理与应用实践(张重生) - Caffe

    如今,深度学习是国际上非常活跃.非常多产的研究领域,它被广泛应用于计算机视觉.图像分析.语音识别和自然语言处理等诸多领域.在多个领域上,深度神经网络已大幅超越了已有算法的性能. 本书是深度学习领域的一 ...

  10. 探索Asp.net mvc 的文件上传(由浅入深)

    代码下载:http://files.cnblogs.com/n-pei/mvcfileupload.zip 最近因为TeamVideo需要用到视频和图片上传功能,所以试着Google了很多资料,和大家 ...