Skip to content

Commit 4a14518

Browse files
committed
add new files
1 parent bee5de7 commit 4a14518

8 files changed

+178
-0
lines changed

287. Find the Duplicate Number.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
func findDuplicate(nums []int) int {
4+
left, right := 1, len(nums)-1
5+
mid := left + (right-left)/2
6+
for left < right {
7+
c := 0
8+
mid = left + (right-left)/2
9+
for i := 0; i < len(nums); i++ {
10+
if nums[i] <= mid {
11+
c++
12+
}
13+
}
14+
if c > mid {
15+
right = mid
16+
} else {
17+
left = mid + 1
18+
}
19+
}
20+
return left
21+
}

344. Reverse String.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
func reverseString(s []byte) {
4+
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
5+
s[i], s[j] = s[j], s[i]
6+
}
7+
8+
}

349. Intersection of Two Arrays.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
func intersection(nums1 []int, nums2 []int) []int {
4+
m := make(map[int]bool)
5+
for _, v := range nums1 {
6+
m[v] = true
7+
}
8+
9+
ret := []int{}
10+
for _, v := range nums2 {
11+
if val, ok := m[v]; val && ok {
12+
ret = append(ret, v)
13+
m[v] = false
14+
}
15+
}
16+
return ret
17+
}

350. Intersection of Two Arrays II.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
func intersect(nums1 []int, nums2 []int) []int {
4+
m := make(map[int]int)
5+
6+
for _, v := range nums1 {
7+
if _, ok := m[v]; ok {
8+
m[v]++
9+
} else {
10+
m[v] = 1
11+
}
12+
}
13+
14+
ret := []int{}
15+
16+
for _, v := range nums2 {
17+
if val, ok := m[v]; val > 0 && ok {
18+
ret = append(ret, v)
19+
m[v]--
20+
}
21+
}
22+
return ret
23+
}

382. Linked List Random Node.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import "math/rand"
4+
5+
type ListNode struct {
6+
Val int
7+
Next *ListNode
8+
}
9+
10+
type Solution struct {
11+
count int
12+
head *ListNode
13+
pool []int
14+
}
15+
16+
/** @param head The linked list's head.
17+
Note that the head is guaranteed to be not null, so it contains at least one node. */
18+
func Constructor(head *ListNode) Solution {
19+
var s Solution
20+
s.head = head
21+
return s
22+
}
23+
24+
/** Returns a random node's value. */
25+
func (this *Solution) GetRandom() int {
26+
r := rand.Intn(this.count + 1)
27+
this.count++
28+
if r == this.count-1 && this.head != nil {
29+
t := this.head
30+
this.head = this.head.Next
31+
this.pool = append(this.pool, t.Val)
32+
return t.Val
33+
} else {
34+
return this.pool[r%len(this.pool)]
35+
}
36+
37+
}

389. Find the Difference.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
//预先把每个字母都装进hash
4+
func findTheDifference(s string, t string) byte {
5+
6+
m := make(map[int32]int, 256)
7+
8+
var i int32
9+
for i = 0; i < 256; i++ {
10+
m[i] = 0
11+
}
12+
13+
for _, v := range s {
14+
m[v]++
15+
}
16+
for _, v := range t {
17+
m[v]--
18+
if m[v] < 0 {
19+
return byte(v)
20+
}
21+
}
22+
return 0
23+
}

404. Sum of Left Leaves.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
func sumOfLeftLeaves(root *TreeNode) int {
10+
sum := 0
11+
if root != nil {
12+
if isLeaf(root.Left) {
13+
sum += root.Left.Val
14+
} else {
15+
sum += sumOfLeftLeaves(root.Left)
16+
}
17+
sum += sumOfLeftLeaves(root.Right)
18+
}
19+
return sum
20+
}
21+
22+
func isLeaf(root *TreeNode) bool {
23+
if root == nil || root.Left != nil || root.Right != nil {
24+
return false
25+
}
26+
return true
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
func countSegments(s string) int {
4+
5+
count := 1
6+
found := false
7+
for i := 0; i < len(s); i++ {
8+
if i > 0 && s[i] == ' ' && s[i+1] != ' ' {
9+
count++
10+
}
11+
if s[i] != ' ' {
12+
found = true
13+
}
14+
if i == len(s)-1 && s[i] == ' ' {
15+
count--
16+
}
17+
}
18+
if found {
19+
return count
20+
}
21+
return 0
22+
}

0 commit comments

Comments
 (0)