[leetcode]6. ZigZag Conversion字符串Z形排列
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 s, int numRows);
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation: P I N
A L S I G
Y A H R
P I
Solution1:
Step1: new StringBuilder[]. For each row, allocate a new StringBuilder to save characters in such row.
Pay attention! StringBuilder is treated like variable-length arrays. So StringBuilder[] is like array of array

Step2: we can observe that for 1st and last row, chars will be added into sb[0] and sb[numRows-1], seperately.
for sloping slide, chars will be added in sb[numRows -2 ] ... sb[1]

Step3: convert each row's StringBuilder into result String

code:
/*
Time Complexity: O(n)
Space Complexity: O(n)
*/ class Solution {
public String convert(String s, int numRows) {
char[] c = s.toCharArray();
int len = c.length;
StringBuilder[] sb = new StringBuilder[numRows];
// 要按row来进行遍历,每一个row先allocate一个StringBuilder
for (int i = 0; i < sb.length; i++) {
sb[i] = new StringBuilder();
}
int idx = 0; //用来扫String s
while (idx < len) {
for (int i = 0; i < numRows && idx < len; i++) {
sb[i].append(c[idx++]);
}
// sloping side
for (int i = numRows - 2; i >= 1 && idx < len; i--) {
sb[i].append(c[idx++]);
} }
//从sb[0]开始,将sb[1], sb[2], sb[3]... append到一个StringBuilder
for (int i = 1; i < sb.length; i++) {
sb[0].append(sb[i]);
}
return sb[0].toString();
}
}
[leetcode]6. ZigZag Conversion字符串Z形排列的更多相关文章
- LeetCode 6. ZigZag Conversion & 字符串
ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...
- Leetcode 6 ZigZag Conversion 字符串处理
题意:将字符串排成Z字形. PAHNAPLSIIGYIR 如果是5的话,是这样排的 P I AP YR H L G N SI A I 于是,少年少女们,自己去找规律吧 提示:每个Z ...
- leetcode:ZigZag Conversion 曲线转换
Question: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...
- 【LeetCode】ZigZag Conversion(Z 字形变换)
这道题是LeetCode里的第6道题. 题目要求: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ...
- leetcode题解||ZigZag Conversion问题
problem: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of r ...
- LeetCode(60)-ZigZag Conversion
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- [LeetCode] 6. ZigZag Conversion (Medium)
原题链接 把字符串按照 ↓↗↓……的顺序,排列成一个 Z 形,返回 从左到右,按行读得的字符串. 思路: 建立一个二维数组来按行保存字符串. 按照 ↓↗↓……的方向进行对每一行加入字符. 太慢了这个解 ...
- Java [leetcode 6] ZigZag Conversion
问题描述: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
- LeetCode题解——ZigZag Conversion
题目: 把一个字符串按照Z型排列后打印出来,例如 "PAYPALISHIRING" 重新排列后为3行,即 P A H N A P L S I I G Y I R 那么输出为&quo ...
随机推荐
- oracle-rman-3
http://blog.csdn.net/leshami/article/details/6032525 rman概述及体系结构 http://blog.itpub.net/23513800/view ...
- Linux(CentOS 7.0)安装Oracle11g R2
// 注释 # root用户 $oracle用户 1. 关闭安全措施 # chkconfig iptables off // 永久关闭防火墙 # serviceiptables stop // ...
- 如何在同一主机中添加多个homestead并行开发
参考源 https://blog.csdn.net/smm188/article/details/79356150 1,在项目目录 git clone homestead 后(见上面流程中的第四步), ...
- oracle user_tables没有新创建的表的问题
oracle 新创建表后,在user_tables没有,在user_tab_columns也没有,暂时未找到办法
- 使用docker compose编排容器
一.安装docker compose 二进制包安装 1.安装 Docker Compose 从 官方 GitHub Release 处直接下载编译好的二进制文件即可 # curl -L https:/ ...
- DBus send byte array over gdbus ----Send dbus data
遇到一个问题,如何通过dbus传送uint8数组元素 有3种方法, 1.直接传 ay 2.传 a(y) 3.xml定义为 ay,但是通过annotation 强行将 guchar 转为GVarian ...
- 学习MeteoInfo二次开发教程(八)
总体没什么问题. 1.创建Projection菜单,Lambert,Geographic,ShowLatLon子菜单. 2.需要添加: using MeteoInfoC.Projections; 3. ...
- C语言数据结构基础学习笔记——C语言基础
抽象数据类型(ADT)是指一个数学模型以及定义在该模型上的一组操作,通常用(数据对象,数据关系,基本操作集)这样的三元组来表示抽象数据类型. 数据结构是相互之间存在一种或多种特定关系的数据元素的集合, ...
- linux服务samba与ftp篇
samba复习: 1.下载samba:yum -y install samba 2.打开配置文件/etc/samba/smb.conf输入: [共享文件名] path = 目录名 (事先创建) pub ...
- k8s学习笔记之六:Pod控制器(kube-controller-manager)
第一章.什么是kube-controller-manager? Controller Manager 由 kube-controller-manager 和 cloud-controller-mana ...