diff --git a/.gitignore b/.gitignore index d26b33d..911c811 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ working +/.idea \ No newline at end of file diff --git a/LinkedList/206_ReverseLinkedList.go b/LinkedList/206_ReverseLinkedList.go new file mode 100644 index 0000000..aedd9dc --- /dev/null +++ b/LinkedList/206_ReverseLinkedList.go @@ -0,0 +1,15 @@ +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func reverseList(head *ListNode) *ListNode { + var current = head + var prev *ListNode + for current != nil { + current.Next, prev, current = prev, current, current.Next + } + return prev +} \ No newline at end of file diff --git a/LinkedList/21_MergeTwoSortedLists.go b/LinkedList/21_MergeTwoSortedLists.go new file mode 100644 index 0000000..00b8ba3 --- /dev/null +++ b/LinkedList/21_MergeTwoSortedLists.go @@ -0,0 +1,29 @@ +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { + + if l1 == nil && l2 == nil { + return nil + } + + if l1 == nil && l2 != nil { + return l2 + } + + if l1 != nil && l2 == nil { + return l1 + } + + if l1.Val <= l2.Val { + l1.Next = mergeTwoLists(l1.Next, l2) + return l1 + } else { + l2.Next = mergeTwoLists(l2.Next, l1) + return l2 + } +} \ No newline at end of file diff --git a/LinkedList/24_SwapNodesInPairs.go b/LinkedList/24_SwapNodesInPairs.go new file mode 100644 index 0000000..ec58c93 --- /dev/null +++ b/LinkedList/24_SwapNodesInPairs.go @@ -0,0 +1,22 @@ +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func swapPairs(head *ListNode) *ListNode { + virtualNode := &ListNode{ + Val: -1, + Next: head, + } + move := virtualNode + for move.Next != nil && move.Next.Next != nil { + a := move.Next + b := a.Next + move.Next, b.Next, a.Next = b, a, b.Next + move = a + } + + return virtualNode.Next +} \ No newline at end of file diff --git a/LinkedList/25_ReverseNodesIn-k-Group.go b/LinkedList/25_ReverseNodesIn-k-Group.go new file mode 100644 index 0000000..aca6f18 --- /dev/null +++ b/LinkedList/25_ReverseNodesIn-k-Group.go @@ -0,0 +1,50 @@ +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func reverseKGroup(head *ListNode, k int) *ListNode { + + if head == nil || k == 1 { + return head + } + + virtualNode := &ListNode{ + Val: -1, + Next: head, + } + before := virtualNode + count := 0 + for head != nil { + count += 1 + if count%k == 0 { + after := head.Next + before = reverseKNodes(before, after) + head = after + } else { + head = head.Next + } + } + return virtualNode.Next +} + +func reverseKNodes(before, after *ListNode) *ListNode { + var head = before.Next + var reversedHead, reversedTail, movePointer *ListNode + reversedHead = head + reversedTail = head + movePointer = head.Next + for movePointer != after { + movePointerNext := movePointer.Next + movePointer.Next = reversedHead + reversedHead = movePointer + movePointer = movePointerNext + } + + before.Next = reversedHead + reversedTail.Next = after + + return reversedTail +} diff --git a/Others/239_SlidingWindowMaximum-1.go b/Others/239_SlidingWindowMaximum-1.go new file mode 100644 index 0000000..f60e19f --- /dev/null +++ b/Others/239_SlidingWindowMaximum-1.go @@ -0,0 +1,22 @@ +func maxSlidingWindow(nums []int, k int) []int { + if len(nums) == 0 { + return nil + } + window := make([]int, 0) + result := make([]int, 0) + + for index, num := range nums { + if index >= k && window[0] <= index-k { + window = window[1:] + } + for len(window) != 0 && nums[window[len(window)-1]] <= num { + window = window[:len(window)-1] + } + window = append(window, index) + if index >= k-1 { + result = append(result, nums[window[0]]) + } + } + + return result +} \ No newline at end of file diff --git a/Others/239_SlidingWindowMaximum.go b/Others/239_SlidingWindowMaximum.go new file mode 100644 index 0000000..7ab5a5f --- /dev/null +++ b/Others/239_SlidingWindowMaximum.go @@ -0,0 +1,46 @@ + +import "container/heap" + +type MaxHep []int + +func (h MaxHep) Len() int { + return len(h) +} + +func (h MaxHep) Less(i, j int) bool { + return h[i] > h[j] +} + +func (h MaxHep) Swap(i, j int) { + h[i], h[j] = h[j], h[i] +} + +func (h *MaxHep) Push(x interface{}) { + *h = append(*h, x.(int)) +} + +func (h *MaxHep) Pop() interface{} { + size := h.Len() + bak := *h + x := bak[size-1] + *h = bak[0 : size-1] + return x +} + +func maxSlidingWindow(nums []int, k int) []int { + + if len(nums) == 0 { + return nil + } + result := make([]int, 0) + for index := range nums { + if index >= k-1 { + tmp := make([]int, k) + copy(tmp, nums[index-k+1:index+1]) + maxHeap := MaxHep(tmp) + heap.Init(&maxHeap) + result = append(result, heap.Pop(&maxHeap).(int)) + } + } + return result +} \ No newline at end of file diff --git a/Queue/703_KtkLargestElementInAStream.go b/Queue/703_KtkLargestElementInAStream.go new file mode 100644 index 0000000..59f8db5 --- /dev/null +++ b/Queue/703_KtkLargestElementInAStream.go @@ -0,0 +1,76 @@ +import ( + "container/heap" + "fmt" +) + +type Item struct { + value int + priority int + index int +} + +type PriorityQueue []*Item + +func (pq PriorityQueue) Len() int { + return len(pq) +} + +func (pq PriorityQueue) Less(i, j int) bool { + return pq[i].priority < pq[j].priority +} + +func (pq PriorityQueue) Swap(i, j int) { + pq[i], pq[j] = pq[j], pq[i] + pq[i].index = i + pq[j].index = j +} + +func (pq *PriorityQueue) Push(x interface{}) { + n := len(*pq) + item := x.(*Item) + item.index = n + *pq = append(*pq, item) +} + +func (pq *PriorityQueue) Pop() interface{} { + old := *pq + n := len(old) + item := old[n-1] + item.index = -1 + *pq = old[0 : n-1] + return item +} + +func (pq PriorityQueue) Print() { + for _, item := range pq { + fmt.Println(item.value) + } +} + +type KthLargest struct { + pq PriorityQueue + k int +} + +func Constructor(k int, nums []int) KthLargest { + kthLargest := KthLargest{ + pq: make(PriorityQueue, 0), + k: k, + } + heap.Init(&kthLargest.pq) + for _, num := range nums { + kthLargest.Add(num) + } + return kthLargest +} + +func (this *KthLargest) Add(val int) int { + heap.Push(&this.pq, &Item{ + value: val, + priority: val, + }) + if this.pq.Len() > this.k { + heap.Pop(&this.pq) + } + return this.pq[0].value +} \ No newline at end of file diff --git a/Stack/20_ValidParentheses.go b/Stack/20_ValidParentheses.go new file mode 100644 index 0000000..726b63a --- /dev/null +++ b/Stack/20_ValidParentheses.go @@ -0,0 +1,27 @@ + +func isValid(s string) bool { + stack := make([]rune, 0) + brackets := map[rune]rune{ + '(': ')', + '[': ']', + '{': '}', + } + + for _, bracket := range s { + if _, ok := brackets[bracket]; ok { + stack = append(stack, bracket) + continue + } + if _, ok := brackets[bracket]; !ok { + if len(stack) > 0 { + last := stack[len(stack)-1] + stack = stack[0 : len(stack)-1] + if brackets[last] == bracket { + continue + } + } + return false + } + } + return len(stack) == 0 +} \ No newline at end of file diff --git a/Stack/225_ImplementStackusingQueues.go b/Stack/225_ImplementStackusingQueues.go new file mode 100644 index 0000000..caa27c8 --- /dev/null +++ b/Stack/225_ImplementStackusingQueues.go @@ -0,0 +1,87 @@ +type Queue struct { + container []int +} + +func NewQueue() *Queue { + return &Queue{ + container: []int{}, + } +} + +func (q *Queue) Push(x int) { + q.container = append(q.container, x) +} + +func (q *Queue) Size() int { + return len(q.container) +} + +func (q *Queue) Pop() int { + pop := q.container[0] + if len(q.container) == 1 { + q.container = []int{} + } else { + q.container = q.container[1:len(q.container)] + } + return pop +} + +func (q *Queue) Peek() int { + return q.container[0] +} + +func (q *Queue) Empty() bool { + return len(q.container) == 0 +} + +type MyStack struct { + input, output *Queue +} + +/** Initialize your data structure here. */ +func Constructor() MyStack { + return MyStack{ + input: NewQueue(), + output: NewQueue(), + } +} + +/** Push element x onto stack. */ +func (this *MyStack) Push(x int) { + this.input.Push(x) +} + +func (this *MyStack) reverseInputThen2Output() { + middle := make([]int, 0) + for !this.input.Empty() { + middle = append(middle, this.input.Pop()) + } + for i, j := 0, len(middle)-1; i < j; i, j = i+1, j-1 { + middle[i], middle[j] = middle[j], middle[i] + } + + for !this.output.Empty() { + middle = append(middle, this.output.Pop()) + } + + for _, e := range middle { + this.output.Push(e) + } +} + +/** Removes the element on top of the stack and returns that element. */ +func (this *MyStack) Pop() int { + this.reverseInputThen2Output() + return this.output.Pop() +} + +/** Get the top element. */ +func (this *MyStack) Top() int { + this.reverseInputThen2Output() + return this.output.Peek() +} + +/** Returns whether the stack is empty. */ +func (this *MyStack) Empty() bool { + return this.output.Empty() && this.input.Empty() +} \ No newline at end of file diff --git a/Stack/232_ImplementQueueUsingStacks.go b/Stack/232_ImplementQueueUsingStacks.go new file mode 100644 index 0000000..2f41caf --- /dev/null +++ b/Stack/232_ImplementQueueUsingStacks.go @@ -0,0 +1,47 @@ +type MyQueue struct { + input []int + output []int +} + +/** Initialize your data structure here. */ +func Constructor() MyQueue { + return MyQueue{ + input: make([]int, 0), + output: make([]int, 0), + } +} + +/** Push element x to the back of queue. */ +func (this *MyQueue) Push(x int) { + this.input = append(this.input, x) +} + +/** Removes the element from in front of queue and returns that element. */ +func (this *MyQueue) Pop() int { + this.i2o() + last := this.output[len(this.output)-1] + this.output = this.output[0 : len(this.output)-1] + return last +} + +/** Get the front element. */ +func (this *MyQueue) Peek() int { + this.i2o() + return this.output[len(this.output)-1] +} + +func (this *MyQueue) i2o() { + if len(this.output) == 0 { + inputLen := len(this.input) + for i := 0; i < inputLen; i++ { + last := this.input[inputLen-i-1] + this.output = append(this.output, last) + this.input = this.input[0 : inputLen-i-1] + } + } +} + +/** Returns whether the queue is empty. */ +func (this *MyQueue) Empty() bool { + return len(this.input) == 0 && len(this.output) == 0 +} \ No newline at end of file