diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a8d9dad --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "cSpell.words": [ + "Luffy" + ] +} \ No newline at end of file diff --git a/README.md b/README.md index cd64139..30744eb 100644 Binary files a/README.md and b/README.md differ diff --git a/addTwoNumbers/l.go b/addTwoNumbers/l.go new file mode 100644 index 0000000..28d7640 --- /dev/null +++ b/addTwoNumbers/l.go @@ -0,0 +1,55 @@ +package main + +/** + * Definition for singly-linked list. + */ +type ListNode struct { + Val int + Next *ListNode +} + +func twoNumbersRecursive(l1, l2 *ListNode, carry int) *ListNode { + sum, nextCarry := sumWithCarry(l1.Val, l2.Val) + if l1.Next == nil && l2.Next == nil { + if carry != 0 && sum+carry >= 10 { + sum, carry = sumWithCarry(sum, carry) + return &ListNode{sum, &ListNode{carry + nextCarry, nil}} + } else { + return &ListNode{sum + carry, nil} + } + } + if l2.Next == nil { + if carry != 0 && sum+carry >= 10 { + sum, carry = sumWithCarry(sum, carry) + return &ListNode{sum, twoNumbersRecursive(l1.Next, &ListNode{0, nil}, carry+nextCarry)} + } else { + return &ListNode{l1.Val + carry, l1.Next} + } + } + if l1.Next == nil { + if carry != 0 && sum+carry >= 10 { + sum, carry = sumWithCarry(sum, carry) + return &ListNode{sum, twoNumbersRecursive(&ListNode{0, nil}, l2.Next, carry+nextCarry)} + } else { + return &ListNode{l2.Val + carry, l2.Next} + } + } + if sum+carry >= 10 { + sum, carry = sumWithCarry(sum, carry) + return &ListNode{sum, twoNumbersRecursive(l1.Next, l2.Next, carry+nextCarry)} + } + + return &ListNode{sum + carry, twoNumbersRecursive(l1.Next, l2.Next, nextCarry)} +} + +func sumWithCarry(n1, n2 int) (sum, carry int) { + sum = n1 + n2 + carry = sum / 10 + sum = sum % 10 + + return sum, carry +} + +func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { + return twoNumbersRecursive(l1, l2, 0) +} diff --git a/addTwoNumbers/l_test.go b/addTwoNumbers/l_test.go new file mode 100644 index 0000000..4c31c51 --- /dev/null +++ b/addTwoNumbers/l_test.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + "reflect" + "testing" +) + +func TestAddTwoNumbers(t *testing.T) { + t.Run("[2,4,3], [5,6,7]", func(t *testing.T) { + l1 := &ListNode{2, &ListNode{4, &ListNode{3, nil}}} + l2 := &ListNode{5, &ListNode{6, &ListNode{4, nil}}} + got := addTwoNumbers(l1, l2) + want := &ListNode{7, &ListNode{0, &ListNode{8, nil}}} + copy := got + for { + fmt.Print(copy) + if copy.Next == nil { + break + } + copy = copy.Next + } + if !reflect.DeepEqual(got, want) { + t.Errorf("got %#v, want %#v", got, want) + } + }) + t.Run("l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]", func(t *testing.T) { + l1 := &ListNode{9, &ListNode{9, &ListNode{9, &ListNode{9, &ListNode{9, &ListNode{9, &ListNode{9, &ListNode{}}}}}}}} + l2 := &ListNode{9, &ListNode{9, &ListNode{9, &ListNode{9, &ListNode{}}}}} + got := addTwoNumbers(l1, l2) + // [8,9,9,9,0,0,0,1] + want := &ListNode{8, &ListNode{9, &ListNode{9, &ListNode{9, &ListNode{0, &ListNode{0, &ListNode{0, &ListNode{1, nil}}}}}}}} + copy := got + for { + fmt.Print(copy) + if copy.Next == nil { + break + } + copy = copy.Next + } + if !reflect.DeepEqual(got, want) { + t.Errorf("got %#v, want %#v", got, want) + } + + }) +} diff --git a/binSearchInsertion/binSearchInsert_test.go b/binSearchInsertion/binSearchInsert_test.go new file mode 100644 index 0000000..e596177 --- /dev/null +++ b/binSearchInsertion/binSearchInsert_test.go @@ -0,0 +1,55 @@ +package main + +import "testing" + +// Example 1: + +// Input: nums = [1,3,5,6], target = 5 +// Output: 2 +// Example 2: + +// Input: nums = [1,3,5,6], target = 2 +// Output: 1 +// Example 3: + +// Input: nums = [1,3,5,6], target = 7 +// Output: 4 +func TestSearchInsert(t *testing.T) { + t.Run("[1,3,5,6] 5", func(t *testing.T) { + nums := []int{1, 3, 5, 6} + + got := searchInsert(nums, 5) + + want := 2 + + if got != want { + t.Errorf("got %d, want %d", got, want) + } + + }) + + t.Run("[1,3,5,6] 2", func(t *testing.T) { + nums := []int{1, 3, 5, 6} + + got := searchInsert(nums, 2) + + want := 1 + + if got != want { + t.Errorf("got %d, want %d", got, want) + } + + }) + t.Run("[1,3,5,6] 7", func(t *testing.T) { + nums := []int{1, 3, 5, 6} + + got := searchInsert(nums, 7) + + want := 4 + + if got != want { + t.Errorf("got %d, want %d", got, want) + } + + }) +} diff --git a/binSearchInsertion/binSearchInsertion.go b/binSearchInsertion/binSearchInsertion.go new file mode 100644 index 0000000..91e0f01 --- /dev/null +++ b/binSearchInsertion/binSearchInsertion.go @@ -0,0 +1,24 @@ +package main + +func searchInsert(numbers []int, target int) int { + p := len(numbers) / 2 + min := 0 + max := len(numbers) - 1 + if numbers[len(numbers)-1] < target { + return len(numbers) + } + for { + if numbers[p] == target { + return p + } else if numbers[p] < target { + min = p + 1 + p = (min + max) / 2 + } else { + max = p + p = (min + max) / 2 + } + if min == max { + return p + } + } +} diff --git a/lastWord/lastWord.go b/lastWord/lastWord.go new file mode 100644 index 0000000..e72bbab --- /dev/null +++ b/lastWord/lastWord.go @@ -0,0 +1,21 @@ +package main + +import "math" + +func lengthOfLastWord(s string) int { + saved := math.MaxInt64 + for i := len(s) - 1; i >= 0; i-- { + if string(s[i]) != " " { + saved = i + break + } + } + word := "" + for i := saved; i >= 0; i-- { + if string(s[i]) == " " { + break + } + word += string(s[i]) + } + return len(word) +} diff --git a/lastWord/lastWord_test.go b/lastWord/lastWord_test.go new file mode 100644 index 0000000..f5ecd89 --- /dev/null +++ b/lastWord/lastWord_test.go @@ -0,0 +1,54 @@ +package main + +import "testing" + +// Example 1: + +// Input: s = "Hello World" +// Output: 5 +// Explanation: The last word is "World" with length 5. +// Example 2: + +// Input: s = " fly me to the moon " +// Output: 4 +// Explanation: The last word is "moon" with length 4. +// Example 3: + +// Input: s = "Luffy is still JoyBoy" +// Output: 6 +// Explanation: The last word is "JoyBoy" with length 6. + +func TestLengthOfLastWord(t *testing.T) { + t.Run("Hello World ", func(t *testing.T) { + s := "Hello World " + got := lengthOfLastWord(s) + want := 5 + + if got != want { + t.Errorf("got %v, want %v", got, want) + } + + }) + + t.Run(" fly me to the moon ", func(t *testing.T) { + s := " fly me to the moon " + got := lengthOfLastWord(s) + want := 4 + + if got != want { + t.Errorf("got %v, want %v", got, want) + } + + }) + + t.Run("Luffy is still JoyBoy", func(t *testing.T) { + s := "Luffy is still JoyBoy" + got := lengthOfLastWord(s) + want := 6 + + if got != want { + t.Errorf("got %v, want %v", got, want) + } + + }) +} diff --git a/longestPrefix/prefix.go b/longestPrefix/prefix.go new file mode 100644 index 0000000..deb0329 --- /dev/null +++ b/longestPrefix/prefix.go @@ -0,0 +1,20 @@ +package main + +func longestCommonPrefix(strs []string) string { + pref := strs[0] + + for i := range strs { + if len(pref) > len(strs[i]) { + pref = strs[i] + } + } + for i := range strs { + for j, c := range pref { + if byte(c) != strs[i][j] { + pref = pref[:j] + break + } + } + } + return pref +} diff --git a/longestPrefix/prefix_test.go b/longestPrefix/prefix_test.go new file mode 100644 index 0000000..88a605d --- /dev/null +++ b/longestPrefix/prefix_test.go @@ -0,0 +1,29 @@ +package main + +import ( + "testing" +) + +// strs = ["dog","racecar","car"] +// strs = ["flower","flow","flight"] +func TestLongestCommonPrefix(t *testing.T) { + t.Run("[dog,racecar,car]", func(t *testing.T) { + strs := []string{"dog", "racecar", "car"} + got := longestCommonPrefix(strs) + want := "" + + if got != want { + t.Errorf("got %v, want %v", got, want) + } + }) + + t.Run("[flower,flow,flight]", func(t *testing.T) { + strs := []string{"flower", "flow", "flight"} + got := longestCommonPrefix(strs) + want := "fl" + + if got != want { + t.Errorf("got %v, want %v", got, want) + } + }) +} diff --git a/mergeLists/merg.go b/mergeLists/merg.go new file mode 100644 index 0000000..0d7d702 --- /dev/null +++ b/mergeLists/merg.go @@ -0,0 +1,36 @@ +package main + +/** + * Definition for singly-linked list. + */ + +type ListNode struct { + Val int + Next *ListNode +} + +func mergeTwoListsRecursive(list1, list2 *ListNode) *ListNode { + if list1 == nil && list2 == nil { + return nil + } + if list1 == nil { + return list2 + } + if list2 == nil { + return list1 + } + if list1.Val <= list2.Val { + list1.Next = mergeTwoListsRecursive(list1.Next, list2) + return list1 + } + if list2.Val < list1.Val { + list2.Next = mergeTwoListsRecursive(list2.Next, list1) + return list2 + } + return nil +} + +func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode { + + return mergeTwoListsRecursive(list1, list2) +} diff --git a/mergeLists/merg_test.go b/mergeLists/merg_test.go new file mode 100644 index 0000000..351e356 --- /dev/null +++ b/mergeLists/merg_test.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + "reflect" + "testing" +) + +// Input: list1 = [1,2,4], list2 = [1,3,4] +// Output: [1,1,2,3,4,4] +// Example 2: + +// Input: list1 = [], list2 = [] +// Output: [] +// Example 3: + +// Input: list1 = [], list2 = [0] +// Output: [0] +func TestMergeTwoLists(t *testing.T) { + l1 := &ListNode{1, &ListNode{2, &ListNode{4, nil}}} + l2 := &ListNode{1, &ListNode{3, &ListNode{4, nil}}} + got := mergeTwoLists(l1, l2) + want := &ListNode{1, &ListNode{1, &ListNode{2, &ListNode{3, &ListNode{4, &ListNode{4, nil}}}}}} + + cp := got + for { + fmt.Printf("%v", cp) + if cp == nil { + break + } + cp = cp.Next + } + + if !reflect.DeepEqual(got, want) { + t.Errorf("got %v, want %v", got, want) + } +} diff --git a/removeDup/removeDup.go b/removeDup/removeDup.go new file mode 100644 index 0000000..5b117da --- /dev/null +++ b/removeDup/removeDup.go @@ -0,0 +1,19 @@ +package main + +func removeDuplicates(nums []int) int { + currentNumb := -101 + count := 0 + repeated := []int{} + for _, n := range nums { + if n != currentNumb { + repeated = append(repeated, n) + currentNumb = n + count++ + } + } + copy(nums, repeated) + // for i, n := range repeated { + // nums[i] = n + // } + return count +} diff --git a/removeDup/removeDup_test.go b/removeDup/removeDup_test.go new file mode 100644 index 0000000..8cb2769 --- /dev/null +++ b/removeDup/removeDup_test.go @@ -0,0 +1,20 @@ +package main + +import ( + "reflect" + "testing" +) + +func TestRemoveDuplicates(t *testing.T) { + nums := []int{0, 0, 1, 1, 1, 2, 2, 3, 3, 4} + nWant := []int{0, 1, 2, 3, 4} + want := 5 + got := removeDuplicates(nums) + + if got != want { + t.Errorf("got %v, want %v", got, want) + } + if !reflect.DeepEqual(nums[:len(nWant)], nWant) { + t.Errorf("got %v, want %v", nums[:len(nWant)], nWant) + } +} diff --git a/validParenthesis/vparenthesis.go b/validParenthesis/vparenthesis.go new file mode 100644 index 0000000..261b2b1 --- /dev/null +++ b/validParenthesis/vparenthesis.go @@ -0,0 +1,39 @@ +package main + +func isValid(s string) bool { + open := [10000]rune{} + + i := 0 + for _, r := range s { + if i == 0 { + switch r { + case '}', ']', ')': + return false + } + } + switch r { + case '(', '[', '{': + open[i] = r + i++ + case ')': + if open[i-1] != '(' { + return false + } + open[i] = 0 + i-- + case ']': + if open[i-1] != '[' { + return false + } + open[i] = 0 + i-- + case '}': + if open[i-1] != '{' { + return false + } + open[i] = 0 + i-- + } + } + return i == 0 +} diff --git a/validParenthesis/vparenthesis_test.go b/validParenthesis/vparenthesis_test.go new file mode 100644 index 0000000..c9e99b5 --- /dev/null +++ b/validParenthesis/vparenthesis_test.go @@ -0,0 +1,16 @@ +package main + +import "testing" + +func TestIsValid(t *testing.T) { + t.Run("(){}}{", func(t *testing.T) { + s := "(){}}{" + got := isValid(s) + want := false + + if got != want { + t.Errorf("got %v, want %v", got, want) + } + }) + // t.Run("()", func(t *testing.T) {}) +}