Skip to content

Commit 1809fa5

Browse files
authored
Merge pull request apachecn#262 from ruanwenjun/leetcode-java
Add Java solution 6,7
2 parents 86fc4b4 + d38c020 commit 1809fa5

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# 5. ZigZag Conversion
2+
3+
**<font color=red>难度: Medium</font>**
4+
5+
## 刷题内容
6+
7+
> 原题连接
8+
9+
* https://leetcode-cn.com/problems/zigzag-conversion/description
10+
11+
> 内容描述
12+
13+
```
14+
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
15+
16+
P A H N
17+
A P L S I I G
18+
Y I R
19+
20+
And then read line by line: "PAHNAPLSIIGYIR"
21+
22+
Write the code that will take a string and make this conversion given a number of rows:
23+
24+
string convert(string s, int numRows);
25+
26+
Example 1:
27+
28+
Input: s = "PAYPALISHIRING", numRows = 3
29+
Output: "PAHNAPLSIIGYIR"
30+
31+
32+
33+
Example 2:
34+
35+
Input: s = "PAYPALISHIRING", numRows = 4
36+
Output: "PINALSIGYAHRPI"
37+
Explanation:
38+
39+
P I N
40+
A L S I G
41+
Y A H R
42+
P I
43+
44+
45+
```
46+
47+
## 解题方案
48+
49+
> 思路 1
50+
******- 时间复杂度: O(len(s))******- 空间复杂度: O(len(s))******
51+
52+
需要将字符串s转换为按N排列,总共有numRows行,直接将字符串转换为N字形,然后输出, beats 74.18%
53+
54+
```java
55+
class Solution {
56+
// 将字符串进行z子排列,行数为numRows
57+
public String convert(String s, int numRows) {
58+
// 思路:先转换为z字
59+
List[] arr = new List[numRows]; // 保存每一行元素
60+
for(int i = 0; i < numRows; i ++){
61+
arr[i] = new ArrayList();
62+
}
63+
char[] chars = s.toCharArray();
64+
for(int i = 0; i < chars.length;){
65+
// 每次打印两列
66+
for(int j = 0; j < numRows && i < chars.length; j++,i++){
67+
List list = arr[j];
68+
list.add(chars[i]);
69+
}
70+
for(int j = numRows - 1; j >= 0 && i < chars.length; j --){
71+
if(j == numRows - 1 || j == 0){
72+
arr[j].add(' ');
73+
}else{
74+
arr[j].add(chars[i]);
75+
i++;
76+
}
77+
}
78+
}
79+
// 输出最终字符串
80+
char[] result = new char[chars.length];
81+
int index = 0;
82+
for(int i = 0; i < numRows; i ++){
83+
List list = arr[i];
84+
for(int j = 0; j < list.size(); j ++){
85+
if(' ' != (char)list.get(j)){
86+
result[index++] = (char) list.get(j);
87+
}
88+
}
89+
}
90+
return new String(result);
91+
}
92+
}
93+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# 5. Reverse Integer
2+
3+
**<font color=red>难度: Easy</font>**
4+
5+
## 刷题内容
6+
7+
> 原题连接
8+
9+
* https://leetcode-cn.com/problems/reverse-integer/description
10+
11+
> 内容描述
12+
13+
```
14+
Given a 32-bit signed integer, reverse digits of an integer.
15+
16+
Example 1:
17+
18+
Input: 123
19+
Output: 321
20+
21+
Example 2:
22+
23+
Input: -123
24+
Output: -321
25+
26+
Example 3:
27+
28+
Input: 120
29+
Output: 21
30+
31+
32+
```
33+
34+
## 解题方案
35+
36+
> 思路 1
37+
******- 时间复杂度: O(n)******- 空间复杂度: O(1)******
38+
39+
将整数翻转,翻转后是否溢出了, beats 95.35%
40+
41+
```java
42+
class Solution {
43+
public int reverse(int x) {
44+
// 使用一个long型变量来保存
45+
long index = 0;
46+
while (x != 0){
47+
index = index * 10 + x %10;
48+
x = x / 10;
49+
}
50+
int result = (int) index;
51+
if(result != index){
52+
return 0;
53+
}
54+
return (int)index;
55+
}
56+
}
57+
```

0 commit comments

Comments
 (0)