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 ...
随机推荐
- 多线程同步 wait notify
package test; public class Test implements Runnable{ public static int j =0; @Override public void r ...
- 开发高峰时的CPU使用率
- 基于List集合映射
1. 实体类中使用List集合 public class Grade { private int id; private String name; private List<Student> ...
- Spark参数配置说明
1 修改$SPARK_HOME/conf目录下的spark-defaults.conf文件 添加以下配置项 spark.sql.hive.convertMetastoreParquet ...
- 【转】select和epoll模型的差异
http://www.cppblog.com/converse/archive/2008/10/12/63836.html epoll为什么这么快 epoll是多路复用IO(I/O Multiplex ...
- JAVA 继承 extends
/* 继承 1.提高了代码的复用性,简化了代码 2.让类与类之间产生了继承关系,才有了后面的多态的特性的存在 注意:千万不要为了获取其它类的功能简化代码,而建立继承关系, 必须要类与类之间存在继承关系 ...
- Android学习笔记02
1.线性布局LinearLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...
- mongodb 查询使用
> db.jd_58tc_raw.findOne() { "_id" : "2659e4e4caf0504ec4362478e2ed57ca", &quo ...
- CentOS 安装 Chrome
cd /etc/yum.repos.d/ vi google.repo [gogle] name=Google-x86_64 baseurl=http://dl.google.com/linux/ ...
- C++学习22 多态的概念及前提条件
在<C++基类和派生类的赋值>一节中讲到,基类的指针也可以指向派生类对象.请看下面的例子: #include <iostream> using namespace std; c ...