Skip to content

Commit e2a010c

Browse files
committed
udpate
1 parent 22ef8d8 commit e2a010c

12 files changed

+191
-102
lines changed

102.二叉树的层序遍历.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -41,45 +41,6 @@ func main() {
4141
fmt.Println(x)
4242
}
4343

44-
/*
45-
func levelOrder(root *TreeNode) [][]int {
46-
result := make([][]int, 0)
47-
if root == nil{
48-
return result
49-
}
50-
51-
queue := []*TreeNode{root}
52-
53-
for len(queue) > 0 {
54-
size := len(queue)
55-
56-
levelData := make([]int, 0)
57-
for i := 0; i < size; i++ {
58-
// get node
59-
node := queue[0] //永远取行首的node
60-
// append value
61-
levelData = append(levelData, node.Val)
62-
63-
// 每次pop一个node
64-
queue = queue[1:]
65-
// put children into queue
66-
if node.Left != nil{
67-
queue = append(queue, node.Left)
68-
}
69-
if node.Right != nil{
70-
queue = append(queue, node.Right)
71-
}
72-
}
73-
//queue = queue[size:]
74-
if len(levelData) > 0 {
75-
result = append(result, levelData)
76-
}
77-
}
78-
return result
79-
80-
}
81-
82-
*/
8344

8445
// @lc code=start
8546
/**

136.只出现一次的数字.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
*/
66

77
/*
8-
1. 任何数和 00 做异或运算,结果仍然是原来的数,即 a \oplus 0=aa⊕0=a。
9-
2. 任何数和其自身做异或运算,结果是 00,即 a \oplus a=0a⊕a=0。
10-
3. 异或运算满足交换律和结合律,即 a \oplus b \oplus a=b \oplus a \oplus a=b \oplus (a \oplus a)=b \oplus0=ba⊕b⊕a=b⊕a⊕a=b⊕(a⊕a)=b⊕0=b
8+
1. 任何数和 0 做异或运算,结果仍然是原来的数,即 a ^ 0 = a
9+
2. 任何数和其自身做异或运算,结果是 0,即 a ^ a = 0
10+
3. 异或运算满足交换律和结合律
1111
*/
1212

1313
// @lc code=start

169.多数元素.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* @lc app=leetcode.cn id=169 lang=golang
3+
*
4+
* [169] 多数元素
5+
*
6+
* https://leetcode-cn.com/problems/majority-element/description/
7+
*
8+
* algorithms
9+
* Easy (66.57%)
10+
* Likes: 1376
11+
* Dislikes: 0
12+
* Total Accepted: 487.6K
13+
* Total Submissions: 732.1K
14+
* Testcase Example: '[3,2,3]'
15+
*
16+
* 给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。
17+
*
18+
* 你可以假设数组是非空的,并且给定的数组总是存在多数元素。
19+
*
20+
*
21+
*
22+
* 示例 1:
23+
*
24+
*
25+
* 输入:[3,2,3]
26+
* 输出:3
27+
*
28+
* 示例 2:
29+
*
30+
*
31+
* 输入:[2,2,1,1,1,2,2]
32+
* 输出:2
33+
*
34+
*
35+
*
36+
*
37+
* 进阶:
38+
*
39+
*
40+
* 尝试设计时间复杂度为 O(n)、空间复杂度为 O(1) 的算法解决此问题。
41+
*
42+
*
43+
*/
44+
45+
// @lc code=start
46+
// Boyer-Moore 投票算法
47+
func majorityElement(nums []int) int {
48+
count, candidate := 0, -1
49+
for i := 0; i < len(nums); i++ {
50+
if count == 0 {
51+
candidate = nums[i]
52+
}
53+
54+
if nums[i] == candidate {
55+
count++
56+
} else {
57+
count--
58+
}
59+
}
60+
61+
return candidate
62+
}
63+
64+
// @lc code=end
65+

