题目来源:

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. HDU 5062 Beautiful Palindrome Number(数学)

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5062 Problem Description A positive integer x can re ...

  2. 大型票务系统中username和password的安全性问题

    讨论请移步至:http://www.zhiliaotech.com/ideajam/idea/detail/307 相关文章: <今天你买到票了吗?--从铁道部12306.cn站点漫谈电子商务站 ...

  3. AngularJs (二) 搭建Deployd 服务爬坑

    Deployd 爬坑 按照书上的教程,介绍Deployd 这个东东,首先进入其deployd.com/网页,发现这个东东着实厉害. THE SIMPLEST WAY TO BUILD AN API 按 ...

  4. JavaScript之面向对象学习六原型模式创建对象的问题,组合使用构造函数模式和原型模式创建对象

    一.仔细分析前面的原型模式创建对象的方法,发现原型模式创建对象,也存在一些问题,如下: 1.它省略了为构造函数传递初始化参数这个环节,结果所有实例在默认的情况下都将取得相同的属性值,这还不是最大的问题 ...

  5. isEmpty()

    String a = new String(); 此时a是分配了内存空间,但值为空,是绝对的空,是一种有值(值存在为空而已) String b = ""; 此时b是分配了内存空间, ...

  6. MFC解决View中添加控件闪烁

    一.简介 我们经常会在我们的View类中添加各种类型的控件,列表控件就是最常用的了.但是我们发现添加控件的时候会,在窗口变化的时候会导致各种各样的闪烁,让我们烦恼异常.所以我对此找到新的解决方案. 二 ...

  7. Exception和RuntimeException

    public class RuntimeExceptionDemo01 { public static void main(String[] args) {     String string=&qu ...

  8. 一个关于css3背景透明的例子

    大家都知道使用opacity调节透明度不仅是背景透明了而且选择区域的文字也跟着透明了, 这是我们不想要的效果,于是强大的css3便有了只让背景透明的功能 那就是background:rgba(0,0, ...

  9. Centos6.4 搭建Git服务器 (最简单的方法)

    下载 git-1.8.2.tar.gz tar -zvxf git-1.8.2.tar.gz cd git-1.8.2.2 sudo make prefix=/usr/local/git all su ...

  10. Mysql笔记之 -- 小试MYSQL主从配置

    mysql主从配置: 硬件: 两台服务器 1.Ubuntu 12.04.4 LTS (GNU/Linux 3.2.0-60-generic-pae i686)  2.Ubuntu 12.04.4 LT ...