Skip to content

Add solution 13, 21, 27, 28, 35, 38, 53, 58 in Golang #228

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 10 commits into from
Oct 25, 2019
14 changes: 14 additions & 0 deletions solution/0013.Roman to Integer/Solution2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
func romanToInt(s string) int {
symbols := map[string]int{"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
ret := 0
l := len(s)
for i := 0; i < l-1; i++ {
if symbols[s[i:i+1]] < symbols[s[i+1:i+2]] {
ret -= symbols[s[i:i+1]]
} else {
ret += symbols[s[i:i+1]]
}
}
ret += symbols[s[l-1:]]
return ret
}
15 changes: 15 additions & 0 deletions solution/0021.Merge Two Sorted Lists/Solution2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
if l1 == nil {
return l2
}
if l2 == nil {
return l1
}
if l1.Val < l2.Val {
l1.Next = mergeTwoLists(l1.Next, l2)
return l1
} else {
l2.Next = mergeTwoLists(l1, l2.Next)
return l2
}
}
13 changes: 13 additions & 0 deletions solution/0027.Remove Element/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
func removeElement(nums []int, val int) int {
if len(nums) == 0 {
return 0
}
i := 0
for j := 0; j < len(nums); j++ {
if nums[j] != val {
nums[i] = nums[j]
i++
}
}
return i
}
26 changes: 26 additions & 0 deletions solution/0028.Implement strStr()/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
func strStr(haystack string, needle string) int {
switch {
case len(needle) == 0:
return 0
case len(needle) > len(haystack):
return -1
case len(needle) == len(haystack):
if needle == haystack {
return 0
}
return -1
}
cursor := 0
for i := 0; i < len(haystack); i++ {
if haystack[i] == needle[cursor] {
cursor++
if cursor == len(needle) {
return i - cursor + 1
}
} else {
i -= cursor
cursor = 0
}
}
return -1
}
12 changes: 12 additions & 0 deletions solution/0035.Search Insert Position/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
func searchInsert(nums []int, target int) int {
left, right := 0, len(nums)
for left < right {
mid := (left + right) >> 1
if nums[mid] >= target {
right = mid
} else {
left = mid + 1
}
}
return left
}
33 changes: 33 additions & 0 deletions solution/0038.Count and Say/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## 报数
### 题目描述

报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下:
```
1. 1
2. 11
3. 21
4. 1211
5. 111221
```

`1` 被读作  `"one 1"`  (`"一个一"`) , 即 `11`。
`11` 被读作 `"two 1s"` (`"两个一"`), 即 `21`。
`21` 被读作 `"one 2"`,  `"one 1"` (`"一个二"` ,  `"一个一"`) , 即 `1211`。

给定一个正整数 n(1 ≤ n ≤ 30),输出报数序列的第 n 项。

注意:整数顺序将表示为一个字符串。



示例 1:
```
输入: 1
输出: "1"
```

示例 2:
```
输入: 4
输出: "1211"
```
22 changes: 22 additions & 0 deletions solution/0038.Count and Say/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
func countAndSay(n int) string {
buf := bytes.NewBufferString("1")
for i := 2; i <= n; i++ {
s := buf.String()
c, l := s[0:1], len(s)
buf.Reset()
count := 0
for j := 0; j < l; j++ {
if c == s[j:j+1] {
count++
} else {
buf.WriteByte(byte(48 + count))
buf.WriteString(c)
count = 1
c = s[j : j+1]
}
}
buf.WriteByte(byte(48 + count))
buf.WriteString(c)
}
return buf.String()
}
15 changes: 15 additions & 0 deletions solution/0053.Maximum Subarray/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
func maxSubArray(nums []int) int {
ans := nums[0]
sum := 0
for _, n := range nums {
if sum > 0 {
sum += n
} else {
sum = n
}
if sum > ans {
ans = sum
}
}
return ans
}
17 changes: 17 additions & 0 deletions solution/0058.Length of Last Word/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## 最后一个单词的长度
### 题目描述

给定一个仅包含大小写字母和空格 `' '` 的字符串,返回其最后一个单词的长度。

如果不存在最后一个单词,请返回 `0` 。

说明:一个单词是指由字母组成,但不包含任何空格的字符串。

示例:



```
输入: "Hello World"
输出: 5
```
18 changes: 18 additions & 0 deletions solution/0058.Length of Last Word/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
func lengthOfLastWord(s string) int {
if len(s) == 0 {
return 0
}
space := []byte(" ")[0]
for len(s) != 0 && s[len(s)-1] == space {
s = s[:len(s)-1]
}
ret := 0
for i := len(s) - 1; i >= 0; i-- {
if s[i] != space {
ret++
} else {
return ret
}
}
return ret
}