题目:给定一个字符串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. 1068 乌龟棋 2010年NOIP全国联赛提高组

    1068 乌龟棋 2010年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Descrip ...

  2. 关于验证码在IE中不刷新的快速解决方法

    今天在做验证码的时候发现在IE中,验证码不会刷新,而谷歌等其他浏览器没有问题,所以我想到应该是缓存问题,因为IE默认的设置是如果访问地址没变化就不会去获取而是加载缓存中的内容 所以解决方案就是在验证码 ...

  3. codeforce Gym 100418K Cards (概率,数学)

    题意:麦田的故事,n张牌,取x张牌,记住前x张牌最大的值m,继续往后取,遇到第一张比m大的牌就停下来.求一个x使得最后的牌在整副牌里是最大的期望最大. 假设最大的牌是A,A在各种位置出现的概率就是相等 ...

  4. [学习笔记] SSD代码笔记 + EifficientNet backbone 练习

    SSD代码笔记 + EifficientNet backbone 练习 ssd代码完全ok了,然后用最近性能和速度都非常牛的Eifficient Net做backbone设计了自己的TinySSD网络 ...

  5. C++调用C语言编译的so文件

    参考链接:https://blog.csdn.net/chenjinlong126/article/details/78990350 一.制作so文件:libadd_c.so或libadd_cpp.s ...

  6. 四种UNIX实现

    四种UNIX实现:FreeBSD 5.2.1          Linux 2.4.22           mac OS X 10.3        Solaris 9 ubuntu 属于哪一种呢?

  7. Bootstrap历练实例:默认的列表组

    Bootstrap 列表组 本章我们将讲解列表组.列表组件用于以列表形式呈现复杂的和自定义的内容.创建一个基本的列表组的步骤如下: 向元素 <ul> 添加 class .list-grou ...

  8. 01_2_模拟spring装载bean

    01_2_模拟spring装载bean 1. xml配置文件内容 beans.xml <beans> <bean id="u" class="com.w ...

  9. debian常用指令

    查看软件xxx安装内容 dpkg -L xxx 查找软件 apt-cache search 正则表达式 查找文件属于哪个包 dpkg -S filename apt-file search filen ...

  10. C语言输出多位小数

    #include<stdio.h>#include<stdlib.h>int main(){int i=0;int m=19;int n=3;int s=0;s=m/n;pri ...