蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目
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]的更多相关文章
- 蜗牛慢慢爬 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 ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
- 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]
题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...
- 蜗牛慢慢爬 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 ...
- 蜗牛慢慢爬 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 ...
- 蜗牛慢慢爬 LeetCode 8. String to Integer (atoi) [Difficulty: Medium]
题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cas ...
随机推荐
- SFTP Using Chilkat Active component
https://www.example-code.com/vb6/sftp_uploadBandwidthThrottle.asp Private Sub Command1_Click() ' Imp ...
- 2017-2018-2 20155315《网络对抗技术》Exp1:PC平台逆向破解
实验目的 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getShe ...
- 学习笔记:启动对特定用户的会话的sql跟踪
学习 Oracle性能诊断艺术 制作一个role: CREATE ROLE sql_trace; 然后再作一个数据库登录级别的触发器: CREATE OR REPLACE TRIGGER enable ...
- node.js学习笔记(一)——创建第一个应用
巧妇难为无米之炊.要学习node.js,当然必须先有node.js环境(可以去官网 http://nodejs.cn/ 下载安装),如果还是不懂怎么配置开发环境,度娘会告诉你一切. 安装完成环境之后, ...
- 电商打折套路分析 —— Python数据分析练习
电商打折套路分析 ——2016天猫双十一美妆数据分析 数据简介 此次分析的数据来自于城市数据团对2016年双11天猫数据的采集和整理,原始数据为.xlsx格式 包括update_time/id/tit ...
- 六边形地图Cube coordinates理解
1.这个是 Axial coordinates,可以实现六边形4个方向上的移动 2.但是六边形还有两个方向需要移动,所以引入了Cube coordinates,这个坐标系多了一个轴向,Y轴,X轴沿水平 ...
- 编码 解码 python
之前一直对python文件中编码解码糊里糊涂,今天看到一篇文章,觉得把我讲的有点明白了.写个心得吧. 1.编码解码是怎么一回事? Python 里面的编码和解码也就是 unicode 和 str 这两 ...
- Python机器学习库SKLearn:数据集转换之管道和特征
转载自:https://blog.csdn.net/cheng9981/article/details/61918129 4.1 管道和特征:组合估计量 4.1.1 管道:链接估计 管道可以用于将多个 ...
- Python文本文件的输入输出操作学习
Python具有基本的文本文件读写功能.Python的标准库提供有更丰富的读写功能. 文本文件的读写主要通过open()所构建的文件对象来实现. 创建文件对象 我们打开一个文件,并使用一个对象来表示该 ...
- 转载----C/C++ 中 const 修饰符用法总结
感谢原创作者,写的好详细.不忍错过,所以转载过来了... 原文地址: https://www.cnblogs.com/icemoon1987/p/3320326.html 在这篇文章中,我总结了一些C ...