-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yannliao
committed
Aug 8, 2017
1 parent
20bcfb3
commit 2fc68a0
Showing
6 changed files
with
190 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |