Skip to content

Commit e8e14e7

Browse files
authored
Merge pull request #182 from taoyq1988/dev
Add soulution.go for 0006.ZigZag Conversion
2 parents 942a5c9 + 2e275e3 commit e8e14e7

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func convert(s string, numRows int) string {
2+
if numRows == 1 {
3+
return s
4+
}
5+
length := len(s)
6+
result := make([]byte, length)
7+
step := 2 * numRows - 2
8+
count := 0
9+
for i := 0; i < numRows; i++ {
10+
for j := 0; j + i < length; j += step {
11+
result[count] = s[i+j]
12+
count++
13+
if i != 0 && i != numRows - 1 && j + step - i < length {
14+
result[count] = s[j+step-i]
15+
count++
16+
}
17+
}
18+
}
19+
return string(result)
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func reverse(x int) int {
2+
slot := make([]int, 11)
3+
count := 0
4+
for x != 0 {
5+
n := x%10
6+
slot[count] = n
7+
count++
8+
x /= 10
9+
}
10+
result := 0
11+
flag := true
12+
for i := 0; i < count; i++ {
13+
if flag && slot[i] == 0 {
14+
continue
15+
}
16+
flag = false
17+
result = 10 * result + slot[i]
18+
}
19+
if result > math.MaxInt32 || result < math.MinInt32 {
20+
return 0
21+
}
22+
return result
23+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
func myAtoi(str string) int {
2+
cer := 0
3+
result := 0
4+
tmpResult := 0
5+
flag := false
6+
for _, n := range str {
7+
if !flag && n == ' ' {
8+
continue
9+
}
10+
flag = true
11+
if cer == 0 {
12+
if n >= '0' && n <= '9' {
13+
cer = 1
14+
} else if n == '+' {
15+
cer = 1
16+
continue
17+
} else if cer == 0 && (n == '-') {
18+
cer = -1
19+
continue
20+
}
21+
}
22+
23+
if n >= '0' && n <= '9' {
24+
tmpResult = tmpResult * 10 + ((int)(n) - 48)
25+
result = cer * tmpResult
26+
} else {
27+
break
28+
}
29+
if result < math.MinInt32 {
30+
return math.MinInt32
31+
}
32+
if result > math.MaxInt32 {
33+
return math.MaxInt32
34+
}
35+
}
36+
return result
37+
}

โ€Žsolution/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,20 @@
4141
โ”‚ย ย  โ”œโ”€โ”€ README.md
4242
โ”‚ย ย  โ”œโ”€โ”€ Solution.cpp
4343
โ”‚ย ย  โ”œโ”€โ”€ Solution.java
44+
โ”‚ย ย  โ”œโ”€โ”€ Solution.go
4445
โ”‚ย ย  โ”œโ”€โ”€ Solution.js
4546
โ”‚ย ย  โ””โ”€โ”€ Solution.py
4647
โ”œโ”€โ”€ 0007.Reverse Integer
4748
โ”‚ย ย  โ”œโ”€โ”€ README.md
4849
โ”‚ย ย  โ”œโ”€โ”€ Solution.java
50+
โ”‚ย ย  โ”œโ”€โ”€ Solution.go
4951
โ”‚ย ย  โ”œโ”€โ”€ Solution.js
5052
โ”‚ย ย  โ”œโ”€โ”€ Solution.py
5153
โ”‚ย ย  โ”œโ”€โ”€ Solution.rb
5254
โ”‚ย ย  โ””โ”€โ”€ Solution2.py
5355
โ”œโ”€โ”€ 0008.String to Integer (atoi)
5456
โ”‚ย ย  โ”œโ”€โ”€ Solution.java
57+
โ”‚ย ย  โ”œโ”€โ”€ Solution.go
5558
โ”‚ย ย  โ”œโ”€โ”€ Solution.js
5659
โ”‚ย ย  โ””โ”€โ”€ Solution.py
5760
โ”œโ”€โ”€ 0009.Palindrome Number

0 commit comments

Comments
ย (0)