蜗牛慢慢爬 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 ...
随机推荐
- 补交20145226蓝墨云班课 -- Arrays和String单元测试
蓝墨云班课 -- Arrays和String单元测试 具体描述: 在IDEA中以TDD的方式对String类和Arrays类进行学习 测试相关方法的正常,错误和边界情况 String类 charAt ...
- Noip前的大抱佛脚----赛前任务
赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...
- 15-[JavaScript]-ECMAScript 1
0.javaScript的发展历程 https://zhuanlan.zhihu.com/p/27985124 1.javaScript是什么? javaScript是一种web前端的描述语言,也是一 ...
- [SCOI2010]传送带 三分法
[SCOI2010]传送带 LG传送门 三分法模板. 关于为什么可以三分,我选择感性理解,有人证明了,总之我是懒得证了. 假设路径是\(A \to E \to F \to D\),\(E\)和\(F\ ...
- Storm 第四章 Storm常见问题
1.集群如何启动,任务如何执行? java -server nimbus,supervisor client--->createTopology(序列化)--->提交jar到nimbusi ...
- directive指令二 require:'^ngModel'
本章主要是讲指令与ngModel的交互. 在angular有一个内置指令叫ngModel,它是angular用来处理表单的最重要的指令.在源码中,页面上的model值的格式化.解析.验证都是由ngMo ...
- Flutter - 下载别人的Flutter项目,本地编译不过
如果直接下载了别人的Flutter项目,点击运行基本会不通过的,这是gradle版本差异造成的. 你需要修改android/gradle/wrapper/gradle-wrapper.properti ...
- UWP Xaml设计器中输入特殊字符
<TextBox Text="欢迎使用小冰科技最新研发的自然语言处理程序.小冰科技旗下还有强大的人脸识别软件——<微识别>,自动追踪和识别人脸:具有科普性质的.清新脱俗的识 ...
- javascript中的toString()方法
javascript中的toString()方法,主要用于Array.Boolean.Date.Error.Function.Number等对象.下面是这些方法的一些解析和简单应用,做个纪律,以作备忘 ...
- .net mvc中session的锁机制
今天遇到个奇怪的问题, 一个秒杀商品系统, 大量秒杀请求进来, 到了action居然是单线程执行! 这样产生的效果就是“这个系统好慢啊!!”. 可是我没有加lock,为什么会变成单线程执行呢? 找资料 ...