From 857dc091cd1b55fa86f891cbe111b0f99d42c299 Mon Sep 17 00:00:00 2001 From: Yeqi Tao Date: Sun, 30 Jun 2019 15:23:23 +0800 Subject: [PATCH 1/3] Add soulution.go for 0006.ZigZag Conversion --- solution/0006.ZigZag Conversion/Solution.go | 20 ++++++++++++++++++++ solution/README.md | 1 + 2 files changed, 21 insertions(+) create mode 100644 solution/0006.ZigZag Conversion/Solution.go diff --git a/solution/0006.ZigZag Conversion/Solution.go b/solution/0006.ZigZag Conversion/Solution.go new file mode 100644 index 0000000000000..929878fa93852 --- /dev/null +++ b/solution/0006.ZigZag Conversion/Solution.go @@ -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) +} \ No newline at end of file diff --git a/solution/README.md b/solution/README.md index 2d3c2709ccf00..cf07f1d1cdcf9 100644 --- a/solution/README.md +++ b/solution/README.md @@ -41,6 +41,7 @@ │   ├── README.md │   ├── Solution.cpp │   ├── Solution.java +│   ├── Solution.go │   ├── Solution.js │   └── Solution.py ├── 0007.Reverse Integer From dc72d258c29a769e6177435be0c710cf10142e2b Mon Sep 17 00:00:00 2001 From: Yeqi Tao Date: Sun, 30 Jun 2019 16:05:00 +0800 Subject: [PATCH 2/3] Add soulution.go for 0007.Reverse Integer --- solution/0007.Reverse Integer/Solution.go | 23 +++++++++++++++++++++++ solution/README.md | 1 + 2 files changed, 24 insertions(+) create mode 100644 solution/0007.Reverse Integer/Solution.go diff --git a/solution/0007.Reverse Integer/Solution.go b/solution/0007.Reverse Integer/Solution.go new file mode 100644 index 0000000000000..05dc2afb09c8a --- /dev/null +++ b/solution/0007.Reverse Integer/Solution.go @@ -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 +} \ No newline at end of file diff --git a/solution/README.md b/solution/README.md index cf07f1d1cdcf9..a540d6634f4d5 100644 --- a/solution/README.md +++ b/solution/README.md @@ -47,6 +47,7 @@ ├── 0007.Reverse Integer │   ├── README.md │   ├── Solution.java +│   ├── Solution.go │   ├── Solution.js │   ├── Solution.py │   ├── Solution.rb From 2e275e3574e1cf26d1f67df508fd493a3f478b03 Mon Sep 17 00:00:00 2001 From: Yeqi Tao Date: Sun, 30 Jun 2019 17:07:40 +0800 Subject: [PATCH 3/3] Add soulution.go for 0008.String to Integer --- .../0008.String to Integer (atoi)/Solution.go | 37 +++++++++++++++++++ solution/README.md | 1 + 2 files changed, 38 insertions(+) create mode 100644 solution/0008.String to Integer (atoi)/Solution.go diff --git a/solution/0008.String to Integer (atoi)/Solution.go b/solution/0008.String to Integer (atoi)/Solution.go new file mode 100644 index 0000000000000..230cf220a693e --- /dev/null +++ b/solution/0008.String to Integer (atoi)/Solution.go @@ -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 +} \ No newline at end of file diff --git a/solution/README.md b/solution/README.md index a540d6634f4d5..b90f6f1271df1 100644 --- a/solution/README.md +++ b/solution/README.md @@ -54,6 +54,7 @@ │   └── Solution2.py ├── 0008.String to Integer (atoi) │   ├── Solution.java +│   ├── Solution.go │   ├── Solution.js │   └── Solution.py ├── 0009.Palindrome Number