Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0eaafc5

Browse files
Merge pull request doocs#228 from ElectricBubble/master
Add solution 13, 21, 27, 28, 35, 38, 53, 58 in Golang
2 parents 12aac43 + 2c85704 commit 0eaafc5

File tree

10 files changed

+185
-0
lines changed

10 files changed

+185
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func romanToInt(s string) int {
2+
symbols := map[string]int{"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
3+
ret := 0
4+
l := len(s)
5+
for i := 0; i < l-1; i++ {
6+
if symbols[s[i:i+1]] < symbols[s[i+1:i+2]] {
7+
ret -= symbols[s[i:i+1]]
8+
} else {
9+
ret += symbols[s[i:i+1]]
10+
}
11+
}
12+
ret += symbols[s[l-1:]]
13+
return ret
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
2+
if l1 == nil {
3+
return l2
4+
}
5+
if l2 == nil {
6+
return l1
7+
}
8+
if l1.Val < l2.Val {
9+
l1.Next = mergeTwoLists(l1.Next, l2)
10+
return l1
11+
} else {
12+
l2.Next = mergeTwoLists(l1, l2.Next)
13+
return l2
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func removeElement(nums []int, val int) int {
2+
if len(nums) == 0 {
3+
return 0
4+
}
5+
i := 0
6+
for j := 0; j < len(nums); j++ {
7+
if nums[j] != val {
8+
nums[i] = nums[j]
9+
i++
10+
}
11+
}
12+
return i
13+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func strStr(haystack string, needle string) int {
2+
switch {
3+
case len(needle) == 0:
4+
return 0
5+
case len(needle) > len(haystack):
6+
return -1
7+
case len(needle) == len(haystack):
8+
if needle == haystack {
9+
return 0
10+
}
11+
return -1
12+
}
13+
cursor := 0
14+
for i := 0; i < len(haystack); i++ {
15+
if haystack[i] == needle[cursor] {
16+
cursor++
17+
if cursor == len(needle) {
18+
return i - cursor + 1
19+
}
20+
} else {
21+
i -= cursor
22+
cursor = 0
23+
}
24+
}
25+
return -1
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func searchInsert(nums []int, target int) int {
2+
left, right := 0, len(nums)
3+
for left < right {
4+
mid := (left + right) >> 1
5+
if nums[mid] >= target {
6+
right = mid
7+
} else {
8+
left = mid + 1
9+
}
10+
}
11+
return left
12+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## 报数
2+
### 题目描述
3+
4+
报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下:
5+
```
6+
1. 1
7+
2. 11
8+
3. 21
9+
4. 1211
10+
5. 111221
11+
```
12+
13+
`1` 被读作 `"one 1"` (`"一个一"`) , 即 `11`
14+
`11` 被读作 `"two 1s"` (`"两个一"`), 即 `21`
15+
`21` 被读作 `"one 2"`, `"one 1"` (`"一个二"` , `"一个一"`) , 即 `1211`
16+
17+
给定一个正整数 n(1 ≤ n ≤ 30),输出报数序列的第 n 项。
18+
19+
注意:整数顺序将表示为一个字符串。
20+
21+
22+
23+
示例 1:
24+
```
25+
输入: 1
26+
输出: "1"
27+
```
28+
29+
示例 2:
30+
```
31+
输入: 4
32+
输出: "1211"
33+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func countAndSay(n int) string {
2+
buf := bytes.NewBufferString("1")
3+
for i := 2; i <= n; i++ {
4+
s := buf.String()
5+
c, l := s[0:1], len(s)
6+
buf.Reset()
7+
count := 0
8+
for j := 0; j < l; j++ {
9+
if c == s[j:j+1] {
10+
count++
11+
} else {
12+
buf.WriteByte(byte(48 + count))
13+
buf.WriteString(c)
14+
count = 1
15+
c = s[j : j+1]
16+
}
17+
}
18+
buf.WriteByte(byte(48 + count))
19+
buf.WriteString(c)
20+
}
21+
return buf.String()
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func maxSubArray(nums []int) int {
2+
ans := nums[0]
3+
sum := 0
4+
for _, n := range nums {
5+
if sum > 0 {
6+
sum += n
7+
} else {
8+
sum = n
9+
}
10+
if sum > ans {
11+
ans = sum
12+
}
13+
}
14+
return ans
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## 最后一个单词的长度
2+
### 题目描述
3+
4+
给定一个仅包含大小写字母和空格 `' '` 的字符串,返回其最后一个单词的长度。
5+
6+
如果不存在最后一个单词,请返回 `0`
7+
8+
说明:一个单词是指由字母组成,但不包含任何空格的字符串。
9+
10+
示例:
11+
12+
13+
14+
```
15+
输入: "Hello World"
16+
输出: 5
17+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func lengthOfLastWord(s string) int {
2+
if len(s) == 0 {
3+
return 0
4+
}
5+
space := []byte(" ")[0]
6+
for len(s) != 0 && s[len(s)-1] == space {
7+
s = s[:len(s)-1]
8+
}
9+
ret := 0
10+
for i := len(s) - 1; i >= 0; i-- {
11+
if s[i] != space {
12+
ret++
13+
} else {
14+
return ret
15+
}
16+
}
17+
return ret
18+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /