This commit is contained in:
2026-07-23 16:56:50 +08:00
parent 124ad9afdf
commit 92784ffb0f
36 changed files with 2015 additions and 608 deletions
+25 -28
View File
@@ -5,39 +5,36 @@ import (
"testing"
)
// https://leetcode.cn/problems/valid-parentheses/description/?envType=study-plan-v2&envId=top-100-liked
// https://leetcode.cn/problems/median-of-two-sorted-arrays/?envType=study-plan-v2&envId=top-100-liked
func isValid(s string) bool {
stack := []rune{}
for _, c := range s {
if c == '(' || c == '[' || c == '{' {
stack = append(stack, c)
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
total := len(nums1) + len(nums2)
mid := total / 2
needAvg := total%2 == 0
c1, c2 := 0, 0
prev, cur := 0, 0
for i := 0; i <= mid; i++ {
prev = cur
if c1 < len(nums1) && (c2 >= len(nums2) || nums1[c1] <= nums2[c2]) {
cur = nums1[c1]
c1++
} else {
if len(stack) == 0 {
return false
}
pre := stack[len(stack)-1]
stack = stack[:len(stack)-1]
if c == ')' && pre != '(' {
return false
}
if c == ']' && pre != '[' {
return false
}
if c == '}' && pre != '{' {
return false
}
cur = nums2[c2]
c2++
}
}
return len(stack) == 0
if needAvg {
return float64(prev+cur) / 2
}
return float64(cur)
}
func Test(t *testing.T) {
fmt.Println(isValid("()"))
fmt.Println(isValid("()[]{}"))
fmt.Println(isValid("(]"))
fmt.Println(isValid("([])"))
fmt.Println(isValid("([)]"))
fmt.Println(isValid("("))
fmt.Println(isValid("]"))
// fmt.Println(findMedianSortedArrays([]int{1, 3}, []int{2}))
// fmt.Println(findMedianSortedArrays([]int{1, 2}, []int{3, 4}))
// fmt.Println(findMedianSortedArrays([]int{}, []int{1}))
// fmt.Println(findMedianSortedArrays([]int{}, []int{2, 3}))
// fmt.Println(findMedianSortedArrays([]int{1, 2, 3, 4, 5}, []int{6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}))
// fmt.Println(findMedianSortedArrays([]int{1, 2}, []int{-1, 3}))
fmt.Println(findMedianSortedArrays([]int{2, 2, 2, 2}, []int{2, 2, 2}))
}