utils

package
v0.0.0-...-8d3c4b6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2021 License: MIT Imports: 5 Imported by: 442

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EPSILON float64 = 0.00001

Functions

func Abs

func Abs(a int) int

func DeleteFromIntSlice

func DeleteFromIntSlice(a []int, target int) []int

DeleteFromIntSlice delete target from slice a

func FloatEquals

func FloatEquals(a, b float64) bool

func InorderTraversal

func InorderTraversal(root *TreeNode) []int

InorderTraversal uses Morris Traversal Algorithm time complexity is O(n) and space complexity is O(1)

func IsEqual

func IsEqual(l, r *TreeNode) bool

func IsEqualList

func IsEqualList(h1, h2 *ListNode) bool

IsEqualList checks l1 == l2

func LevelOrderTravesal

func LevelOrderTravesal(root *TreeNode) []int

func LowerBound

func LowerBound(nums []int, target int) int

LowerBound returns the lowest pos for target in the sorted array

func Max

func Max(a, b int) int

func Maxf

func Maxf(a, b float64) float64

func Min

func Min(a, b int) int

func NumOfOnes

func NumOfOnes(a int) int

func PostorderTraversal

func PostorderTraversal(root *TreeNode) []int

PostorderTraversal uses Morris Traversal Algorithm time complexity is O(n) and space complexity is O(1)

func Pow

func Pow(base, exp int) int

func PreorderTraversal

func PreorderTraversal(root *TreeNode) []int

PreorderTraversal uses Morris Traversal Algorithm time complexity is O(n) and space complexity is O(1)

func Upperbound

func Upperbound(nums []int, target int) int

Upperbound find the proper posion for target in nums, time complexity O(log(N))

Types

type DNode

type DNode struct {
	Val        interface{}
	Prev, Next *DNode
}

DNode represents the double linked node

type Deque

type Deque struct {
	// contains filtered or unexported fields
}

Deque is the double ended queue

func NewDeque

func NewDeque() *Deque

func (*Deque) Back

func (deq *Deque) Back() interface{}

func (*Deque) Front

func (deq *Deque) Front() interface{}

func (*Deque) IsEmpty

func (deq *Deque) IsEmpty() bool

IsEmpty check deque is empty or not

func (*Deque) PopBack

func (deq *Deque) PopBack() interface{}

func (*Deque) PopFront

func (deq *Deque) PopFront() interface{}

func (*Deque) PushBack

func (deq *Deque) PushBack(val interface{})

PushBack push val at back

func (*Deque) PushFront

func (deq *Deque) PushFront(val interface{})

PushFront push val at front

func (*Deque) String

func (deq *Deque) String() string

type Graph

type Graph struct {
	// contains filtered or unexported fields
}

Graph represent a graph

func ConstructAdjacencyList

func ConstructAdjacencyList(n int, conns [][]int, directed bool) *Graph

ConstructAdjacencyList create an adjacency list based on the connections array

func (*Graph) NumofSccs

func (gp *Graph) NumofSccs() int

NumofSccs returns the number of Strong Connected Componnets in the graph

type Int64PriorityQueue

type Int64PriorityQueue []int64

func (Int64PriorityQueue) Len

func (pq Int64PriorityQueue) Len() int

func (Int64PriorityQueue) Less

func (pq Int64PriorityQueue) Less(i, j int) bool

func (*Int64PriorityQueue) Pop

func (pq *Int64PriorityQueue) Pop() interface{}

func (*Int64PriorityQueue) Push

func (pq *Int64PriorityQueue) Push(x interface{})

func (Int64PriorityQueue) Swap

func (pq Int64PriorityQueue) Swap(i, j int)

func (Int64PriorityQueue) Top

func (pq Int64PriorityQueue) Top() int64

type ListNode

type ListNode struct {
	Val  int
	Next *ListNode
}

ListNode is a node structure in a list

func ConstructFromSlice

func ConstructFromSlice(s []int) *ListNode

ConstructFromSlice returns a *ListNode built by s

func ConstructFromSliceWithCycle

func ConstructFromSliceWithCycle(s []int) *ListNode

ConstructFromSliceWithCycle returns a ListNode with cycle.

func ReverseList

func ReverseList(head *ListNode) *ListNode

ReverseList return the reversed list

func (*ListNode) String

func (l *ListNode) String() string

type ListNodePriorityQueue

type ListNodePriorityQueue []*ListNode

func (ListNodePriorityQueue) Len

