u
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package top100liked
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// https://leetcode.cn/problems/median-of-two-sorted-arrays/?envType=study-plan-v2&envId=top-100-liked
|
||||
|
||||
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 {
|
||||
cur = nums2[c2]
|
||||
c2++
|
||||
}
|
||||
}
|
||||
if needAvg {
|
||||
return float64(prev+cur) / 2
|
||||
}
|
||||
return float64(cur)
|
||||
}
|
||||
|
||||
func Test(t *testing.T) {
|
||||
// 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}))
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package top100liked
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// https://leetcode.cn/problems/valid-parentheses/description/?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)
|
||||
} 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
|
||||
}
|
||||
}
|
||||
}
|
||||
return len(stack) == 0
|
||||
}
|
||||
|
||||
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("]"))
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package top100liked
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// https://leetcode.cn/problems/min-stack/description/?envType=study-plan-v2&envId=top-100-liked
|
||||
|
||||
type MinStack struct {
|
||||
slice []int
|
||||
minSlice []int // 记录每个层级的最小值
|
||||
}
|
||||
|
||||
func Constructor() MinStack {
|
||||
return MinStack{}
|
||||
}
|
||||
|
||||
func (this *MinStack) Push(value int) {
|
||||
if len(this.slice) == 0 {
|
||||
this.minSlice = append(this.minSlice, value)
|
||||
} else {
|
||||
this.minSlice = append(this.minSlice, min(this.minSlice[len(this.minSlice)-1], value))
|
||||
}
|
||||
this.slice = append(this.slice, value)
|
||||
}
|
||||
|
||||
func (this *MinStack) Pop() {
|
||||
this.slice = this.slice[:len(this.slice)-1]
|
||||
this.minSlice = this.minSlice[:len(this.minSlice)-1]
|
||||
}
|
||||
|
||||
func (this *MinStack) Top() int {
|
||||
return this.slice[len(this.slice)-1]
|
||||
}
|
||||
|
||||
func (this *MinStack) GetMin() int {
|
||||
return this.minSlice[len(this.minSlice)-1]
|
||||
}
|
||||
|
||||
func Test1(t *testing.T) {
|
||||
minStack := Constructor()
|
||||
minStack.Push(-2)
|
||||
minStack.Push(0)
|
||||
minStack.Push(-3)
|
||||
fmt.Println(minStack.GetMin())
|
||||
minStack.Pop()
|
||||
fmt.Println(minStack.Top())
|
||||
fmt.Println(minStack.GetMin())
|
||||
}
|
||||
|
||||
func Test2(t *testing.T) {
|
||||
minStack := Constructor()
|
||||
minStack.Push(1)
|
||||
minStack.Push(2)
|
||||
fmt.Println(minStack.Top())
|
||||
fmt.Println(minStack.GetMin())
|
||||
minStack.Pop()
|
||||
fmt.Println(minStack.GetMin())
|
||||
fmt.Println(minStack.Top())
|
||||
}
|
||||
Reference in New Issue
Block a user