题目

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 text, int nRows);

翻译

此题坑爹在于关于zigzag的形式没有说清楚 然后在leetcode上负分较多

其实只要理解了zigzag的形式就好了

/*n=numRows
Δ=2n-2 1 2n-1 4n-3
Δ= 2 2n-2 2n 4n-4 4n-2
Δ= 3 2n-3 2n+1 4n-5 .
Δ= . . . . .
Δ= . n+2 . 3n .
Δ= n-1 n+1 3n-3 3n-1 5n-5
Δ=2n-2 n 3n-2 5n-4
*/

Hints

Related Topics: String

一开始我的想法是用二维数组在遍历的时候把字符存到对应的位置 之后再join到一个字符串里面 这样挺直观的 但是python的话慢了一点

事实上我的代码有点硬推出每个字符的位置填上去的意思(囧) discuss中类似状态机改变index变化步长的方法显然更好

代码

Java

public String convert(String s, int nRows) {
char[] c = s.toCharArray();
int len = c.length;
StringBuffer[] ss = new StringBuffer[nRows];
for (int i = 0; i < ss.length; i++) ss[i] = new StringBuffer(); int i = 0;
while (i < len) {
for (int idx = 0; idx < nRows && i < len; idx++)
ss[idx].append(c[i++]);
for (int idx = nRows-2; idx >= 1 && i < len; idx--)
ss[idx].append(c[i++]);
}
for (int idx = 1; idx < ss.length; idx++)
ss[0].append(ss[idx]);
return ss[0].toString();
}

Python

class Solution(object):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
l = len(s)
if l<=1 or numRows<=1: return s
k = numRows - 2
n = l/(numRows+k)+1
n = n*(k+1) zigzag = [['']*n for i in range(numRows)]
for i in range(l):
c = (2*numRows-2)*(i/(2*numRows-2))
if (i-c)/numRows==0:
zigzag[((i-c)%numRows)][(i/(2*numRows-2))*(k+1)] = s[i]
else:
zigzag[numRows-2-(i-c)%numRows][(i/(2*numRows-2))*(k+1)+((i-c)%numRows+1)] = s[i]
ss =''
for i in range(numRows):
ss += ''.join(zigzag[i]) return ss #better solution
class Solution(object):
def convert(self, s, numRows):
if numRows==1or numRows>=len(s):
return s
L = ['']*numRows
index, step = 0, 1 for i in s:
L[index] += i
if index==0:
step = 1
elif index==numRows-1:
step = -1
index += step
return ''.join(L)

蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]的更多相关文章

  1. 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]

    题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  2. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  3. 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

    题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  5. 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]

    题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

  6. 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]

    题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...

  7. 蜗牛慢慢爬 LeetCode 1.Two Sum [Difficulty: Easy]

    题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...

  8. 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]

    题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  9. 蜗牛慢慢爬 LeetCode 8. String to Integer (atoi) [Difficulty: Medium]

    题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cas ...

随机推荐

  1. Facebook如何管理150亿张照片

    Facebook 的照片分享很受欢迎,迄今,Facebook 用户已经上传了150亿张照片,加上缩略图,总容量超过1.5PB,而每周新增的照片为2亿2000万张,约25TB,高峰期,Facebook ...

  2. 2017-2018-2 20155315《网络对抗技术》Exp3:免杀原理与实践

    实验目的 通过本部分内容的学习,认识到杀软局限性,提高在工作生活中对于恶意软件防范能力. 教程 实验内容 使用msf编码器,msfvenom,veil-evasion,shellcode编程等免杀工具 ...

  3. 【BZOJ1041】[HAOI2008]圆上的整点

    [BZOJ1041][HAOI2008]圆上的整点 题面 bzoj 洛谷 题解 不妨设\(x>0,y>0\) \[ x^2+y^2=r^2\\ y^2=(x+r)(x-r) \] 设\(r ...

  4. springboot之cas客户端

    一.CAS Client 与受保护的客户端应用部署在一起,以 Filter 方式保护受保护的资源.对于访问受保护资源的每个 Web 请求,CAS Client 会分析该请求的 Http 请求中是否包含 ...

  5. SpringMVC 异常信息ASM ClassReader failed to parse class file的问题解决

    1.  环境信息: Spring 3.2.0,  JDK 1.8.0 2.  运行简单的程序,出现以下错误信息: 2.  运行简单的程序,出现以下错误信息: Caused by: org.spring ...

  6. 聊聊Http协议

    http协议是大家在互联网中最为熟悉的协议,只要上网大家都会遇到,但是,很多人被问道什么是http协议,http协议的内容是什么就懵了.这里,我们随便聊聊http协议. 首先,我们说说协议.我一直觉得 ...

  7. svn图文教程-宋正河整理

    下载地址:http://download.csdn.net/download/songzhengdong82/4433476 在线浏览:http://wenku.baidu.com/view/07f1 ...

  8. Kubernetes服务发现之Service详解

    一.引子 Kubernetes Pod 是有生命周期的,它们可以被创建,也可以被销毁,然后一旦被销毁生命就永远结束.通过ReplicationController 能够动态地创建和销毁Pod(列如,需 ...

  9. ubuntu/linux中安装Tomcat(附图解详细步骤)

    我的linux系统使用的是ubuntu14 1.首先需要先到Tomcat官网上下载对应linux系统的压缩包,可以直接在Ubuntu系统中进行下载,下载后的默认路径为主文件夹路径下的下载文件目录下 注 ...

  10. CentOS7安装VMware Tools

    安装依赖包 [root@localhost ~]# yum -y install perl gcc gcc-c++ make cmake kernel kernel-headers kernel-de ...