题目描述
将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。

比如输入字符串为 “LEETCODEISHIRING” 行数为 3 时,排列如下:

L C I R
E T O E S I I G
E D H N
1
2
3
之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:“LCIRETOESIIGEDHN”。

请你实现这个将字符串进行指定行数变换的函数:

string convert(string s, int numRows);
1
示例1
输入: s = "LEETCODEISHIRING", numRows = 3
输出: "LCIRETOESIIGEDHN"
1
2
示例2
输入: s = "LEETCODEISHIRING", numRows = 4
输出: "LDREOEIIECIHNTSG"
解释:

L D R
E O E I I
E C I H N
T S G
1
2
3
4
5
6
7
8
例题解法:
因为我自己写的将近 100 行代码,实在不能看,所以就去看了解析,下面是解析的解法。
由题我们可知一般情况下输出的行数就是传入的 numRows,而当传入的字符串字符数小于 numRows 时,行数即为该字符串所包含的字符数,所以直接用 n(行数)个 StringBuilder 来存储每行的字符,最后再将它们连接到一起即我们要输出的结果。(真是巧妙啊这个方法)
尤其要注意当字符数少于需要输出的行数这种情况。

class Solution {
public String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
ArrayList<StringBuilder> arrs = new ArrayList<>();
for (int i = 0; i < Math.min(numRows, s.length()); i++) {
arrs.add(new StringBuilder());
}
boolean direction = true;
int cur_row = 0;
for (int i = 0; i < s.length(); i++) {
arrs.get(cur_row).append(s.charAt(i));
cur_row = cur_row + (direction ? 1 : -1);
if (cur_row == 0 || cur_row == numRows - 1) direction = !direction;
}
StringBuilder result = new StringBuilder();
for (int i = 0; i < arrs.size(); i++) {
result.append(arrs.get(i));
}
return result.toString();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
自己的憨憨解法
是真的菜,还想先去计算了数组大小,然后再数组一个一个添加,实属憨憨写法,只有我自己能看懂。

class Solution {
public String convert(String s, int numRows) {
int n = numRows;
String result = "";
int len = s.length();
int column = 0;
if (n == 1) {
column = len;
result += s;
} else if (n == 2) {
column = len/2 + len%2;
for (int i=0, j=0; i < column; i++, j+=2) {
result += s.charAt(j);
}
if (len%2 == 1) {
for (int i=0, j=1; i < column-1; i++, j+=2) {
result += s.charAt(j);
}
} else {
for (int i=0, j=1; i < column; i++, j+=2) {
result += s.charAt(j);
}
}
} else {
int length = len;
boolean a = true;
int l_str = 0, s_str = 0;
while (length != 0) {
if (a) {
l_str++;
if (l_str == n) {
a = false;
l_str = 0;
column++;
}
} else {
s_str++;
if (s_str == (n-2)) {
a = true;
s_str = 0;
}
column++;
}
length--;
}
if (l_str>0 && l_str<n) {
column++;
}
char[][] z = new char[n][column];
for (int m = 0; m < n; m++) {
for (int k =0; k < column; k++) {
z[m][k] = 32;
}
}
ArrayList<Character> arr = new ArrayList<>();
for (int i = 0; i < len; i++) {
arr.add(s.charAt(i));
}

int long_str_count = 0; //max is n
int single_char_count = 0; //max is 3n-2
boolean flag = true;
int i = 0, j = 0;
while (true) {
if (flag) {
z[i][j] = arr.get(0);
arr.remove(0);
long_str_count++;
if (arr.isEmpty()) {
break;
}
if (long_str_count==n) {
flag = false;
long_str_count = 0;
i--;
j++;
} else {
i++;
}
} else {
z[i][j] = arr.get(http://www.my516.com);
arr.remove(0);
single_char_count++;
if (arr.isEmpty()) {
break;
}
if (single_char_count == (n-2)) {
flag = true;
single_char_count = 0;
} else {
}
j++;
i--;
}
}
for (int m = 0; m < n; m++) {
for (int k =0; k < column; k++) {
if (z[m][k] != 32) {
result += z[m][k];
}
}
}
}
return result;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
获得知识
StringBuilder 比 String 执行更快,因为使用 String 进行连接时,每次都会创建一个新的对象,而 StingBuilder 不会。
---------------------

6——Z 字形变换(ZigZag Conversion)的更多相关文章

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

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

  2. C#版[击败100.00%的提交] - Leetcode 6. Z字形变换 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  3. Z 字形变换 C++实现 java实现 leetcode系列(六)

    Z 字形变换  java实现 C++实现  将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 ...

  4. Leetcode题库——6.Z字形变换

    @author: ZZQ @software: PyCharm @file: convert.py @time: 2018/9/20 20:12 要求: Z字形变换 将字符串 "PAYPAL ...

  5. LeetCode Golang 6. Z 字形变换

    6. Z 字形变换 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L ...

  6. Leetcode(6)Z字形变换

    Leetcode(6)Z字形变换 [题目表述]: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ...

  7. Java实现 LeetCode 6 Z字形变换

    6. Z 字形变换 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L ...

  8. 【LeetCode】6. ZigZag Conversion Z 字形变换

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字形变换,ZigZag,题解,Leetcode, 力扣,P ...

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

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

随机推荐

  1. [TS-A1487][2013中国国家集训队第二次作业]分配游戏[二分]

    根据题意,设$3n$次比较中胜了$w$次,负了$l$次,平了$d$次,所有场次中胜了$W$次,负了$L$次,平了$D$次.如果一场赢了,那么$w-l$就会$+1$,相同地,$W-L$也会$+1$:如果 ...

  2. springCloud学习-服务链路追踪(Spring Cloud Sleuth)

    1.简介 Spring Cloud Sleuth 是 Spring Cloud 的一个组件,它的主要功能是在分布式系统中提供服务链路追踪的解决方案. 常见的链路追踪组件有 Google 的 Dappe ...

  3. springMVC入门笔记

    目录 一.回顾Servlet 二.SpringMVC简介 三.搭建SpringMVC第一个案例 四.简单流程及配置 五.使用注解开发Controller 六.参数绑定 基本数据类型的获取: 如果表单域 ...

  4. D. Multiplication Table 二分查找

    刚做这道题根本没想到二分,最关键是没想出来怎样统计在这个矩阵中比一个数小的有几个怎么算.造成自己想了好久最后看了别人的提示才做出来.哎.好久不做题太弱了 #include<iostream> ...

  5. [Node.js] Add Logging to a Node.js Application using Winston

    Winston is a popular logging library for NodeJS which allows you to customise the output, as well as ...

  6. Oracle数据库导出导入

    需求为将数据库A中的数据导出为*.dmp文件.然后将*.dmp文件导入到数据库B. 1.导出数据库A     在cmd窗体输入下面命令: 导出所有数据库 exp username/password@数 ...

  7. uiautomator +python 安卓UI自动化尝试

    使用方法基本说明:https://www.cnblogs.com/mliangchen/p/5114149.html,https://blog.csdn.net/Eugene_3972/article ...

  8. VC问题 IntelliSense:“没有可用的附加信息”,[请參见“C++项目 IntelliSense 疑难解答”,获得进一步的帮助]

    在XP上安装VS2010 后发现 IntelliSense不能使用,但在Windows7上是能够正常使用这功能的.关于IntelliSense不能使用的问题已有网友提出了是由于KB2876217这个补 ...

  9. selenium清空默认文字

    默认输入框 鼠标点击上去还有文案 直接用 clear不可以

  10. 从头认识java-15.2 Collection的经常用法(2)-注意点

    上一章节我们讲述了Collection的经常用法.还有之前的章节我们介绍了Collection的addAll方法,这一章节我们介绍一下它的注意点. 注意点就是,在经常用法里面,基本都是可选操作. 什么 ...