[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 ...
随机推荐
- windows添加永久静态路由
添加路由最好在命令行管理员模式下操作 添加临时路由命令,重启后失效 route add 172.16.1.0 mask 255.255.255.0 10.0.10.19 其中,172.16.1.0 是 ...
- openstack--9--深入理解虚拟机
登录计算节点查看进程 [root@linux-node2 ~]# ps aux | grep kvm root 824 0.0 0.0 0 0 ? S< 10:19 0:00 [kvm-irqf ...
- etcd和redis的比较和日常使用场景
转自https://blog.csdn.net/weixin_41571449/article/details/79429511 个人观点:etcd的红火来源于kurbernetes用etcd做服务发 ...
- a small notepad++ plugin to support doxygen 1key generate
Precondition: doxygen in c:\ folder testdoxygen.bat --- path %path%;C:\Doxygen;C:\Doxygen\graphviz\b ...
- 互联网同步yum服务器,中科大 rsync createrepo
参考文章 https://blog.csdn.net/chenjia6605/article/details/82734945 1.本机安装所需工具: yum -y install rsync cre ...
- centos查看系统版本信息
1.查看版本文件名称 ll /etc/*centos* 2.显示系统版本号 cat /etc/centos-release
- NumPy-快速处理数据--ndarray对象--数组的创建和存取
本文摘自<用Python做科学计算>,版权归原作者所有. NumPy为Python提供了快速的多维数组处理的能力,而SciPy则在NumPy基础上添加了众多的科学计算所需的各种工具包,有了 ...
- 使用phpAnalysis打造PHP应用非侵入式性能分析器
使用phpAnalysis打造PHP应用非侵入式性能分析器,查找PHP性能瓶颈. 什么是phpAnalysis phpAnalysis是一款轻量级非侵入式PHP应用性能分析器,适用于开发.测试及生产环 ...
- day2模块初识别
sys_mod.py import sys print(sys.argv) #['C:/Users/Administrator/desktop/s17/day2/sys_mod.py'] 打印脚本的绝 ...
- 《Celeste》 开发者是如何精心制作“冲刺”的
转自:http://www.gameres.com/804804.html 简介与序曲 在Celesete里,许多细微的行动都是发生在转瞬之间的,甚至往往比你想象中还要“转瞬之间”. 这里是 [游戏机 ...