Skip to content

Commit a841eac

Browse files
committed
Add solution 6、8、12
1 parent 1eac649 commit a841eac

32 files changed

+1348
-300
lines changed

README.md

+235-235
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode
2+
3+
func convert(s string, numRows int) string {
4+
matrix, down, up := make([][]byte, numRows, numRows), 0, numRows-2
5+
for i := 0; i != len(s); {
6+
if down != numRows {
7+
matrix[down] = append(matrix[down], byte(s[i]))
8+
down++
9+
i++
10+
} else if up > 0 {
11+
matrix[up] = append(matrix[up], byte(s[i]))
12+
up--
13+
i++
14+
} else {
15+
up = numRows - 2
16+
down = 0
17+
}
18+
}
19+
solution := make([]byte, 0, len(s))
20+
for _, row := range matrix {
21+
for _, item := range row {
22+
solution = append(solution, item)
23+
}
24+
}
25+
return string(solution)
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question6 struct {
9+
para6
10+
ans6
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para6 struct {
16+
s string
17+
numRows int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans6 struct {
23+
one string
24+
}
25+
26+
func Test_Problem6(t *testing.T) {
27+
28+
qs := []question6{
29+
30+
{
31+
para6{"PAYPALISHIRING", 3},
32+
ans6{"PAHNAPLSIIGYIR"},
33+
},
34+
35+
{
36+
para6{"PAYPALISHIRING", 4},
37+
ans6{"PINALSIGYAHRPI"},
38+
},
39+
40+
{
41+
para6{"A", 1},
42+
ans6{"A"},
43+
},
44+
}
45+
46+
fmt.Printf("------------------------Leetcode Problem 6------------------------\n")
47+
48+
for _, q := range qs {
49+
_, p := q.ans6, q.para6
50+
fmt.Printf("【input】:%v 【output】:%v\n", p, convert(p.s, p.numRows))
51+
}
52+
fmt.Printf("\n\n\n")
53+
}
+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# [6. ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/)
2+
3+
4+
## 题目
5+
6+
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)
7+
8+
```
9+
P A H N
10+
A P L S I I G
11+
Y I R
12+
```
13+
14+
And then read line by line: `"PAHNAPLSIIGYIR"`
15+
16+
Write the code that will take a string and make this conversion given a number of rows:
17+
18+
```
19+
string convert(string s, int numRows);
20+
```
21+
22+
**Example 1:**
23+
24+
```
25+
Input: s = "PAYPALISHIRING", numRows = 3
26+
Output: "PAHNAPLSIIGYIR"
27+
```
28+
29+
**Example 2:**
30+
31+
```
32+
Input: s = "PAYPALISHIRING", numRows = 4
33+
Output: "PINALSIGYAHRPI"
34+
Explanation:
35+
P I N
36+
A L S I G
37+
Y A H R
38+
P I
39+
```
40+
41+
**Example 3:**
42+
43+
```
44+
Input: s = "A", numRows = 1
45+
Output: "A"
46+
```
47+
48+
**Constraints:**
49+
50+
- `1 <= s.length <= 1000`
51+
- `s` consists of English letters (lower-case and upper-case), `','` and `'.'`.
52+
- `1 <= numRows <= 1000`
53+
54+
## 题目大意
55+
56+
将一个给定字符串 `s` 根据给定的行数 `numRows` ,以从上往下、从左到右进行 Z 字形排列。
57+
58+
比如输入字符串为 `"PAYPALISHIRING"` 行数为 3 时,排列如下:
59+
60+
```go
61+
P A H N
62+
A P L S I I G
63+
Y I R
64+
```
65+
66+
之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:`"PAHNAPLSIIGYIR"`
67+
68+
请你实现这个将字符串进行指定行数变换的函数:
69+
70+
```go
71+
string convert(string s, int numRows);
72+
```
73+
74+
## 解题思路
75+
76+
- 这一题没有什么算法思想,考察的是对程序控制的能力。用 2 个变量保存方向,当垂直输出的行数达到了规定的目标行数以后,需要从下往上转折到第一行,循环中控制好方向ji
77+
78+
## 代码
79+
80+
```go
81+
package leetcode
82+
83+
func convert(s string, numRows int) string {
84+
matrix, down, up := make([][]byte, numRows, numRows), 0, numRows-2
85+
for i := 0; i != len(s); {
86+
if down != numRows {
87+
matrix[down] = append(matrix[down], byte(s[i]))
88+
down++
89+
i++
90+
} else if up > 0 {
91+
matrix[up] = append(matrix[up], byte(s[i]))
92+
up--
93+
i++
94+
} else {
95+
up = numRows - 2
96+
down = 0
97+
}
98+
}
99+
solution := make([]byte, 0, len(s))
100+
for _, row := range matrix {
101+
for _, item := range row {
102+
solution = append(solution, item)
103+
}
104+
}
105+
return string(solution)
106+
}
107+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode
2+
3+
func myAtoi(s string) int {
4+
maxInt, signAllowed, whitespaceAllowed, sign, digits := int64(2<<30), true, true, 1, []int{}
5+
for _, c := range s {
6+
if c == ' ' && whitespaceAllowed {
7+
continue
8+
}
9+
if signAllowed {
10+
if c == '+' {
11+
signAllowed = false
12+
whitespaceAllowed = false
13+
continue
14+
} else if c == '-' {
15+
sign = -1
16+
signAllowed = false
17+
whitespaceAllowed = false
18+
continue
19+
}
20+
}
21+
if c < '0' || c > '9' {
22+
break
23+
}
24+
whitespaceAllowed, signAllowed = false, false
25+
digits = append(digits, int(c-48))
26+
}
27+
var num, place int64
28+
place, num = 1, 0
29+
lastLeading0Index := -1
30+
for i, d := range digits {
31+
if d == 0 {
32+
lastLeading0Index = i
33+
} else {
34+
break
35+
}
36+
}
37+
if lastLeading0Index > -1 {
38+
digits = digits[lastLeading0Index+1:]
39+
}
40+
var rtnMax int64
41+
if sign > 0 {
42+
rtnMax = maxInt - 1
43+
} else {
44+
rtnMax = maxInt
45+
}
46+
digitsCount := len(digits)
47+
for i := digitsCount - 1; i >= 0; i-- {
48+
num += int64(digits[i]) * place
49+
place *= 10
50+
if digitsCount-i > 10 || num > rtnMax {
51+
return int(int64(sign) * rtnMax)
52+
}
53+
}
54+
num *= int64(sign)
55+
return int(num)
56+
}

leetcode/0008.String-to-Integer-atoi/8. String to Integer (atoi)_test.go

Whitespace-only changes.

0 commit comments

Comments
 (0)