题目来源:

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. POJ 3111 K Best(最大化平均值)

    题目链接:click here~~ [题目大意]有n个物品的重量和价值各自是Wi和Vi.从中选出K个物品使得单位重量的价值最大,输出物品的编号 [解题思路]:最大化平均值的经典.參见click her ...

  2. SDOTOJ2088 refresh的停车场(栈和队列)

     refresh的停车场 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit S ...

  3. css背景渐变兼容(兼容所有ie)

    css3里面一行可以搞定的事,换到ie里,要用滤镜,在网上找了很多,不知道什么原因都没用,终于找到个有用的,放在这里,方便大家用,自己也找得到~ 完整型代码,兼容所有浏览器: ​background: ...

  4. Ubuntu 14.04 静态IP设置

    1. 编辑/etc/network/interfaces vim /etc/network/interfaces 2.将以下五项添加到/etc/network/interfaces中 Static d ...

  5. Qt(QML)本地化

    Internationalization and Localization with Qt Quick 程序国际化 1) Use qsTr() for all  Literial UI strings ...

  6. WinSock网络编程基础(3)server

    上一篇讲的是简单的发送数据的客户端的实现.接下来讲的是如何实现收发数据服务器.这里说的服务器其实就是一个进程,它需要等待任意数量的客户端与之建立起连接,以便响应它们的请求. 服务器必须在已知的名称上监 ...

  7. c#实现pdf另存为功能

    c#实现pdf另存为功能 /// <summary> /// PDF另存为效果 /// </summary> /// <param name="fileName ...

  8. apktool 反翻译错误

    -出现 UndefinedResObject Exception : 这是因为被反编译的apk中有当前的framework不支持的属性:解决方式如下: 1.删除C:\Users\Administrat ...

  9. SICP 习题 (1.13) 解题总结

    SICP习题1.13要求证明Fib(n)是最接近φn/√5 的整数,其中φ=(1+√5)/2 .题目还有一个提示,提示解题者利用归纳法和斐波那契数的定义证明Fib(n)=(φn - ψn) / √5 ...

  10. input 输入框获得/失去焦点时隐藏/显示文字(jquery版)

    input输入框在获得或失去焦点时隐藏或显示文字,这样的焦点效果想必很多朋友在填写form表格的时候都曾见识过吧,本文使用jquery实现以下,感兴趣的朋友可以参考下哈 大家可以看效果图的搜索输入框, ...