2.两数相加.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* @lc app=leetcode.cn id=2 lang=golang
3+
*
4+
* [2] 两数相加
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* type ListNode struct {
11+
* Val int
12+
* Next *ListNode
13+
* }
14+
*/
15+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
16+
17+
var (
18+
head, tail *ListNode
19+
carry = 0
20+
)
21+
22+
for l1 != nil || l2 != nil {
23+
n1, n2 := 0, 0
24+
if l1 != nil {
25+
n1 = l1.Val
26+
// 移动l1
27+
l1 = l1.Next
28+
}
29+
if l2 != nil {
30+
n2 = l2.Val
31+
// 移动l2
32+
l2 = l2.Next
33+
}
34+
sum := n1 + n2 + carry
35+
left := sum % 10
36+
carry = sum / 10
37+
38+
if head == nil {
39+
// 初始化tail, head
40+
head = &ListNode{
41+
Val: left,
42+
}
43+
tail = head
44+
} else {
45+
node := &ListNode{
46+
Val: left,
47+
}
48+
// 移动tail
49+
tail.Next = node
50+
tail = tail.Next
51+
}
52+
}
53+
54+
if carry > 0 {
55+
node := &ListNode{
56+
Val: carry,
57+
}
58+
tail.Next = node
59+
60+
}
61+
return head
62+
63+
}
64+
65+
// @lc code=end
66+

2192.有向无环图中一个节点的所有祖先.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

239.滑动窗口最大值.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* @lc app=leetcode.cn id=239 lang=golang
3+
*
4+
* [239] 滑动窗口最大值
5+
*/
6+
// @lc code=start
7+
8+
var a []int
9+
10+
type hp struct{ sort.IntSlice }
11+
12+
func (h hp) Less(i, j int) bool { return a[h.IntSlice[i]] > a[h.IntSlice[j]] }
13+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
14+
func (h *hp) Pop() interface{} {
15+
a := h.IntSlice
16+
v := a[len(a)-1]
17+
h.IntSlice = a[:len(a)-1]
18+
return v
19+
}
20+
21+
/*
22+
构造一个max heap, 队列元素存储原数组元素的index
23+
24+
1. 每移动1位, push(j)进heap
25+
2. 检查heap[0]下标是否越界, 越界就执行pop()
26+
3. heap[0]即为当前位置最大值
27+
*/
28+
func maxSlidingWindow(nums []int, k int) []int {
29+
a = nums
30+
q := &hp{make([]int, k)}
31+
for i := 0; i < k; i++ {
32+
q.IntSlice[i] = i
33+
}
34+
heap.Init(q)
35+
36+
n := len(nums)
37+
ans := make([]int, 1, n-k+1)
38+
ans[0] = nums[q.IntSlice[0]]
39+
for i := k; i < n; i++ {
40+
heap.Push(q, i)
41+
for q.IntSlice[0] <= i-k {
42+
heap.Pop(q)
43+
}
44+
ans = append(ans, nums[q.IntSlice[0]])
45+
}
46+
return ans
47+
}
48+
49+
// @lc code=end
50+

257.二叉树的所有路径.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func binaryTreePaths(root *TreeNode) []string {
2929
return
3030
}
3131
current := fmt.Sprintf("%s%d->", prefix, n.Val)
32-
// 到达leaf node
32+
// 只有leaf node才写将自己的path放入result
3333
if n.Left == nil && n.Right == nil {
3434
result = append(result, current[:len(current)-2])
3535
}
Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
package main
2-
3-
func main() {
4-
removeDuplicates([]int{1, 2, 2, 3, 3, 4})
5-
6-
}
7-
81
/*
92
* @lc app=leetcode.cn id=26 lang=golang
103
*
@@ -28,30 +21,6 @@ func removeDuplicates(nums []int) int {
2821
return left
2922

3023
}
31-
func swap(nums []int, x, y int) {
32-
nums[x], nums[y] = nums[y], nums[x]
33-
}
3424

3525
// @lc code=end
3626

37-
// 冒泡移动到最后
38-
func bak(nums []int) int {
39-
40-
size := len(nums)
41-
42-
// 从第2个开始向前比较
43-
i := 1
44-
for i < size {
45-
if nums[i] == nums[i-1] {
46-
// move to last
47-
for j := i; j < size-1; j++ {
48-
swap(nums, j, j+1)
49-
}
50-
// exclude last
51-
size--
52-
}
53-
i++
54-
}
55-
return size
56-
57-
}

387.字符串中的第一个唯一字符.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ func firstUniqChar(s string) int {
1010
cache := [26]int{}
1111

1212
for i := range s {
13-
cache[s[i]-'a']++
13+
cache[s[i]-byte('a')]++
1414
}
1515
for i := range s {
16-
if cache[s[i]-'a'] == 1 {
16+
if cache[s[i]-byte('a')] == 1 {
1717
return i
1818
}
1919
}

470.用-rand-7-实现-rand-10.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)