Skip to content

Commit 5959af5

Browse files
First batch of problems
0 parents  commit 5959af5

14 files changed

+861
-0
lines changed

3sum-closest.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
"sort"
7+
)
8+
9+
func intAbs(n int) int {
10+
return (int)(math.Abs((float64)(n)))
11+
}
12+
13+
func threeSumClosest(nums []int, target int) int {
14+
sort.Ints(nums)
15+
result := nums[0] + nums[1] + nums[2]
16+
17+
for k := 0; k <= len(nums)-3; k++ {
18+
i := k + 1
19+
j := len(nums) - 1
20+
21+
if nums[k] > 0 && nums[k] > target {
22+
return result
23+
}
24+
25+
for i < j {
26+
ijkSum := nums[i] + nums[j] + nums[k]
27+
28+
if ijkSum == target {
29+
return target
30+
} else {
31+
if intAbs(target-ijkSum) < intAbs(target-result) {
32+
result = ijkSum
33+
}
34+
35+
if ijkSum < target {
36+
i++
37+
} else {
38+
j--
39+
}
40+
}
41+
}
42+
}
43+
44+
return result
45+
}
46+
47+
func main() {
48+
49+
test1 := []int{-1, 2, 1, -4}
50+
ans1 := threeSumClosest(test1, 1)
51+
52+
fmt.Printf("ans1: %v\n", ans1)
53+
54+
}

3sum.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
)
7+
8+
func checkIfTriplePresent(result [][]int, triple []int) bool {
9+
for _, t := range result {
10+
if t[0] > triple[0] {
11+
return false
12+
}
13+
14+
if t[0] == triple[0] && t[1] == triple[1] && t[2] == triple[2] {
15+
return true
16+
}
17+
}
18+
19+
return false
20+
}
21+
22+
func threeSum(nums []int) [][]int {
23+
sort.Ints(nums)
24+
result := make([][]int, 0 /*len(nums)*/, 10)
25+
26+
for k := 0; k <= len(nums)-3; k++ {
27+
if k > 0 && nums[k] == nums[k-1] {
28+
continue
29+
}
30+
31+
if nums[k] > 0 {
32+
break
33+
}
34+
35+
i := k + 1
36+
j := len(nums) - 1
37+
38+
for i < j {
39+
ijSum := nums[i] + nums[j]
40+
41+
if ijSum == -nums[k] {
42+
// add to result
43+
resultTriple := []int{nums[k], nums[i], nums[j]}
44+
45+
result = append(result, resultTriple)
46+
47+
i++
48+
j--
49+
50+
for i < j && nums[i] == nums[i-1] {
51+
i++
52+
}
53+
54+
for i < j && nums[j] == nums[j+1] {
55+
j--
56+
}
57+
} else if ijSum < -nums[k] {
58+
i++
59+
} else {
60+
j--
61+
}
62+
}
63+
}
64+
65+
return result
66+
}
67+
68+
func main() {
69+
70+
test1 := []int{-1, 0, 1, 2, -1, -4}
71+
ans1 := threeSum(test1)
72+
73+
fmt.Printf("ans1: %v\n", ans1)
74+
75+
}

