Skip to content

Commit 77b465a

Browse files
committed
feat: add solutions
1 parent 2b5e78a commit 77b465a

File tree

18 files changed

+449
-1
lines changed

18 files changed

+449
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package binary_tree
2+
3+
// https://leetcode.cn/problems/symmetric-tree/description/?envType=study-plan-v2&envId=top-100-liked
4+
// 给你一个二叉树的根节点 root , 检查它是否轴对称。
5+
6+
// 如果一个树的左子树与右子树镜像对称,那么这个树是对称的。
7+
//
8+
//因此,该问题可以转化为:两个树在什么情况下互为镜像?
9+
//
10+
//如果同时满足下面的条件,两个树互为镜像:
11+
//
12+
//它们的两个根结点具有相同的值
13+
//每个树的右子树都与另一个树的左子树镜像对称
14+
//
15+
//
16+
//我们可以实现这样一个递归函数,通过「同步移动」两个指针的方法来遍历这棵树,
17+
//p 指针和 q 指针一开始都指向这棵树的根,随后 p 右移时,q 左移,p 左移时,
18+
//q 右移。每次检查当前 p 和 q 节点的值是否相等,如果相等再判断左右子树是否对称。
19+
20+
func isSymmetric(root *TreeNode) bool {
21+
return check(root, root)
22+
}
23+
24+
func check(p, q *TreeNode) bool {
25+
if p == nil && q == nil {
26+
return true
27+
}
28+
if p == nil || q == nil {
29+
return false
30+
}
31+
return check(p.Left, q.Right) && check(p.Right, q.Left) && p.Val == q.Val
32+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package add_two_numbers
2+
3+
/**
4+
* Definition for singly-linked list.
5+
* type ListNode struct {
6+
* Val int
7+
* Next *ListNode
8+
* }
9+
*/
10+
11+
type ListNode struct {
12+
Val int
13+
Next *ListNode
14+
}
15+
16+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
17+
return nil
18+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package calculator_lcci
2+
3+
import "strings"
4+
5+
// 给定一个包含正整数、加(+)、减(-)、乘(*)、除(/)的算数表达式(括号除外),计算其结果。
6+
// 表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格 。 整数除法仅保留整数部分。
7+
// 示例 1:
8+
// 输入: "3+2*2"
9+
// 输出: 7
10+
// 示例 2:
11+
// 输入: " 3/2 "
12+
// 输出: 1
13+
// 示例 3:
14+
// 输入: " 3+5 / 2 "
15+
// 输出: 5
16+
// 说明:
17+
// 你可以假设所给定的表达式都是有效的。
18+
// 请不要使用内置的库函数 eval。
19+
20+
// Calculate 百度二面
21+
func Calculate(s string) int {
22+
var stack []int
23+
s = strings.Replace(s, " ", "", -1)
24+
var operator = '+'
25+
var num = 0
26+
for i, c := range s {
27+
if c >= '0' && c <= '9' {
28+
num = num*10 + int(c-'0')
29+
}
30+
if c < '0' || c > '9' || i == len(s)-1 {
31+
switch operator {
32+
case '+':
33+
stack = append(stack, num)
34+
case '-':
35+
stack = append(stack, -num)
36+
case '*':
37+
stack[len(stack)-1] *= num
38+
case '/':
39+
stack[len(stack)-1] /= num
40+
}
41+
num = 0
42+
operator = c
43+
}
44+
}
45+
result := 0
46+
for _, v := range stack {
47+
result += v
48+
}
49+
return result
50+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package calculator_lcci
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestCalculate(t *testing.T) {
9+
strings := []string{"1+ 2*3- 4/2"}
10+
for _, s := range strings {
11+
fmt.Println(Calculate(s))
12+
}
13+
}
14+
15+
func TestNum(t *testing.T) {
16+
var s = "111"
17+
var num int32
18+
for _, c := range s {
19+
if c >= '0' && c <= '9' {
20+
num = num*10 + c - '0'
21+
}
22+
}
23+
fmt.Println(num)
24+
}

solutions/climbing-stairs/climbing-stairs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package climbing_stairs
22

33
/***
4-
假设你正在爬楼梯。需要 n 阶你才能到达楼顶
4+
假设你正在爬楼梯。需要 n阶你才能到达楼顶
55
66
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
77
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package container_with_most_water
2+
3+
// 盛最多水的容器
4+
// https://leetcode.cn/problems/container-with-most-water/description/?envType=study-plan-v2&envId=top-100-liked
5+
// 给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。
6+
// 找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
7+
// 返回容器可以储存的最大水量。
8+
// 说明:你不能倾斜容器。
9+
// 输入:[1,8,6,2,5,4,8,3,7]
10+
// 输出:49
11+
// 解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
12+
// 示例 2:
13+
// 输入:height = [1,1]
14+
// 输出:1
15+
// 提示
16+
// n == height.length
17+
// 2 <= n <= 105
18+
// 0 <= height[i] <= 104
19+
func maxArea(height []int) int {
20+
left, right := 0, len(height)-1
21+
maxArea := 0
22+
for left < right {
23+
width := right - left
24+
h := 0
25+
// 在最大容器问题中,移动较低一侧指针的核心原因在于:当前容器的面积受限于较矮的边, 而移动较高的边无法突破这一限制。
26+
// 通过移动较矮侧的指针,我们试图寻找更高的边,以抵消宽度减少的影响,从而可能获得更大的面积
27+
// 面积公式与短板效应:面积由 宽度(两指针间距) × 高度(两指针中较矮的值)
28+
if height[left] < height[right] {
29+
h = height[left]
30+
left++
31+
} else {
32+
h = height[right]
33+
right--
34+
}
35+
if area := width * h; area > maxArea {
36+
maxArea = area
37+
}
38+
}
39+
return maxArea
40+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package container_with_most_water
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestMaxArea(t *testing.T) {
9+
arrs := []int{1, 8, 6, 2, 5, 4, 8, 3, 7}
10+
fmt.Println(maxArea(arrs))
11+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package group_anagrams
2+
3+
import "sort"
4+
5+
// GroupAnagrams https://leetcode.cn/problems/group-anagrams/description/?envType=study-plan-v2&envId=top-100-liked
6+
// 字母异位词分组
7+
// 给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。
8+
//
9+
// 字母异位词 是由重新排列源单词的所有字母得到的一个新单词。
10+
//
11+
// 示例 1:
12+
//
13+
// 输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
14+
// 输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
15+
// 示例 2:
16+
//
17+
// 输入: strs = [""]
18+
// 输出: [[""]]
19+
// 示例 3:
20+
//
21+
// 输入: strs = ["a"]
22+
// 输出: [["a"]]
23+
//
24+
// 提示:
25+
//
26+
// 1 <= strs.length <= 104
27+
// 0 <= strs[i].length <= 100
28+
// strs[i] 仅包含小写字母
29+
// 相同字母排列得到的单词,放到一个数组中,通过排序实现
30+
func GroupAnagrams(strs []string) [][]string {
31+
wordMap := make(map[string][]string)
32+
for _, s := range strs {
33+
charArr := []byte(s)
34+
sort.Slice(charArr, func(i, j int) bool {
35+
return charArr[i] < charArr[j]
36+
})
37+
wordMap[string(charArr)] = append(wordMap[string(charArr)], s)
38+
}
39+
res := make([][]string, 0)
40+
for _, w := range wordMap {
41+
res = append(res, w)
42+
}
43+
return res
44+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package group_anagrams
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestGroupAnagrams(t *testing.T) {
9+
strs := []string{"eat", "tea", "tan", "ate", "nat", "bat"}
10+
res := GroupAnagrams(strs)
11+
fmt.Println(res)
12+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package longest_consecutive_sequence
2+
3+
// https://leetcode.cn/problems/longest-consecutive-sequence/description/?envType=study-plan-v2&envId=top-100-liked
4+
// 给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。
5+
//
6+
//请你设计并实现时间复杂度为 O(n) 的算法解决此问题。
7+
//
8+
//示例 1:
9+
//
10+
//输入:nums = [100,4,200,1,3,2]
11+
//输出:4
12+
//解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
13+
//示例 2:
14+
//
15+
//输入:nums = [0,3,7,2,5,8,4,6,0,1]
16+
//输出:9
17+
18+
// LongestConsecutive map记录数字是否存在,存在就一直用map往后判断,记录最大长度
19+
func LongestConsecutive(nums []int) int {
20+
numMap := make(map[int]bool)
21+
longestLength := 0
22+
for _, num := range nums {
23+
numMap[num] = true
24+
}
25+
26+
for num := range numMap {
27+
if !numMap[num-1] {
28+
currentNum := num
29+
currentLength := 1
30+
for numMap[currentNum+1] {
31+
currentNum++
32+
currentLength++
33+
}
34+
if currentLength > longestLength {
35+
longestLength = currentLength
36+
}
37+
}
38+
}
39+
return longestLength
40+
}

0 commit comments

Comments
 (0)