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 ...
随机推荐
- javascript中两种基本常用排序算法分析
备注:内容大部分从网上复制,代码为自己手写.仅做知识的温故知新,并非原创. 1.冒泡排序(Bubble Sort) (1)算法描述 冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两 ...
- ComboBox控件“设置 DataSource 属性后无法修改项集合”的解决【转】
编写Winform程序,遇到comboBox的绑定事件和索引项变更事件的冲突问题,就是“设置 DataSource 属性后无法修改项集合”的错误问题,网上查了很多,大多说在索引项变更是进行非空判断,还 ...
- 使用com.sun.imageio.plugins.png.PNGMetadata读取图片的元数据
所谓图片元数据,就是除了我们肉眼看到的图片内容外,隐藏在这些内容背后的一些技术数据. 本文介绍如何使用Java代码将一张图片的隐藏信息读取出来. 首先不需要下载任何额外的Java库,用JDK自带的库就 ...
- 如何将S/4HANA系统存储的图片文件用Java程序保存到本地
我在S/4HANA的事务码MM02里为Material维护图片文件作为附件: 通过如下简单的ABAP代码即可将图片文件的二进制内容读取出来: REPORT zgos_api. DATA ls_appl ...
- UVA 1442 Cave 洞穴 (贪心+扫描)
题意:有一个洞穴,每个位置有一个底的高度p[i],和对应顶的高度s[i],要往里面尽量放燃料,要求燃料不能碰到顶,可以无限接近. 题解:制约燃料储放的就是顶的高度了,分别求出设当前储放位置的向两边的延 ...
- Android(java)学习笔记125:保存数据到SD卡 (附加:保存数据到内存)
1. 如果我们要想读写数据到SD卡中,首先必须知道SD的路径: File file = new File(Environment.getExternalStorageDirectory()," ...
- coreData-Fetching Managed Objects
https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CoreDataSnippets ...
- 解决在matplotlib使用中文的问题
原生的matplotlib并不支持直接使用中文,而需要修改一下相应的文件,上网搜了下,找到一个最简洁的办法. NO.1 找到matplotlibrc文件 C:\Python26\Lib\site-pa ...
- 括号匹配性检测C语言实现
#include <stdio.h> #define SIMPLE_KUOHAO "(()1231qeqw)(@#$)" #define COMPLEX_KUOHAO ...
- 【Git版本控制】idea中使用git进行项目管理
转载博文:完整教程-idea使用git进行项目管理(总结版)