func (pq ListNodePriorityQueue) Len() int

func (ListNodePriorityQueue) Less

func (pq ListNodePriorityQueue) Less(i, j int) bool

func (*ListNodePriorityQueue) Pop

func (pq *ListNodePriorityQueue) Pop() interface{}

func (*ListNodePriorityQueue) Push

func (pq *ListNodePriorityQueue) Push(x interface{})

func (ListNodePriorityQueue) Swap

func (pq ListNodePriorityQueue) Swap(i, j int)

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

Queue is FIFO

func NewQueue

func NewQueue() *Queue

NewQueue returns a *Queue

func (*Queue) Enroll

func (q *Queue) Enroll(value interface{})

Enroll put a value in the queue

func (*Queue) IsEmpty

func (q *Queue) IsEmpty() bool

IsEmpty check if the q is empty or not

func (*Queue) Pull

func (q *Queue) Pull() interface{}

Pull from the head

func (*Queue) Size

func (q *Queue) Size() int

Size returns the size of q

type RTrie

type RTrie struct {
	Root *TrieNode
	// contains filtered or unexported fields
}

func NewRTrie

func NewRTrie(r int) *RTrie

func (*RTrie) Contains

func (rt *RTrie) Contains(query string) bool

func (*RTrie) Get

func (rt *RTrie) Get(query string) interface{}

func (*RTrie) Put

func (rt *RTrie) Put(key string, value interface{})

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

Stack is LIFO

func NewStack

func NewStack() *Stack

NewStack returns a brand new stack

func (*Stack) IsEmpty

func (s *Stack) IsEmpty() bool

IsEmpty returns true if the stack is empty

func (*Stack) Len

func (s *Stack) Len() int

Len returns a stack's length

func (*Stack) Pop

func (s *Stack) Pop() interface{}

Pop pops the top element in the stack

func (*Stack) Push

func (s *Stack) Push(value interface{})

Push pushes a value to the stack

func (*Stack) String

func (s *Stack) String() string

func (*Stack) Top

func (s *Stack) Top() interface{}

Top returns the top element in the stack

type StringHeap

type StringHeap []string

A StringHeap is a min-heap of strings

func NewStringHeap

func NewStringHeap() *StringHeap

NewStringHeap returns an empty StringHeap

func (StringHeap) Len

func (h StringHeap) Len() int

func (StringHeap) Less

func (h StringHeap) Less(i, j int) bool

func (*StringHeap) Pop

func (h *StringHeap) Pop() interface{}

func (*StringHeap) Push

func (h *StringHeap) Push(x interface{})

func (StringHeap) Swap

func (h StringHeap) Swap(i, j int)

type TSTNode

type TSTNode struct {
	Value               interface{}
	Cb                  byte
	Left, Middle, Right *TSTNode
}

TSTNode represents the ternary search trie node

type TernarySearchTrie

type TernarySearchTrie struct {
	Root *TSTNode
}

func NewTST

func NewTST() *TernarySearchTrie

func (*TernarySearchTrie) Contains

func (t *TernarySearchTrie) Contains(key string) bool

func (*TernarySearchTrie) Get

func (t *TernarySearchTrie) Get(key string) interface{}

func (*TernarySearchTrie) LongestPrefixOf

func (t *TernarySearchTrie) LongestPrefixOf(query string) string

func (*TernarySearchTrie) Put

func (t *TernarySearchTrie) Put(key string, val interface{})

func (*TernarySearchTrie) WithPrefix

func (t *TernarySearchTrie) WithPrefix(query string) []interface{}

type TreeNode

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

func ConstructTree

func ConstructTree(s []int) *TreeNode

we use math.MinInt32 to represents null

type TrieNode

type TrieNode struct {
	Value    interface{}
	Children []*TrieNode
}

type TuplePriorityQueue

type TuplePriorityQueue [][3]int

func (TuplePriorityQueue) Len

func (pq TuplePriorityQueue) Len() int

func (TuplePriorityQueue) Less

func (pq TuplePriorityQueue) Less(i, j int) bool

func (*TuplePriorityQueue) Pop

func (pq *TuplePriorityQueue) Pop() interface{}

func (*TuplePriorityQueue) Push

func (pq *TuplePriorityQueue) Push(x interface{})

func (TuplePriorityQueue) Swap

func (pq TuplePriorityQueue) Swap(i, j int)

func (TuplePriorityQueue) Top

func (pq TuplePriorityQueue) Top() [3]int

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL