Skip to content

Commit 8054fd1

Browse files
committed
initial commit
0 parents  commit 8054fd1

File tree

10 files changed

+224
-0
lines changed

10 files changed

+224
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Problem: Remove Duplicates from Sorted Array
2+
3+
**Source:** <https://leetcode.com/problems/remove-duplicates-from-sorted-array/>
4+
5+
Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.
6+
7+
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
8+
```
9+
Given nums = [1,1,2],
10+
11+
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
12+
It does not matter what you leave beyond the new length.
13+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package removedublicatesfromsortedarray
2+
3+
func removeDuplicates(nums []int) int {
4+
n := len(nums)
5+
if n == 0 || n == 1 {
6+
return n
7+
}
8+
j := 0
9+
for i := 0; i < n-1; i++ {
10+
if nums[i] != nums[i+1] {
11+
nums[j] = nums[i]
12+
j++
13+
}
14+
}
15+
nums[j] = nums[n-1]
16+
j++
17+
return j
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package removedublicatesfromsortedarray
2+
3+
import "testing"
4+
5+
func TestRemoveDuplicates(t *testing.T) {
6+
res := removeDuplicates([]int{1, 1, 2})
7+
if res != 2 {
8+
t.Fail()
9+
}
10+
}

remove-element/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package removeelement
2+
3+
func removeElement(nums []int, val int) int {
4+
n := len(nums)
5+
if n == 0 {
6+
return 0
7+
}
8+
j := 0
9+
for i := 0; i < n; i++ {
10+
if nums[i] != val {
11+
nums[j] = nums[i]
12+
j++
13+
}
14+
}
15+
return j
16+
}

remove-element/main_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package removeelement
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestRemoveElement(t *testing.T) {
9+
testCases := []struct {
10+
Nums []int
11+
Val int
12+
Len int
13+
}{
14+
{
15+
Nums: []int{3, 2, 2, 3},
16+
Val: 3,
17+
Len: 2,
18+
},
19+
{
20+
Nums: []int{1},
21+
Val: 1,
22+
Len: 0,
23+
},
24+
{
25+
Nums: []int{3, 3, 3},
26+
Val: 3,
27+
Len: 0,
28+
},
29+
}
30+
for _, test := range testCases {
31+
t.Run(fmt.Sprintf("nums=%v val=%d expected=%d", test.Nums, test.Val, test.Len), func(t *testing.T) {
32+
res := removeElement(test.Nums, test.Val)
33+
if res != test.Len {
34+
t.Errorf("wanted %d, but got %d", test.Len, res)
35+
}
36+
})
37+
}
38+
}

reverse-integer/main.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package reverseinteger
2+
3+
import (
4+
"math"
5+
)
6+
7+
// Psevdo code:
8+
// a. Extract off the rightmost digit of your input number. (1234 % 10) = 4
9+
// b. Take that digit (4) and add it into a new reversedNum.
10+
// c. Multiply reversedNum by 10 (4 * 10) = 40, this exposes a zero to the right of your (4).
11+
// d. Divide the input by 10, (removing the rightmost digit). (1234 / 10) = 123
12+
// e. Repeat at step a with 123
13+
// Source: https://stackoverflow.com/questions/3806126/java-reverse-an-int-value-without-using-array
14+
func reverse(x int) int {
15+
var n int
16+
negative := x < 0
17+
if negative {
18+
x = -x
19+
}
20+
for x != 0 {
21+
remainder := x % 10
22+
n = n*10 + remainder
23+
x = x / 10
24+
}
25+
if n > math.MaxInt32 {
26+
return 0
27+
}
28+
if negative {
29+
return -n
30+
}
31+
return n
32+
}

reverse-integer/main_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package reverseinteger
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestReverse(t *testing.T) {
9+
testCases := []struct {
10+
Input int
11+
Output int
12+
}{
13+
{
14+
Input: 123,
15+
Output: 321,
16+
},
17+
{
18+
Input: 11,
19+
Output: 11,
20+
},
21+
{
22+
Input: -1,
23+
Output: -1,
24+
},
25+
{
26+
Input: -123,
27+
Output: -321,
28+
},
29+
{
30+
Input: 120,
31+
Output: 21,
32+
},
33+
{
34+
Input: 1534236469,
35+
Output: 0,
36+
},
37+
{
38+
Input: 0,
39+
Output: 0,
40+
},
41+
}
42+
for _, test := range testCases {
43+
t.Run(fmt.Sprintf("input=%d output=%d", test.Input, test.Output), func(t *testing.T) {
44+
res := reverse(test.Input)
45+
if res != test.Output {
46+
t.Errorf("wanted %d, but got %d", test.Output, res)
47+
}
48+
})
49+
}
50+
}

two-sum/Readme.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Problem: Two Sum
2+
3+
**Source:** <https://leetcode.com/problems/two-sum/description/>
4+
5+
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
6+
7+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
8+
9+
**Example:**
10+
11+
```go
12+
Given nums = [2, 7, 11, 15], target = 9,
13+
14+
Because nums[0] + nums[1] = 2 + 7 = 9,
15+
return [0, 1].
16+
```

two-sum/main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package twosum
2+
3+
func twoSum(nums []int, target int) []int {
4+
m := map[int]int{}
5+
for i, v := range nums {
6+
complement := target - v
7+
c, ok := m[complement]
8+
if ok {
9+
return []int{c, i}
10+
}
11+
m[v] = i
12+
}
13+
panic("no solution")
14+
}

two-sum/main_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package twosum
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestTwoSum(t *testing.T) {
9+
nums := []int{2, 7, 11, 15}
10+
target := 9
11+
expected := []int{0, 1}
12+
res := twoSum(nums, target)
13+
t.Logf("res: %#v", res)
14+
if !reflect.DeepEqual(expected, res) {
15+
t.Fail()
16+
}
17+
}

0 commit comments

Comments
 (0)