Skip to content

25_ReverseNodesIn-k-Group.go #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
working
/.idea
15 changes: 15 additions & 0 deletions LinkedList/206_ReverseLinkedList.go
Original file line number Diff line number Diff line change
@@ -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
}
29 changes: 29 additions & 0 deletions LinkedList/21_MergeTwoSortedLists.go
Original file line number Diff line number Diff line change
@@ -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
}
}
22 changes: 22 additions & 0 deletions LinkedList/24_SwapNodesInPairs.go
Original file line number Diff line number Diff line change
@@ -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
}
50 changes: 50 additions & 0 deletions LinkedList/25_ReverseNodesIn-k-Group.go
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 22 additions & 0 deletions Others/239_SlidingWindowMaximum-1.go
Original file line number Diff line number Diff line change
@@ -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
}
46 changes: 46 additions & 0 deletions Others/239_SlidingWindowMaximum.go
Original file line number Diff line number Diff line change
@@ -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
}
76 changes: 76 additions & 0 deletions Queue/703_KtkLargestElementInAStream.go
Original file line number Diff line number Diff line change
@@ -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
}
27 changes: 27 additions & 0 deletions Stack/20_ValidParentheses.go
Original file line number Diff line number Diff line change
@@ -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
}
87 changes: 87 additions & 0 deletions Stack/225_ImplementStackusingQueues.go
Original file line number Diff line number Diff line change
@@ -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()
}
Loading