Skip to content

Commit

Permalink
tip(go/patterns): del folder src
Browse files Browse the repository at this point in the history
  • Loading branch information
TIGERB committed Nov 4, 2020
1 parent a7146f7 commit d649b8d
Show file tree
Hide file tree
Showing 82 changed files with 11,658 additions and 0 deletions.
4 changes: 4 additions & 0 deletions go/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com
golang.org
gopkg.in
tingle
76 changes: 76 additions & 0 deletions go/algorithm/heap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import "fmt"

// 树:节点node、根节点root、子节点child、叶节点leaf
// 二叉树:每个节点最多有两个节点
// 完全二叉树
// 满二叉树

// bigTopHeap 大顶堆
func bigTopHeap(input []int) []int {
aroundFind := false
lenInput := len(input)
i := 0
for {
if 2*(i+1) > lenInput {
if !aroundFind {
return input
}
i = 0
aroundFind = false
continue
}
if input[i] < input[2*i+1] {
tmp := input[i]
input[i] = input[2*i+1]
input[2*i+1] = tmp
aroundFind = true
}
if 2*(i+1) < lenInput && input[i] < input[2*i+2] {
tmp := input[i]
input[i] = input[2*i+2]
input[2*i+2] = tmp
aroundFind = true
}
i++
}
return input
}

// lessTopHeap 小顶堆
func lessTopHeap(input []int) []int {
aroundFind := false
lenInput := len(input)
i := 0
for {
if 2*(i+1) > lenInput {
if !aroundFind {
return input
}
i = 0
aroundFind = false
continue
}
if input[i] > input[2*i+1] {
tmp := input[i]
input[i] = input[2*i+1]
input[2*i+1] = tmp
aroundFind = true
}
if 2*(i+1) < lenInput && input[i] > input[2*i+2] {
tmp := input[i]
input[i] = input[2*i+2]
input[2*i+2] = tmp
aroundFind = true
}
i++
}
return input
}

func main() {
input := []int{2, 7, 26, 25, 19, 17, 1, 90, 3, 36}
fmt.Println(bigTopHeap(input))
fmt.Println(lessTopHeap(input))
}
24 changes: 24 additions & 0 deletions go/algorithm/leetcode/1207.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

func main() {

}

func uniqueOccurrences(arr []int) bool {
resMap := map[int]int{}
for _, item := range arr {
if _, ok := resMap[item]; ok {
resMap[item]++
continue
}
resMap[item] = 1
}
resNewMap := map[int]int{}
for _, item := range resMap {
if _, ok := resNewMap[item]; ok {
return false
}
resNewMap[item] = 1
}
return true
}
86 changes: 86 additions & 0 deletions go/algorithm/leetcode/144-tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import "fmt"

type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}

func main() {
two := &TreeNode{Val: 3}
one := &TreeNode{Val: 2, Left: two}
root := &TreeNode{Val: 1, Left: one}
fmt.Println(preorderTraversal(root))
}

// type Stack struct {
// data []
// }

/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func preorderTraversal(root *TreeNode) []int {
res := []int{}
if root == nil {
return res
}

// 初始化栈
stack := []*TreeNode{}
// 入栈
pushStack := func(stack []*TreeNode, val *TreeNode) []*TreeNode {
return append(stack, val)
}
// 出栈
popStack := func(stack []*TreeNode) ([]*TreeNode, *TreeNode) {
len := len(stack)
if len == 0 {
return []*TreeNode{}, nil
}
val := stack[len-1]
return append(stack[:0], stack[:len-1]...), val
}

// 根节点入栈
stack = pushStack(stack, root)
node := &TreeNode{}
for {
if len(stack) == 0 {
return res
}
stack, node = popStack(stack)
res = append(res, node.Val)
if node.Right != nil {
stack = pushStack(stack, node.Right)
}
if node.Left != nil {
stack = pushStack(stack, node.Left)
}
}
}

/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func preorderTraversalRecursion(root *TreeNode) []int {
if root == nil {
return []int{}
}
res := []int{root.Val}
res = append(res, preorderTraversal(root.Left)...)
res = append(res, preorderTraversal(root.Right)...)
return res
}
106 changes: 106 additions & 0 deletions go/algorithm/leetcode/347-topk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package leetcode

// ================
// leetcode题解
// 347. 前 K 个高频元素
// ================

// 给定一个非空的整数数组,返回其中出现频率前 k 高的元素。

// 示例 1:

// 输入: nums = [1,1,1,2,2,3], k = 2
// 输出: [1,2]
// 示例 2:

// 输入: nums = [1], k = 1
// 输出: [1]

// 提示:

// 你可以假设给定的 k 总是合理的,且 1 ≤ k ≤ 数组中不相同的元素的个数。
// 你的算法的时间复杂度必须优于 O(n log n) , n 是数组的大小。
// 题目数据保证答案唯一,换句话说,数组中前 k 个高频元素的集合是唯一的。
// 你可以按任意顺序返回答案。

// 来源:力扣(LeetCode)
// 链接:https://leetcode-cn.com/problems/top-k-frequent-elements
// 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

// 解法一:快排
func topKFrequent(nums []int, k int) []int {
res := []int{}
if len(nums) == 0 || k == 0 {
return res
}
numsMap := map[int]int{}
for _, v := range nums {
if _, ok := numsMap[v]; ok {
numsMap[v]++
continue
}
numsMap[v] = 1
}

resSlice := []int{}
for _, v := range numsMap {
resSlice = append(resSlice, v)
}

// resSlice 快排
resSliceLen := len(resSlice)
resSlice = quick(resSlice, 0, resSliceLen)

// 输出后k个(最大值)
resSlice = append(resSlice[:0], resSlice[resSliceLen-k:]...)

resSliceMap := map[int]byte{}
for _, v := range resSlice {
resSliceMap[v] = '0'
}
for k, v := range numsMap {
if _, ok := resSliceMap[v]; ok {
res = append(res, k)
}
}

return res
}

func quick(resSlice []int, left int, right int) (res []int) {
if left >= right {
return resSlice
}
base := left
i := right
j := left
for {
if i <= j {
break
}
for i = right; i > base; i-- {
if resSlice[i] < resSlice[base] {
tmp := resSlice[i]
resSlice[i] = resSlice[base]
resSlice[base] = tmp
base = i
break
}
}

for j = left; j < base; j++ {
if resSlice[j] > resSlice[base] {
tmp := resSlice[j]
resSlice[j] = resSlice[base]
resSlice[base] = tmp
base = j
break
}
}
}

resSlice = quick(resSlice, left, base-1)
resSlice = quick(resSlice, base+1, right)

return resSlice
}
61 changes: 61 additions & 0 deletions go/algorithm/leetcode/55-2-balance-tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。

//

// 示例 1:

// 给定二叉树 [3,9,20,null,null,15,7]

// 3
// / \
// 9 20
// / \
// 15 7
// 返回 true 。

// 示例 2:

// 给定二叉树 [1,2,2,3,3,null,null,4,4]

// 1
// / \
// 2 2
// / \
// 3 3
// / \
// 4 4
// 返回 false 。

//

// 限制:

// 1 <= 树的结点个数 <= 10000
// 注意:本题与主站 110 题相同:https://leetcode-cn.com/problems/balanced-binary-tree/

// 来源:力扣(LeetCode)
// 链接:https://leetcode-cn.com/problems/ping-heng-er-cha-shu-lcof
// 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}

/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isBalanced(root *TreeNode) bool {
if root == nil {
return false
}
if root.Left != nil {
deep
}
}
Loading

0 comments on commit d649b8d

Please sign in to comment.