描述

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. codeforce469DIV2——C. Zebras

    题意 0, 010, 01010 这一类的01交替且开头和结尾都为0的序列被称为zebra序列.给出一段01序列,尝试能否把他分为k个子序列使得每个子序列都是zebra序列. 分析 这个题应该算是水题 ...

  2. Win10系统优化/设置脚本

    Win10系统优化/设置脚本 用了很长时间win10了,用的过程中,发现了一些问题,关于系统基本的优化,和个人的使用习惯设置等等,做成了一个脚本,可以一键设置win10的系统设置,结合DWS对Win1 ...

  3. opennebula 安装指定参数

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

  4. 39.FORMAT() 函数

    FORMAT() 函数 FORMAT 函数用于对字段的显示进行格式化. SQL FORMAT() 语法 SELECT FORMAT(column_name,format) FROM table_nam ...

  5. 基于django rest framework做认证组件

    先导入要用到的类 from rest_framework.authentication import BaseAuthentication from rest_framework.exceptions ...

  6. Entity Framework 6.0 Tutorials(3):Code-based Configuration

    Code-based Configuration: Entity Framework 6 has introduced code based configuration. Now, you can c ...

  7. Entity Framework 6.0 Tutorials(1):Introduction

    以下系统文章为EF6.0知识的介绍,本章是第一篇 原文地址:http://www.entityframeworktutorial.net/entityframework6/introduction.a ...

  8. 过渡函数transition-timing-function

  9. 常用eclipse插件

     http://dist.springsource.com/release/TOOLS/update/e4.4/ http://propedit.sourceforge.jp/eclipse/upda ...

  10. Join导致冗余数据引起慢SQL

    业务过程中碰到多个join引起慢SQL问题,数据量不大,但查询很慢,搜到一片BLog,参考解决. 业务过程不记录,以blog内容重现: 原SQL: select distinct abc.pro_co ...