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 ...
随机推荐
- js----作用域链
作用域链是javascript的一个难点,要了解它就要了解作用域.变量.执行环境.生命周期等. 下面是找的资料加总结,加深理解. 作用域 变量的作用域可分为 A:全局作用域----最外层函数定义的变量 ...
- oralce 中union 和union all 的简单使用说明
union:对两个结果集进行合并操作:不包括重复行:同时进行默认规则的排序: union all:对两个结果集进行合并操作:包括重复行:不排序:
- c# devexpress 多文档界面
学习记录 https://blog.csdn.net/qq_25473787/article/details/81208894?utm_source=blogxgwz0
- 360. Sort Transformed Array二元一次方程返回大数序列
[抄题]: Given a sorted array of integers nums and integer values a, b and c. Apply a quadratic functio ...
- loongnix社区
http://www.loongnix.org/index.php/%E9%A6%96%E9%A1%B5
- .net like模糊查询参数化
List<SqlParameter> paras = new List<SqlParameter>(); if (!string.IsNullOrEmpty(ciName)) ...
- sql存储过程进行条件筛选
1.创建临时表,把存储过程结果集保存到临时表,对临时表进行筛选. Create Table #TmpTable(FieldList) Insert Into #TmpTable Exec StoreP ...
- 4412 uboot上手
1,了解 print 查看UBOOT软件的环境变量 (变量名=变量) setenv.saveenv setenv abc 100 200 设置 添加一个变量值 修改一个已有的变量 ...
- 201621123002《JAVA程序设计》第三章学习总结
1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词,如类.对象.封装等 关键词:类 对象 封装 构造函数 this,static,final 1.2 用思维导图或者Onenote或 ...
- HTML中调用JavaScript的几种情况和规范写法
JavaScript执行在html中,引用有几种方式? 我知道的方法有3种: 第一种:外部引用远程JavaScript文件.如<script type="text/javascript ...