4sum.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
)
7+
8+
// nums is assumed to be pre-sorted
9+
func threeSum(nums []int, target int) [][]int {
10+
result := make([][]int, 0, 10)
11+
12+
for k := 0; k <= len(nums)-3; k++ {
13+
if k > 0 && nums[k] == nums[k-1] {
14+
continue
15+
}
16+
17+
i := k + 1
18+
j := len(nums) - 1
19+
20+
for i < j {
21+
ijkSum := nums[i] + nums[j] + nums[k]
22+
23+
if ijkSum == target {
24+
// add to result
25+
resultTriple := []int{nums[k], nums[i], nums[j]}
26+
27+
result = append(result, resultTriple)
28+
29+
i++
30+
j--
31+
32+
for i < j && nums[i] == nums[i-1] {
33+
i++
34+
}
35+
36+
for i < j && nums[j] == nums[j+1] {
37+
j--
38+
}
39+
} else if ijkSum < target {
40+
i++
41+
} else {
42+
j--
43+
}
44+
}
45+
}
46+
47+
return result
48+
}
49+
50+
func fourSum(nums []int, target int) [][]int {
51+
sort.Ints(nums)
52+
result := make([][]int, 0, 10)
53+
54+
for i, v := range nums {
55+
if i > 0 && nums[i] == nums[i-1] {
56+
continue
57+
}
58+
59+
if i > len(nums)-4 {
60+
break
61+
}
62+
63+
fmt.Printf("v: %d; looking for %d in %v\n", v, target-v, nums[i+1:])
64+
resultsWOv := threeSum(nums[i+1:], target-v)
65+
66+
for _, r := range resultsWOv {
67+
result = append(result, []int{v, r[0], r[1], r[2]})
68+
}
69+
}
70+
71+
return result
72+
}
73+
74+
func main() {
75+
76+
test1 := []int{1, -2, -5, -4, -3, 3, 3, 5}
77+
ans1 := fourSum(test1, -11)
78+
fmt.Printf("ans1: %v\n", ans1)
79+
80+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# leetcode-problems

all-possible-full-binary-trees.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
func allPossibleFBT(N int) []*TreeNode {
10+
if N%2 == 0 {
11+
return []*TreeNode{}
12+
}
13+
14+
if N == 1 {
15+
return []*TreeNode{&(TreeNode{Val: 0, Left: nil, Right: nil})}
16+
}
17+
18+
possibleTrees := make([]*TreeNode, 0, 5)
19+
20+
for i := 1; i < N-1; i++ {
21+
lN := i
22+
rN := N - 1 - i
23+
24+
lTrees := allPossibleFBT(lN)
25+
rTrees := allPossibleFBT(rN)
26+
27+
for _, lTree := range lTrees {
28+
for _, rTree := range rTrees {
29+
root := TreeNode{Val: 0, Left: lTree, Right: rTree}
30+
possibleTrees = append(possibleTrees, &root)
31+
}
32+
}
33+
}
34+
35+
return possibleTrees
36+
}

atoi.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
func myAtoi(str string) int {
9+
sgn := 1
10+
num := 0
11+
numBegun := false
12+
numDigs := 0
13+
14+
for i := 0; i < len(str); i++ {
15+
if numBegun && (str[i] < '0' || str[i] > '9') {
16+
break
17+
}
18+
19+
if str[i] == ' ' {
20+
continue
21+
}
22+
23+
if str[i] == '-' {
24+
sgn = -1
25+
numBegun = true
26+
27+
continue
28+
}
29+
30+
if str[i] == '+' {
31+
numBegun = true
32+
33+
continue
34+
}
35+
36+
if str[i] < '0' || str[i] > '9' {
37+
break
38+
}
39+
40+
if num == -1*(1<<31) || num == ((1<<31)-1) {
41+
break
42+
}
43+
44+
if str[i] >= '0' && str[i] <= '9' {
45+
numBegun = true
46+
digit := (int)(str[i] - '0')
47+
48+
if numDigs == 10 {
49+
if sgn == -1 {
50+
// Hack ... we don't want -1 being multiplied with INT_MIN (the result will overflow INT_MAX)
51+
sgn = 1
52+
num = -2147483648
53+
} else {
54+
num = 2147483647
55+
}
56+
57+
break
58+
}
59+
60+
num = 10*num + digit
61+
numDigs = len(fmt.Sprintf("%d", num))
62+
}
63+
}
64+
65+
ans := sgn * num
66+
67+
if ans < math.MinInt32 {
68+
return math.MinInt32
69+
} else if ans > math.MaxInt32 {
70+
return math.MaxInt32
71+
} else {
72+
return ans
73+
}
74+
}
75+
76+
func main() {
77+
78+
tests := map[string]int{
79+
"1234": 1234,
80+
" 1234": 1234,
81+
" ": 0,
82+
" +": 0,
83+
" -": 0,
84+
" 1 2": 1,
85+
" 1+2": 1,
86+
"-1+2": -1,
87+
"++1": 0,
88+
"--1": 0,
89+
"abcd1234": 0,
90+
" 1234 43": 1234,
91+
"-91283472332": -2147483648,
92+
"91283472332": 2147483647,
93+
" -91283472332 ": -2147483648,
94+
" 91283472332 ": 2147483647,
95+
"2147483648": 2147483647,
96+
"-2147483649": -2147483648,
97+
}
98+
99+
failures := make(map[string]int)
100+
failureMessages := make([]string, 0, len(tests))
101+
102+
for str, num := range tests {
103+
ans := myAtoi(str)
104+
105+
if ans != num {
106+
failureMessages = append(failureMessages, fmt.Sprintf("Test failed for %s. Expected %d, got %d\n", str, num, ans))
107+
failures[str] = ans
108+
}
109+
}
110+
111+
if len(failures) == 0 {
112+
fmt.Printf("All tests passed\n")
113+
} else {
114+
for _, msg := range failureMessages {
115+
fmt.Printf("Failure: %s\n", msg)
116+
}
117+
}
118+
119+
}

0 commit comments

Comments
 (0)