init
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package top100liked
|
||||
|
||||
// https://leetcode.cn/problems/two-sum/description/?envType=study-plan-v2&envId=top-100-liked
|
||||
|
||||
func twoSum(nums []int, target int) []int {
|
||||
for i, n := range nums {
|
||||
t := target - n
|
||||
for j, c := range nums[i+1:] {
|
||||
if t == c {
|
||||
return []int{i, i + j + 1}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return []int{}
|
||||
}
|
||||
|
||||
// func twoSum(nums []int, target int) []int {
|
||||
// map1 := make(map[int]int)
|
||||
// for k, v := range nums {
|
||||
// sub := target - v
|
||||
// if iv, ok := map1[sub]; ok {
|
||||
// return []int{iv, k}
|
||||
// }
|
||||
|
||||
// map1[v] = k
|
||||
// }
|
||||
|
||||
// return []int{}
|
||||
// }
|
||||
@@ -0,0 +1,79 @@
|
||||
package top100liked
|
||||
|
||||
// https://leetcode.cn/problems/group-anagrams/?envType=study-plan-v2&envId=top-100-liked
|
||||
|
||||
// 我居然能在算法题里写出这么离谱的代码
|
||||
|
||||
// type s2 struct {
|
||||
// Str string
|
||||
// CharMap map[rune]int
|
||||
// Used bool
|
||||
// }
|
||||
|
||||
// func (s *s2) Equal(target *s2) bool {
|
||||
// return maps.Equal(s.CharMap, target.CharMap)
|
||||
// }
|
||||
|
||||
// func newS2() *s2 {
|
||||
// return &s2{
|
||||
// Str: "",
|
||||
// CharMap: make(map[rune]int),
|
||||
// Used: false,
|
||||
// }
|
||||
// }
|
||||
|
||||
// func groupAnagrams(strs []string) [][]string {
|
||||
// l := make([]*s2, 0)
|
||||
// for _, str := range strs {
|
||||
// _s2 := newS2()
|
||||
// _s2.Str = str
|
||||
// for _, c := range str {
|
||||
// _s2.CharMap[c]++
|
||||
// }
|
||||
// l = append(l, _s2)
|
||||
// }
|
||||
// res := [][]string{}
|
||||
// for i, s2_1 := range l {
|
||||
// if s2_1.Used {
|
||||
// continue
|
||||
// }
|
||||
// r := []string{}
|
||||
// r = append(r, s2_1.Str)
|
||||
// s2_1.Used = true
|
||||
// for j := i + 1; j < len(l); j++ {
|
||||
// s2_2 := l[j]
|
||||
// if !s2_2.Used && s2_1.Equal(s2_2) {
|
||||
// r = append(r, s2_2.Str)
|
||||
// s2_2.Used = true
|
||||
// }
|
||||
// }
|
||||
// res = append(res, r)
|
||||
// }
|
||||
|
||||
// return res
|
||||
// }
|
||||
|
||||
func stringTurntobytes(s string) [26]byte {
|
||||
bytes := [26]byte{}
|
||||
for _, v := range s {
|
||||
bytes[v-'a']++
|
||||
}
|
||||
return bytes
|
||||
}
|
||||
|
||||
func groupAnagrams(strs []string) [][]string {
|
||||
m := make(map[[26]byte]int)
|
||||
res := make([][]string, 0)
|
||||
index := 0
|
||||
for _, str := range strs {
|
||||
bytes := stringTurntobytes(str)
|
||||
if _, ok := m[bytes]; ok {
|
||||
res[m[bytes]] = append(res[m[bytes]], str)
|
||||
} else {
|
||||
m[bytes] = index
|
||||
index++
|
||||
res = append(res, []string{str})
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package top100liked
|
||||
|
||||
import "testing"
|
||||
|
||||
// https://leetcode.cn/problems/longest-consecutive-sequence/description/?envType=study-plan-v2&envId=top-100-liked
|
||||
|
||||
func longestConsecutive(nums []int) int {
|
||||
numSet := map[int]bool{}
|
||||
for _, num := range nums {
|
||||
numSet[num] = true
|
||||
}
|
||||
res := 0
|
||||
for k := range numSet {
|
||||
if numSet[k-1] {
|
||||
continue
|
||||
}
|
||||
curNum := k
|
||||
curLen := 1
|
||||
for numSet[curNum+1] {
|
||||
curNum++
|
||||
curLen++
|
||||
}
|
||||
if curLen > res {
|
||||
res = curLen
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func TestS3(t *testing.T) {
|
||||
println(longestConsecutive([]int{100, 4, 200, 1, 3, 2}))
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package top100liked
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// https://leetcode.cn/problems/move-zeroes/?envType=study-plan-v2&envId=top-100-liked
|
||||
|
||||
// func moveZeroes(nums []int) {
|
||||
// for range nums {
|
||||
// for i := 0; i < len(nums); i++ {
|
||||
// if nums[i] == 0 {
|
||||
// if i+1 < len(nums) && nums[i+1] != 0 {
|
||||
// nums[i], nums[i+1] = nums[i+1], nums[i]
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
func moveZeroes(nums []int) {
|
||||
size := len(nums)
|
||||
left, right := 0, 0
|
||||
for right < size {
|
||||
if nums[right] != 0 {
|
||||
nums[left], nums[right] = nums[right], nums[left]
|
||||
left++
|
||||
}
|
||||
right++
|
||||
}
|
||||
}
|
||||
|
||||
func TestS4(t *testing.T) {
|
||||
input := []int{0, 1, 0, 3, 12}
|
||||
moveZeroes(input)
|
||||
fmt.Printf("%+v", input)
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package top100liked
|
||||
|
||||
import "testing"
|
||||
|
||||
// https://leetcode.cn/problems/container-with-most-water/?envType=study-plan-v2&envId=top-100-liked
|
||||
|
||||
func maxArea(height []int) int {
|
||||
l := 0
|
||||
r := len(height) - 1
|
||||
maxArea := 0
|
||||
for l < r {
|
||||
maxArea = max(maxArea, (r-l)*min(height[l], height[r]))
|
||||
if height[l] < height[r] {
|
||||
l++
|
||||
} else {
|
||||
r--
|
||||
}
|
||||
}
|
||||
return maxArea
|
||||
}
|
||||
|
||||
func TestS5(t *testing.T) {
|
||||
println(maxArea([]int{1, 8, 6, 2, 5, 4, 8, 3, 7}))
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package top100liked
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// https://leetcode.cn/problems/3sum/?envType=study-plan-v2&envId=top-100-liked
|
||||
|
||||
func threeSum(nums []int) [][]int {
|
||||
sort.Ints(nums)
|
||||
|
||||
n := len(nums)
|
||||
res := make([][]int, 0)
|
||||
|
||||
for i := 0; i < n-2; i++ {
|
||||
// 去重第一个数
|
||||
if i > 0 && nums[i] == nums[i-1] {
|
||||
continue
|
||||
}
|
||||
|
||||
// 剪枝:当前最小三数和已经大于 0
|
||||
if nums[i]+nums[i+1]+nums[i+2] > 0 {
|
||||
break
|
||||
}
|
||||
|
||||
// 剪枝:当前最大三数和仍然小于 0
|
||||
if nums[i]+nums[n-2]+nums[n-1] < 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
target := -nums[i]
|
||||
l, r := i+1, n-1
|
||||
|
||||
for l < r {
|
||||
sum := nums[l] + nums[r]
|
||||
|
||||
if sum == target {
|
||||
res = append(res, []int{nums[i], nums[l], nums[r]})
|
||||
|
||||
leftVal := nums[l]
|
||||
rightVal := nums[r]
|
||||
|
||||
// 命中后,左右都跳过重复值
|
||||
for l < r && nums[l] == leftVal {
|
||||
l++
|
||||
}
|
||||
for l < r && nums[r] == rightVal {
|
||||
r--
|
||||
}
|
||||
} else if sum < target {
|
||||
leftVal := nums[l]
|
||||
|
||||
// 当前 l 太小,且重复的 l 也一样太小,直接跳过
|
||||
for l < r && nums[l] == leftVal {
|
||||
l++
|
||||
}
|
||||
} else {
|
||||
rightVal := nums[r]
|
||||
|
||||
// 当前 r 太大,且重复的 r 也一样太大,直接跳过
|
||||
for l < r && nums[r] == rightVal {
|
||||
r--
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func TestS6(t *testing.T) {
|
||||
nums := []int{-100, -70, -60, 110, 120, 130, 160}
|
||||
fmt.Printf("%+v", threeSum(nums))
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package top100liked
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// https://leetcode.cn/problems/trapping-rain-water/?envType=study-plan-v2&envId=top-100-liked
|
||||
|
||||
func trap(height []int) (ans int) {
|
||||
n := len(height)
|
||||
if n == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
leftMax := make([]int, n)
|
||||
leftMax[0] = height[0]
|
||||
for i := 1; i < n; i++ {
|
||||
leftMax[i] = max(leftMax[i-1], height[i])
|
||||
}
|
||||
|
||||
rightMax := make([]int, n)
|
||||
rightMax[n-1] = height[n-1]
|
||||
for i := n - 2; i >= 0; i-- {
|
||||
rightMax[i] = max(rightMax[i+1], height[i])
|
||||
}
|
||||
|
||||
for i, h := range height {
|
||||
ans += min(leftMax[i], rightMax[i]) - h
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func TestS7(t *testing.T) {
|
||||
println(trap([]int{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}))
|
||||
// {4,2,0,3,2,5}
|
||||
}
|
||||
Reference in New Issue
Block a user