题目:给定一个字符串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字排列,输出结果)的更多相关文章

  1. LeetCode ZigZag Conversion(将字符串排成z字型)

    class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...

  2. LeetCode OJ:ZigZag Conversion(字符串的Z字型转换)

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  3. leetcode6:Zigzag Conversion@Python

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  4. LeetCode6 ZigZag Conversion

    题意: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  5. 【LeetCode】ZigZag Conversion(Z 字形变换)

    这道题是LeetCode里的第6道题. 题目要求: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ...

  6. 6. ZigZag Conversion - LeetCode

    Question 6. ZigZag Conversion Solution 题目大意:将字符串按Z字型排列,然后再一行一行按字符输出 思路:按题目中的第一个例子,画出如下图,通过n的不同值,可以找出 ...

  7. LeetCode题解——ZigZag Conversion

    题目: 把一个字符串按照Z型排列后打印出来,例如 "PAYPALISHIRING" 重新排列后为3行,即 P A H N A P L S I I G Y I R 那么输出为&quo ...

  8. [Swift]LeetCode6. Z字形变换 | ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

随机推荐

  1. Bootstrap设置按钮禁用

    在Bootstrap中,按钮可以使用button标签或者a标签.设置按钮禁用可以通过两种方式,一种是通用CSS样式,一种是用过JS脚本动态设置,下面举例说明! <!DOCTYPE html> ...

  2. iOS中转义后的html标签如何还原

    最近用swift做一个公司的小项目,遇到一个问题,就是通过api获取的html文本中的标签都已经被转义了, <p class="MsoNormal" align=" ...

  3. SpringBoot LogBack文件配置

    <?xml version="1.0" encoding="UTF-8"?> <configuration scan="true&q ...

  4. spring security 2.x HttpSessionEventPublisher 以及listener配置

    在环境为spring security2.x时 *JDK6 spring 2* 正确的filter路径是:org.springframework.security.ui.session.HttpSes ...

  5. db2新添用户

    --1.新添用户  -目录 /XX/XX  -组  XX 用户名useradd -d /home/xx -g users xx--2.修改密码passwd xx--3.在QC中grant权限.新添表空 ...

  6. 在一个另一个文件中 #include一个**dlg.h文件,会发生dlg的资源ID未定义的错误 :

    1    在一个另一个文件中 #include一个**dlg.h文件,会发生dlg的资源ID未定义的错误 : dlg1.h(23) : error C2065: 'IDD_DIALOG1' : und ...

  7. 为Oracle Clusterware修改公用及私有网络接口

    出于种种原因我们可能需要为已安装的Oracle集群软件修改其使用的公用或私有网络所使用的网络接口(How to Change Interconnect/Public Interface IP or S ...

  8. CodeForces 66C Petya and File System (实现)

    模拟题,map搞一搞.要想清楚一个结点应该是要通过一个字符串找到下一个结点,题目保证所以文件夹非空,所以只要判断一个结点是不是叶子结点就可以判断它是不是文件,用了点c11的特性. #include&l ...

  9. Vmware 安装CentOS7时连不上网问题的解决

    在VmWare 上安装Centos7时,装好vmware后还是连不上网,通过查找资料原来是因为有线网卡没有激活,默认centos和redhat7都是不启用有线网卡的,要么手动开启,要么安装时直接启用! ...

  10. Bootstrap历练实例:默认的进度条

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...