题目来源:

https://leetcode.com/problems/zigzag-conversion/


题意分析:

这道题目是字符串处理的题目。输入一个字符串和一个数字,将字符串填入倒Z形输入字符串,然后按照列读取字符,得到一个新的字符,输出这个字符。例如:字符串"PAYPALISHIRING",3

P   A   H   N
A P L S I I G
Y   I   R    

得到的新字符是”PAHNAPLSIIGYIR”。


题目思路:

这题只是简单的字符串处理。首先,我们可以对Z形字符串进行简单优化一下,将Z中的“折”合起来。比如:“ABCDEFGHIJKL”,4

A     G    
B   F H   L
C E   I K  
D     J    

优化后:

A   G  
B F H L
C E I K
D   J  

不难发现,得到的结果不变,而列表的列数减少了。在C和C++中可以直接定义一个二维数组,将字符读入,然后直接读取即可。但是用python初始化列表比较麻烦,我觉得直接通过下标规律可以直接得到,第一行和最后一行下一个下标是上一个+ 2*numRows – 2;而其他是先+ 2*(numRows - i) – 2 再+ 2*i,i是第几行。


代码(python):

 class Solution(object):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
size = len(s)
if size <= numRows or numRows == 1:
return s
ans = ''
i = 0
while i < numRows:
j = i
if i == 0 or i == numRows - 1:
while j < size:
ans += s[j]
j += 2*numRows - 2
if 2 * numRows - 2 == 0:
break
else:
while j < size:
ans += s[j]
j += 2*(numRows - i) - 2
if j >= size:
break
ans += s[j]
j += 2*i
i += 1
return ans

转载请注明出处:http://www.cnblogs.com/chruny/p/4798575.html

[LeetCode]题解(python):006-ZigZag Conversion的更多相关文章

  1. No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  2. leetcode第六题 ZigZag Conversion (java)

    ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...

  3. LeetCode--No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  4. 【LeetCode】006. ZigZag Conversion

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

  5. 【JAVA、C++】LeetCode 006 ZigZag Conversion

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

  6. [Leetcode]006. ZigZag Conversion

    public class Solution { public String convert(String s, int nRows) { if (s == null || s.isEmpty() || ...

  7. 006 ZigZag Conversion

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

  8. leetcode第六题--ZigZag Conversion

    Problem: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of r ...

  9. LeetCode之“字符串”:ZigZag Conversion

    题目链接 题目要求: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...

  10. LeetCode(6) ZigZag Conversion

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

随机推荐

  1. Hat's Fibonacci(大数,好)

    Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. CocoaPods 安装和使用

    CocoaPods的安装 >1. 打开终端, 输入 gem sources -remove https://rubygems.org/ >2. 再输入 gem sources -a htt ...

  3. iOS的扩展类,扩展属性

    Objective-C有两个扩展机制:Associative和Category.Category用来扩展类方法,Associative用于扩展属性.Associative机制的原理是把两个对象关联起来 ...

  4. BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )

    一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...

  5. Mysql 死锁问题

    Innodb锁系统(4) Insert/Delete 锁处理及死锁示例分析 http://mysqllover.com/?p=431 关于innodb死锁 http://afei2.sinaapp.c ...

  6. hadoop搭建杂记:Linux下JDK环境变量的设置(三种配置环境变量的方法)

    Linux下JDK环境变量的设置(三种配置环境变量的方法) Linux下JDK环境变量的设置(三种配置环境变量的方法) ①修改/etc/profile文件 如果你的计算机仅仅作为开发使用时推荐使用这种 ...

  7. CSS background-repeat 属性

    ###起因 >今天遇到一个问题,就是在给一个元素设置width 属性为100% 之后, 鼠标放上去之后,仍然只有部分是阴影状态,如下图所示: --- 经过一番思索,这TM 不就是,hover 上 ...

  8. character-RNN模型介绍以及代码解析

    RNN是一个很有意思的模型.早在20年前就有学者发现了它强大的时序记忆能力,另外学术界以证实RNN模型属于Turning-Complete,即理论上可以模拟任何函数.但实际运作上,一开始由于vanis ...

  9. 怎样从一个DLL中导出一个C++类

    原文作者:Alex Blekhman    翻译:朱金灿 原文来源: http://www.codeproject.com/KB/cpp/howto_export_cpp_classes.aspx 译 ...

  10. java 执行bat文件 并输出信息

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...