LeetCode--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() ;
}
LeetCode--No.006 ZigZag Conversion的更多相关文章
- 【LeetCode】006. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 《LeetBook》leetcode题解(6): ZigZag Conversion[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- leetcode题解 6.ZigZag Conversion
6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...
- 【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() || ...
- 【一天一道LeetCode】#6 ZigZag Conversion
一天一道LeetCode系列 (一)题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given ...
- 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【LeetCode】6. ZigZag Conversion Z 字形变换
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字形变换,ZigZag,题解,Leetcode, 力扣,P ...
随机推荐
- [转]jquery.form.js的ajaxSubmit和ajaxForm使用
参考 http://www.cnblogs.com/popzhou/p/4338040.html 依赖的脚本文件 <script src="../Javascript/jquery-1 ...
- 7za命令报错Error: xxx is not supported archive
问题: 执行7za命令时报错:Error: xxx is not supported archive 原因: 当前7za版本过低 直接执行7za可以看到当前版本: 7-Zip (A) [64] ...
- 初学c# -- c#创建开机自启服调用外部交互式exe文件
在c#创建的开机自启动服务里,调用外部可执行文件有以下问题:1.带窗口的交互式的exe文件调用后,实际并没有被执行:2.服务是随windows启动的,服务启动后可能windows桌面还没出来,会报错误 ...
- Java中CountDownLatch和CyclicBarrier
Java编程思想中的例子import javax.validation.constraints.Size; import java.util.Random; import java.util.conc ...
- react组件回顶部
在挂载更新里面判断滚动条的距离(滚动条不能overflow: auto 踩坑) componentDidMount(){ window.addEventListener('scroll' , ()=& ...
- python基础(二)列表与字典
列表list-数组stus=['苹果','香蕉','橘子','红枣',111.122,]# 下标 0 1 2 3 4#下标,索引,角标#print(stus[4]) #st=[]#空list#st=l ...
- Java14-java语法基础(十三)接口
Java14-java语法基础(十三)接口 一.接口 1.接口的作用 Java出于安全性.简化程序结构的考虑,不支持多继承而仅支持单继承.然而实际问题中很多情况下仅仅依靠单继承并不能将复杂的问题描述清 ...
- 机器学习(二)--------单变量线性回归(Linear Regression with One Variable)
面积与房价 训练集 (Training Set) Size Price 2104 460 852 178 ...... m代表训练集中实例的数量x代表输入变量 ...
- ad_imh
1. stc15 ad_1.7 9600 1t #include < #include <absacc.h> #include <stdio.h> #includ ...
- 转载:C# 将引用的DLL文件放到指定的目录下
当软件引用的DLL比较多的时候,全部的DLL都放在exe同目录下,显得比较乱,如果能把dll放到响应的文件夹下面,就方便很多 下面是解决该问题的一种方法: 右键点击项目:属性->设置,项目会生成 ...