Leetcode6--->Zigzag Conversion(将给定字符串按照Z字排列,输出结果)
题目:给定一个字符串s,一个整数numRows, 将字符串s按照竖Z的方式排列,然后输出结果;
举例:String s = "PAYPALISHIRING";
排列后为:
P A H N
A P L S I I G
Y I R
String convert(String s, int 3) 结果为"PAHNAPLSIIGYIR"
解题思路:
1. 由于每一行到底有多少个字母是不确定的,因此每行使用ArrayList来保存字母;如5,6,7行代码所示;
2. 遍历字符串:分为三个部分:第一部分(第13行代码)循环,即从上往下走;第二部分条件判断(第18行代码),判断从上往下走是否已经到达最下层,若到达,下一步应该往上走,因此ArrayList的标号应该减2;第三部分循环(第21行代码),是从下往上走;
3. 遍历结束时,list[0]中保存的是第一行的字符串,list[1] 中保存的是第二行字符串,以此类推......
代码如下:
public class Solution {
public String convert(String s, int numRows) {
if(s == null || s.length() < 1 || numRows <= 1)
return s;
List<List> list = new ArrayList<List>();
for(int i = 0; i < numRows; i++){
list.add(new ArrayList<Character>());
}
int index = 0; // 判断是第几个ArrayList,即是第几行
int i = 0;
int len = s.length();
while(i < len) {
while(index < numRows && i < len){
list.get(index).add(s.charAt(i));
index ++;
i++;
}
if(index == numRows){
index -= 2;
}
while(index > 0 && i < len){
list.get(index).add(s.charAt(i));
i++;
index --;
}
}
StringBuffer sb = new StringBuffer();
for(int j =0; j < list.size(); j++){
for(int k = 0; k < list.get(j).size(); k++){
sb.append(list.get(j).get(k));
}
}
return sb.toString();
}
}
Leetcode6--->Zigzag Conversion(将给定字符串按照Z字排列,输出结果)的更多相关文章
- LeetCode ZigZag Conversion(将字符串排成z字型)
class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...
- LeetCode OJ:ZigZag Conversion(字符串的Z字型转换)
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- leetcode6:Zigzag Conversion@Python
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode6 ZigZag Conversion
题意: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- 【LeetCode】ZigZag Conversion(Z 字形变换)
这道题是LeetCode里的第6道题. 题目要求: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ...
- 6. ZigZag Conversion - LeetCode
Question 6. ZigZag Conversion Solution 题目大意:将字符串按Z字型排列,然后再一行一行按字符输出 思路:按题目中的第一个例子,画出如下图,通过n的不同值,可以找出 ...
- LeetCode题解——ZigZag Conversion
题目: 把一个字符串按照Z型排列后打印出来,例如 "PAYPALISHIRING" 重新排列后为3行,即 P A H N A P L S I I G Y I R 那么输出为&quo ...
- [Swift]LeetCode6. Z字形变换 | ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
随机推荐
- restframework安装及APIView分析
一.restframework的安装 方式一:pip3 install djangorestframework 方式二:pycharm图形化界面安装 方式三:pycharm命令行下安装(装在当前工程所 ...
- jquery 一键复制到剪切板
今天做项目有一个功能,通过点击事件复制一段文本到剪切板,在网上找了一些,整理了一下,方便需要的朋友使用. <a id="copy" data-text="12345 ...
- 四道java语言练习基础题:
一.==符的使 首先看一段比较有意思的代码 Integer a = 1000,b=1000; Integer c = 100,d=100; public void mRun(final String ...
- [选择排序] 时间复杂度O(n^2)
思路:从未排序的序列中,找到最小的元素,放到序列的起始位置, 再从剩下没排序的里面,找到最小的,放到已经排序的末尾. 原地操作几乎是选择排序的唯一优点,当空间复杂度要求较高时,可以考虑选择排序:实际适 ...
- SharePoint 2016 如何修改Library 地址
Scenario #1 如何为一个Library 修改下访问 网络路径地址 1.点击library,点开open with explorer,使用Windows资源管理器打开文档库 2.在文件夹层次结 ...
- 如何清除SharePoint Server 配置缓存
日常运维中您可能需要清除SharePoint 2010/2013/2016中的过期配置缓存.例如,有时计时器作业往往会卡住,并在这种情况下清除缓存.您可以手动清除SharePoint配置缓存或者使用P ...
- 2017.10.3 QBXT 模拟赛
题目链接 T1 模拟 #include <cstring> #include <cstdio> #define N 105000 int L,R; char s[N]; int ...
- rhythmbox插件开发笔记3:安装 makefile && schema && po
本篇主要讲通过makefile方式来安装rhythmbox插件的相关知识. makefile 如果makefile是什么,请自行谷歌 参考了pandasunny同学的rhythmbox-baidu-m ...
- 用函数求lnx,lgx等
https://blog.csdn.net/liujian20150808/article/details/50628061
- Python 继承实现的原理(继承顺序)
继承顺序 Python3 : 新式类的查找顺序:广度优先 新式类的继承: class A(object): Python2 3 都是了 MRO算法--生成一个列表保存继承顺序表 不找到底部 Pytho ...