From 2bef8e248d73d295e616818c0be7b89e647b467f Mon Sep 17 00:00:00 2001 From: ersalazar Date: Mon, 13 May 2024 13:12:30 -0600 Subject: [PATCH 1/2] Create median-of-two-sorted-arrays.go --- go/median-of-two-sorted-arrays.go | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 go/median-of-two-sorted-arrays.go diff --git a/go/median-of-two-sorted-arrays.go b/go/median-of-two-sorted-arrays.go new file mode 100644 index 0000000..0c3d3b9 --- /dev/null +++ b/go/median-of-two-sorted-arrays.go @@ -0,0 +1,44 @@ +package leetcodesolutions + +func findMedianSortedArrays(nums1 []int, nums2 []int) float64 { + na := len(nums1) + nb := len(nums2) + n := na + nb + + var solve func(A, B []int, startA, endA, startB, endB, k int) float64 + solve = func(A, B []int, startA, endA, startB, endB, k int) float64 { + if startA > endA { + return float64(B[k-startA]) + } + if startB > endB { + return float64(A[k-startB]) + } + + indexA := (endA + startA) / 2 + indexB := (endB + startB) / 2 + + valueA := A[indexA] + valueB := B[indexB] + + if k > (indexA + indexB) { + if valueA <= valueB { + return solve(A, B, indexA+1, endA, startB, endB, k) + } else { + return solve(A, B, startA, endA, indexB+1, endB, k) + } + } else { + if valueA <= valueB { + return solve(A, B, startA, endA, startB, indexB-1, k) + } else { + return solve(A, B, startA, indexA-1, startB, endB, k) + } + } + } + + if n%2 == 0 { + return (solve(nums1, nums2, 0, na-1, 0, nb-1, n/2-1) + + solve(nums1, nums2, 0, na-1, 0, nb-1, n/2)) / 2.0 + } else { + return solve(nums1, nums2, 0, na-1, 0, nb-1, n/2) + } +} From a2d90bb82b3f7f59b433e4418df0e3b826addba8 Mon Sep 17 00:00:00 2001 From: ersalazar Date: Mon, 13 May 2024 13:16:13 -0600 Subject: [PATCH 2/2] Create regular-expression-matching.go --- go/regular-expression-matching.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 go/regular-expression-matching.go diff --git a/go/regular-expression-matching.go b/go/regular-expression-matching.go new file mode 100644 index 0000000..fe986d6 --- /dev/null +++ b/go/regular-expression-matching.go @@ -0,0 +1,17 @@ +package leetcodesolutions + +func isMatch(s string, p string) bool { + + if len(p) == 0 { + return len(s) == 0 + } + + firstCharMatch := len(s) > 0 && (p[0] == s[0] || p[0] == '.') + + if len(p) >= 2 && p[1] == '*' { + return isMatch(s, p[2:]) || (firstCharMatch && isMatch(s[1:], p)) + } else { + return firstCharMatch && isMatch(s[1:], p[1:]) + } + +}