No.006 ZigZag Conversion
6. ZigZag Conversion
- Total Accepted: 98584
- Total Submissions: 398018
- Difficulty: Easy
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);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
- 显然,n = 1 时,转换之后的字符串最后就是原先的字符串了
- 当n > 1时
- 那么第 0 行,没有重复,所有字符的位置index就是0,0+period,0+2*period。。。即每个周期内添加一个字符
- 对于第 1 行,显然有重复,所有字符的位置index就是(1,0 +period - 1 ),(1+period,0+2*period-1),(1+2*peroid,0+3*period-1).。。。即每个周期内添加两个字符
- 。。。
- 对于第 i 行,显然有重复,所有字符的位置index就是(i,0 +period - i ),(i+period,0+2*period-i),(i+2*peroid,0+3*period-i).。。。即每个周期内添加两个字符
- 。。。
- 对于第 (n-1) 行,也就是最后一行,没有重复,所有字符的位置index就是(n-1), (n-1)+ period , (n-1)+2* period 。。。即每个周期内添加一个字符
- 对于第0行和最后一行有一个特点就是 (period-i)%peroid == i
所以,代码如下:
public String convert(String s, int numRows) {
if(s == null || s.length() <= numRows || numRows == 1){
return s ;
}
StringBuilder res = new StringBuilder() ;
char [] arr = s.toCharArray() ;
int period = 2*numRows - 2 ;
for(int i = 0 ; i < numRows ; i++){
for(int j = i ; j < s.length(); j += period){
if((period-i)%period != i){
res.append(arr[j]) ;
if((j+period-2*i) < s.length()){
res.append(arr[j+period-2*i]) ;
}
}else{
res.append(arr[j]) ;
}
}
}
return res.toString() ;
}
No.006 ZigZag Conversion的更多相关文章
- LeetCode--No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- 【LeetCode】006. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【JAVA、C++】LeetCode 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- [Leetcode]006. ZigZag Conversion
public class Solution { public String convert(String s, int nRows) { if (s == null || s.isEmpty() || ...
- 《LeetBook》leetcode题解(6): ZigZag Conversion[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
- 64. ZigZag Conversion
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- leetcode第六题 ZigZag Conversion (java)
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
随机推荐
- [dts]DTS实例分析
此篇源文件arch/arm/boot/dts/imx6sx.dtsi 1. dts和dtsi完成的功能 以下是两段较为常见的dtsi和dts代码 uart5: serial@021f4000 { co ...
- 黄聪:利用Aspose.Word控件实现Word文档的操作(转)
撰写人:伍华聪 http://www.iqidi.com Aspose系列的控件,功能都挺好,之前一直在我的Winform开发框架中用Aspose.Cell来做报表输出,可以实现多样化的报表设计及 ...
- jquery.cookie.js存与取以及过期时间设置
$(function(){ $(".active_out .abtn").click(function(){ $(this).parents(".active_out&q ...
- 一个原生的JavaScript拖动方法
代码: 1 function drag(t,p){ 2 3 var point = p || null, 4 target = t || null, 5 resultX = 0, 6 resultY ...
- [Bug-IOS] - linker command failed with exit code 1 (use -v to see invocation)
Ld /Users/Rubert/Library/Developer/Xcode/DerivedData/OC_Language-emftyzftyvhdpuaxipddjmpnpvox/Build/ ...
- Google glog 使用
Google glog 使用 1 简介 Googleglog 库实现了应用级的日志记录,提供了C++ 风格的流操作和各种助手宏. 代码示例: #include <glog/logg ...
- 关于Cannot assign to 'self' outside of a method in the init family解决方法
有时候我们重写父类的init方法时不注意将init后面的第一个字母写成了小写,在这个方法里面又调用父类的初始化方法(self = [super init];)时会报错,错误信息如下:error:Can ...
- 网页地图map
<map name="map"> <area shape="rect" coords="75,75,99,99" nohr ...
- python3-cookbook
http://python3-cookbook.readthedocs.io/zh_CN/latest/index.html 一般的类找方法,通过MRO找到第一个就停了对吧,可以描述器好像会顺着MRO ...
- Java多线程之join
1.join方法只有在继承了Thread类的线程中才有. 2.线程必须要start() 后再join才能起作用. 将另外一个线程join到当前线程,则需要等到join进来的线程执行完才会继续执行当前线 ...