Skip to content

Add soulution.go for 0006.ZigZag Conversion #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions solution/0006.ZigZag Conversion/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
func convert(s string, numRows int) string {
if numRows == 1 {
return s
}
length := len(s)
result := make([]byte, length)
step := 2 * numRows - 2
count := 0
for i := 0; i < numRows; i++ {
for j := 0; j + i < length; j += step {
result[count] = s[i+j]
count++
if i != 0 && i != numRows - 1 && j + step - i < length {
result[count] = s[j+step-i]
count++
}
}
}
return string(result)
}
23 changes: 23 additions & 0 deletions solution/0007.Reverse Integer/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
func reverse(x int) int {
slot := make([]int, 11)
count := 0
for x != 0 {
n := x%10
slot[count] = n
count++
x /= 10
}
result := 0
flag := true
for i := 0; i < count; i++ {
if flag && slot[i] == 0 {
continue
}
flag = false
result = 10 * result + slot[i]
}
if result > math.MaxInt32 || result < math.MinInt32 {
return 0
}
return result
}
37 changes: 37 additions & 0 deletions solution/0008.String to Integer (atoi)/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
func myAtoi(str string) int {
cer := 0
result := 0
tmpResult := 0
flag := false
for _, n := range str {
if !flag && n == ' ' {
continue
}
flag = true
if cer == 0 {
if n >= '0' && n <= '9' {
cer = 1
} else if n == '+' {
cer = 1
continue
} else if cer == 0 && (n == '-') {
cer = -1
continue
}
}

if n >= '0' && n <= '9' {
tmpResult = tmpResult * 10 + ((int)(n) - 48)
result = cer * tmpResult
} else {
break
}
if result < math.MinInt32 {
return math.MinInt32
}
if result > math.MaxInt32 {
return math.MaxInt32
}
}
return result
}
3 changes: 3 additions & 0 deletions solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,20 @@
│   ├── README.md
│   ├── Solution.cpp
│   ├── Solution.java
│   ├── Solution.go
│   ├── Solution.js
│   └── Solution.py
├── 0007.Reverse Integer
│   ├── README.md
│   ├── Solution.java
│   ├── Solution.go
│   ├── Solution.js
│   ├── Solution.py
│   ├── Solution.rb
│   └── Solution2.py
├── 0008.String to Integer (atoi)
│   ├── Solution.java
│   ├── Solution.go
│   ├── Solution.js
│   └── Solution.py
├── 0009.Palindrome Number
Expand Down