Skip to content

Commit

Permalink
mergeKLists & so on
Browse files Browse the repository at this point in the history
  • Loading branch information
yannliao committed Aug 8, 2017
1 parent 20bcfb3 commit 2fc68a0
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 3 deletions.
20 changes: 17 additions & 3 deletions generateParenthesis.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
package leetcode

// func generateParenthesis(n int) []string {

// }
func generateParenthesis(n int) []string {
var res []string
place(&res, "", 0, 0, n)
return res
}
func place(res *[]string, str string, open int, close int, max int) {
if len(str) == max*2 {
*res = append(*res, str)
return
}
if open < max {
place(res, str+"(", open+1, close, max)
}
if close < open {
place(res, str+")", open, close+1, max)
}
}
46 changes: 46 additions & 0 deletions mergeKLists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package leetcode

/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func mergeKLists(lists []*ListNode) *ListNode {
t := new(ListNode)
res := t
flag := true
if len(lists) == 0 {
return res.Next
}
for flag {
j := 0
for i := 0; i < len(lists); i++ {
j = min(lists, j, i)
}
if lists[j] != nil {
v := lists[j]
lists[j] = v.Next
v.Next = nil
t.Next = v
t = t.Next
} else {
flag = false
}
}
return res.Next
}

func min(lists []*ListNode, j int, i int) int {
if lists[j] == nil {
return i
}
if lists[i] == nil {
return j
}
if lists[j].Val > lists[i].Val {
return i
}
return j
}
61 changes: 61 additions & 0 deletions mergeKListsWithPQ.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package leetcode

import "container/heap"

/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func mergeKListsWithPQ(lists []*ListNode) *ListNode {
if len(lists) < 1 {
return nil
}
var pq priority

for i := 0; i < len(lists); i++ {
if lists[i] != nil {
pq = append(pq, lists[i])
}
}
heap.Init(&pq)
res := new(ListNode)
t := res
for pq.Len() > 0 {
item := heap.Pop(&pq).(*ListNode)
t.Next = item
t = t.Next
if item.Next != nil {
heap.Push(&pq, item.Next)
}
}
return res.Next
}

type priority []*ListNode

func (pq priority) Len() int { return len(pq) }

func (pq priority) Less(i, j int) bool {
return pq[i].Val < pq[j].Val
}

func (pq priority) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
}

func (pq *priority) Push(x interface{}) {
// n := len(*pq)
item := x.(*ListNode)
*pq = append(*pq, item)
}

func (pq *priority) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
*pq = old[0 : n-1]
return item
}
15 changes: 15 additions & 0 deletions removeDuplicates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package leetcode

func removeDuplicates(nums []int) int {
if len(nums) <= 1 {
return len(nums)
}
id := 1
for i := 1; i < len(nums); i++ {
if nums[i] != nums[i-1] {
nums[id] = nums[i]
id++
}
}
return id
}
31 changes: 31 additions & 0 deletions reverseKGroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package leetcode

/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reverseKGroup(head *ListNode, k int) *ListNode {
pivot := head
var count int
for count < k && pivot != nil {
pivot = pivot.Next
count++
}
if count == k {
if pivot != nil {
pivot = reverseKGroup(pivot, k)
}
for count > 0 {
count--
tmp := head.Next
head.Next = pivot
pivot = head
head = tmp
}
head = pivot
}
return head
}
20 changes: 20 additions & 0 deletions swapPairs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package leetcode

/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func swapPairs(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
last := head.Next.Next
temp := head
head = head.Next
head.Next = temp
temp.Next = swapPairs(last)
return head
}

0 comments on commit 2fc68a0

Please sign in to comment.