From ce99490294e459379d5c4281b542da51dbe953cc Mon Sep 17 00:00:00 2001 From: Angelica Gardner <23157778+angelicagardner@users.noreply.github.com> Date: Mon, 17 Jun 2024 23:11:56 +0200 Subject: [PATCH] feat: add romanToInteger solution --- .golangci.yaml | 29 ++++++++++++++++ LeetCode/problem001_TwoSum.go | 4 +-- LeetCode/problem001_TwoSum_test.go | 40 ++++++++++++++-------- LeetCode/problem009_PalindromeNumber.go | 7 +++- LeetCode/problem013_RomanToInteger.go | 31 +++++++++++++++++ LeetCode/problem013_RomanToInteger_test.go | 28 +++++++++++++++ 6 files changed, 122 insertions(+), 17 deletions(-) create mode 100644 .golangci.yaml create mode 100644 LeetCode/problem013_RomanToInteger.go create mode 100644 LeetCode/problem013_RomanToInteger_test.go diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..a10b952 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,29 @@ +linters: + enable: + - errorlint + #- godot + - goimports + - misspell + - nolintlint + - predeclared + - testifylint + - unconvert + - durationcheck + - exportloopref + - nilerr + - whitespace + - unparam + - tagalign + - stylecheck + - gosec + #- gofumpt + #- perfsprint + # - nestif + # - noctx + # - prealloc + # - depguard + # - revive + # - gocritic + # - wastedassign + # - goconst + # - gomnd \ No newline at end of file diff --git a/LeetCode/problem001_TwoSum.go b/LeetCode/problem001_TwoSum.go index 75ebd33..c57b07b 100644 --- a/LeetCode/problem001_TwoSum.go +++ b/LeetCode/problem001_TwoSum.go @@ -6,7 +6,7 @@ package leetcode // Time complexity: O(n^2) // Space complexity: O(1) -func TwoSumBruteForce(nums []int, target int) []int { +func twoSumBruteForce(nums []int, target int) []int { for i := 0; i < len(nums); i++ { for j := i + 1; j < len(nums); j++ { if nums[i]+nums[j] == target { @@ -19,7 +19,7 @@ func TwoSumBruteForce(nums []int, target int) []int { // Time complexity: O(n) // Space complexity: O(n) -func TwoSumHashMap(nums []int, target int) []int { +func twoSumHashMap(nums []int, target int) []int { hashMap := make(map[int]int) for i, num := range nums { complement := target - num diff --git a/LeetCode/problem001_TwoSum_test.go b/LeetCode/problem001_TwoSum_test.go index 194aad2..9bd6ed2 100644 --- a/LeetCode/problem001_TwoSum_test.go +++ b/LeetCode/problem001_TwoSum_test.go @@ -5,22 +5,22 @@ import ( "testing" ) -var tests = []struct { - name string - nums []int - target int - output []int -}{ - {"case1", []int{2, 7, 11, 15}, 9, []int{0, 1}}, - {"case2", []int{2, 1, 5, 3}, 4, []int{1, 3}}, - {"case3", []int{3, 2, 4}, 6, []int{1, 2}}, - {"case4", []int{3, 3}, 6, []int{0, 1}}, -} - func TestTwoSumBruteForce(t *testing.T) { + var tests = []struct { + name string + nums []int + target int + output []int + }{ + {"case1", []int{2, 7, 11, 15}, 9, []int{0, 1}}, + {"case2", []int{2, 1, 5, 3}, 4, []int{1, 3}}, + {"case3", []int{3, 2, 4}, 6, []int{1, 2}}, + {"case4", []int{3, 3}, 6, []int{0, 1}}, + } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - result := TwoSumBruteForce(tt.nums, tt.target) + result := twoSumBruteForce(tt.nums, tt.target) if !reflect.DeepEqual(result, tt.output) { t.Errorf("TwoSumBruteForce(%v, %v) = %v; want %v", tt.nums, tt.target, result, tt.output) } @@ -29,9 +29,21 @@ func TestTwoSumBruteForce(t *testing.T) { } func TestTwoSumHashMap(t *testing.T) { + var tests = []struct { + name string + nums []int + target int + output []int + }{ + {"case1", []int{2, 7, 11, 15}, 9, []int{0, 1}}, + {"case2", []int{2, 1, 5, 3}, 4, []int{1, 3}}, + {"case3", []int{3, 2, 4}, 6, []int{1, 2}}, + {"case4", []int{3, 3}, 6, []int{0, 1}}, + } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - result := TwoSumHashMap(tt.nums, tt.target) + result := twoSumHashMap(tt.nums, tt.target) if !reflect.DeepEqual(result, tt.output) { t.Errorf("TwoSumHashMap(%v, %v) = %v; want %v", tt.nums, tt.target, result, tt.output) } diff --git a/LeetCode/problem009_PalindromeNumber.go b/LeetCode/problem009_PalindromeNumber.go index bf57773..2bf1a4c 100644 --- a/LeetCode/problem009_PalindromeNumber.go +++ b/LeetCode/problem009_PalindromeNumber.go @@ -1,10 +1,15 @@ -/* Problem 9. Palindrome Number */ +/* +Problem 9. Palindrome Number +*/ + package leetcode import ( "math" ) +// Time complexity: O(log10​(n)) +// Space complexity: O(1) func isPalindrome(x int) bool { // Negative numbers are not palindromes if x < 0 { diff --git a/LeetCode/problem013_RomanToInteger.go b/LeetCode/problem013_RomanToInteger.go new file mode 100644 index 0000000..a063d42 --- /dev/null +++ b/LeetCode/problem013_RomanToInteger.go @@ -0,0 +1,31 @@ +/* +13. Roman to Integer +*/ + +package leetcode + +// Time complexity: O(n) +// Space complexity: O(1) +func romanToInteger(s string) int { + roman := map[byte]int{ + 'I': 1, + 'V': 5, + 'X': 10, + 'L': 50, + 'C': 100, + 'D': 500, + 'M': 1000, + } + n := len(s) + total := 0 + + for i := 0; i < n; i++ { + if i+1 < n && roman[s[i]] < roman[s[i+1]] { + total -= roman[s[i]] + } else { + total += roman[s[i]] + } + } + + return total +} diff --git a/LeetCode/problem013_RomanToInteger_test.go b/LeetCode/problem013_RomanToInteger_test.go new file mode 100644 index 0000000..3770db7 --- /dev/null +++ b/LeetCode/problem013_RomanToInteger_test.go @@ -0,0 +1,28 @@ +package leetcode + +import ( + "reflect" + "testing" +) + +func TestRomanToInteger(t *testing.T) { + var tests = []struct { + name string + input string + output int + }{ + {"case1", "III", 3}, + {"case2", "LVIII", 58}, + {"case3", "MCMXCIV", 1994}, + {"case4", "CMXCVIII", 998}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := romanToInteger(tt.input) + if !reflect.DeepEqual(result, tt.output) { + t.Errorf("romanToInteger(%v) = %v; want %v", tt.input, result, tt.output) + } + }) + } +}