-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yu Zhao
committed
May 12, 2018
1 parent
4ea8d6d
commit 1d211d1
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type Set map[interface{}]bool | ||
|
||
func (s *Set) Add(key interface{}) { | ||
(*s)[key] = true | ||
} | ||
|
||
func (s *Set) Remove(key interface{}) { | ||
delete((*s), key) | ||
} | ||
|
||
func (s *Set) Find(key interface{}) bool { | ||
return (*s)[key] | ||
} | ||
|
||
func main() { | ||
mp := make(Set) | ||
mp.Add("a") | ||
mp.Add("b") | ||
mp.Add("a") | ||
fmt.Println(mp.Find("a")) | ||
fmt.Println(mp.Find("b")) | ||
fmt.Println(mp.Find("c")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// find first repeated in a list | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
arr1 := []int{1,2,3,1,2,3} | ||
arr2 := []int{1,1,0,0} | ||
arr3 := []int{1,2,3,4,5} | ||
fmt.Println(findFirstRepeat(arr1)) | ||
fmt.Println(findFirstRepeat(arr2)) | ||
fmt.Println(findFirstRepeat(arr3)) | ||
} | ||
|
||
|
||
func findFirstRepeat(arr []int) (int, bool) { // return index, found | ||
table := make(map[int]bool, len(arr)) | ||
for index, item := range arr { | ||
if !table[item] { | ||
table[item] = true | ||
} else { | ||
return index, true | ||
} | ||
} | ||
return -1, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
) | ||
|
||
func main() { | ||
arr := []int{1,5,6,7,2,8,3,4,9} | ||
|
||
fmt.Println(find3ForSum(arr, 13)) | ||
fmt.Println(find3ForSum1(arr, 13)) | ||
} | ||
|
||
func find3ForSum(arr []int, sum int) (int, int, int, bool) { | ||
// brute force, O(n^3) | ||
length := len(arr) | ||
|
||
for i := 0; i < length - 2; i ++ { | ||
for j := i + 1; j < length - 1; j ++ { | ||
for k := j + 1; k < length; k ++ { | ||
if arr[i] + arr[j] + arr[k] == sum { | ||
return arr[i], arr[j], arr[k], true | ||
} | ||
} | ||
} | ||
} | ||
return -1, -1, -1, false | ||
} | ||
|
||
func find3ForSum1(arr []int, sum int) (int, int, int, bool) { | ||
sort.Slice(arr, func (i, j int) bool { | ||
return arr[i] < arr[j] | ||
}) | ||
// fmt.Println(arr) | ||
|
||
length := len(arr) | ||
|
||
for i := 0; i < length - 2; i ++ { | ||
j := i + 1 | ||
k := length - 1 | ||
for ; j < length - 1 && k > j; { | ||
temp := arr[i] + arr[j] + arr[k] | ||
if temp == sum { | ||
return arr[i], arr[j], arr[k], true | ||
} else if temp > sum { | ||
k -- | ||
} else { | ||
j ++ | ||
} | ||
} | ||
} | ||
return -1, -1, -1, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// 5.37 | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
matrix := [][]int{ | ||
[]int{1,11,21,31}, | ||
[]int{2,12,22,32}, | ||
[]int{3,13,23,33}, | ||
[]int{4,14,24,34}} | ||
|
||
fmt.Println(find2d(matrix, -1)) | ||
fmt.Println(find2d(matrix, 1)) | ||
fmt.Println(find2d(matrix, 31)) | ||
fmt.Println(find2d(matrix, 2)) | ||
fmt.Println(find2d(matrix, 13)) | ||
fmt.Println(find2d(matrix, 4)) | ||
fmt.Println(find2d(matrix, 34)) | ||
fmt.Println(find2d(matrix, 100)) | ||
} | ||
|
||
func find2d(matrix[][]int, target int) (row, col int) { | ||
col = len(matrix[0]) - 1 | ||
|
||
for row < len(matrix) && col >= 0 { | ||
this := matrix[row][col] | ||
if this == target { | ||
return | ||
} else if this < target { | ||
row ++ | ||
} else { | ||
col -- | ||
} | ||
} | ||
return -1, -1 | ||
} |