Browse Source

Try leetcode-cli (quite useful)

dengxinyi 6 years ago
parent
commit
cfb5503af3

+ 22 - 0
easy/202.happy-number.go

@@ -0,0 +1,22 @@
+func sqr(x int) int {
+	return x * x
+}
+
+func isHappy(n int) bool {
+	occur := make(map[int]struct{})
+	for {
+		if _, ok := occur[n]; ok {
+			return false
+		} else if n == 1 {
+			return true
+		}
+		occur[n] = struct{}{}
+		var next int
+		for next = 0; n != 0; n /= 10 {
+			next += sqr(n % 10)
+		}
+		n = next
+	}
+	return false
+}
+

+ 18 - 0
easy/203.remove-linked-list-elements.go

@@ -0,0 +1,18 @@
+/**
+ * Definition for singly-linked list.
+ * type ListNode struct {
+ *     Val int
+ *     Next *ListNode
+ * }
+ */
+func removeElements(head *ListNode, val int) *ListNode {
+	dummy := &ListNode{0, head}
+	for curr := dummy; curr.Next != nil; {
+		if curr.Next.Val == val {
+			curr.Next = curr.Next.Next
+		} else {
+			curr = curr.Next
+		}
+	}
+	return dummy.Next;
+}

+ 23 - 0
easy/204.count-primes.go

@@ -0,0 +1,23 @@
+import (
+	"math"
+)
+
+func countPrimes(n int) int {
+	notPrime := make([]bool, n+1)
+	sqrtN := int(math.Sqrt(float64(n)))
+	for k := 2; k <= sqrtN; k++ {
+		if notPrime[k] {
+			continue
+		}
+		for m := 2*k; m < n; m += k {
+			notPrime[m] = true
+		}
+	}
+	cnt := 0
+	for i := 2; i < n; i++ {
+		if !notPrime[i] {
+			cnt++
+		}
+	}
+	return cnt
+}

+ 15 - 0
easy/205.isomorphic-strings.go

@@ -0,0 +1,15 @@
+func isIsomorphic(s string, t string) bool {
+	m1 := make(map[byte]byte)
+	m2 := make(map[byte]byte)
+	for i := range s {
+		b1, ok1 := m1[s[i]]
+		b2, ok2 := m2[t[i]]
+		if !ok1 && !ok2 {
+			m1[s[i]] = t[i]
+			m2[t[i]] = s[i]
+		} else if b1 != t[i] || b2 != s[i] {
+			return false
+		}
+	}
+	return true
+}

+ 37 - 0
easy/206.reverse-linked-list.go

@@ -0,0 +1,37 @@
+/**
+ * Definition for singly-linked list.
+ * type ListNode struct {
+ *     Val int
+ *     Next *ListNode
+ * }
+ */
+func reverseList(head *ListNode) *ListNode {
+	reverseRecurse(&head)
+	return head
+// return reverseIter(head, nil)
+}
+
+func reverseIter(head, tail *ListNode) *ListNode {
+	if head == nil {
+		return tail
+	}
+	curr := head
+	head = head.Next
+	curr.Next = tail
+	return reverseIter(head, curr)
+}
+
+func reverseRecurse(head **ListNode) {
+	first := *head
+	if first == nil {
+		return
+	}
+	rest := first.Next
+	if rest == nil {
+		return
+	}
+	reverseRecurse(&rest)
+	first.Next.Next = first
+	first.Next = nil
+	*head = rest
+}

+ 11 - 0
easy/217.contains-duplicate.go

@@ -0,0 +1,11 @@
+func containsDuplicate(nums []int) bool {
+	set := make(map[int]struct{})
+	for i := range nums {
+		if _, ok := set[nums[i]]; ok {
+			return true
+		} else {
+			set[nums[i]] = struct{}{}
+		}
+	}
+	return false
+}

+ 27 - 0
easy/219.contains-duplicate-ii.go

@@ -0,0 +1,27 @@
+func containsNearbyDuplicate(nums []int, k int) bool {
+	if k <= 0 {
+		return false
+	}
+	queue := make([]int, 0)
+	set := make(map[int]struct{})
+	i, n := 0, len(nums)
+	for i = 0; i < k && i < n; i++ {
+		queue = append(queue, nums[i])
+		if _, ok := set[nums[i]]; ok {
+			return true
+		} else {
+			set[nums[i]] = struct{}{}
+		}
+	}
+	for ; i < n; i++ {
+		queue = append(queue, nums[i])
+		if _, ok := set[nums[i]]; ok {
+			return true
+		} else {
+			delete(set, queue[0])
+			set[nums[i]] = struct{}{}
+			queue = queue[1:]
+		}
+	}
+	return false
+}

+ 90 - 0
easy/225.implement-stack-using-queues.go

@@ -0,0 +1,90 @@
+type MyStack struct {
+    Peek Queue
+	Body Queue
+}
+
+
+/** Initialize your data structure here. */
+func Constructor() MyStack {
+	return MyStack{make([]int, 0), make([]int, 0)}
+}
+
+
+/** Push element x onto stack. */
+func (this *MyStack) Push(x int)  {
+	this.Peek.Enqueue(x)
+	if this.Peek.Size() == 2 {
+		this.Body.Enqueue(this.Peek.Dequeue())
+	}
+}
+
+
+/** Removes the element on top of the stack and returns that element. */
+func (this *MyStack) Pop() int {
+	n := this.Body.Size()
+	top := this.Peek.Dequeue()
+	for i := 0; i < n-1; i++ {
+		this.Peek.Enqueue(this.Body.Dequeue())
+	}
+	this.Peek, this.Body = this.Body, this.Peek
+	return top
+}
+
+
+/** Get the top element. */
+func (this *MyStack) Top() int {
+	return this.Peek.Front()
+}
+
+
+/** Returns whether the stack is empty. */
+func (this *MyStack) Empty() bool {
+	return this.Peek.Empty()
+}
+
+
+/**
+ * Your MyStack object will be instantiated and called as such:
+ * obj := Constructor();
+ * obj.Push(x);
+ * param_2 := obj.Pop();
+ * param_3 := obj.Top();
+ * param_4 := obj.Empty();
+ */
+
+
+// Queue ...
+type Queue []int
+
+
+// Dequeue ...
+func (q *Queue) Dequeue() int {
+	front := (*q)[0]
+	*q = (*q)[1:]
+	return front
+}
+
+
+// Enqueue ...
+func (q *Queue) Enqueue(x int) {
+	*q = append(*q, x)
+}
+
+
+// Front ...
+func (q Queue) Front() int {
+	return q[0]
+}
+
+
+// Size ...
+func (q Queue) Size() int {
+	return len(q)
+}
+
+
+// Empty ...
+func (q Queue) Empty() bool {
+	return len(q) == 0
+}
+

+ 17 - 0
easy/226.invert-binary-tree.go

@@ -0,0 +1,17 @@
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func invertTree(root *TreeNode) *TreeNode {
+	if root == nil {
+		return nil
+	}
+	ltree, rtree := invertTree(root.Left), invertTree(root.Right)
+	root.Left, root.Right = rtree, ltree
+	return root
+}
+

+ 8 - 0
easy/231.power-of-two.go

@@ -0,0 +1,8 @@
+func isPowerOfTwo(n int) bool {
+	if n != 0 {
+		for n & 1 == 0 {
+			n >>= 1
+		}
+	}
+	return n == 1
+}

+ 85 - 0
easy/232.implement-queue-using-stacks.go

@@ -0,0 +1,85 @@
+type MyQueue struct {
+	Stack MyStack
+	Queue MyStack
+}
+
+
+/** Initialize your data structure here. */
+func Constructor() MyQueue {
+	return MyQueue{make([]int, 0), make([]int, 0)}
+}
+
+
+/** Push element x to the back of queue. */
+func (this *MyQueue) Push(x int)  {
+	for !this.Queue.Empty() {
+		this.Stack.Push(this.Queue.Pop())
+	}
+	this.Stack.Push(x)
+}
+
+
+/** Removes the element from in front of queue and returns that element. */
+func (this *MyQueue) Pop() int {
+	for !this.Stack.Empty() {
+		this.Queue.Push(this.Stack.Pop())
+	}
+	return this.Queue.Pop()
+}
+
+
+/** Get the front element. */
+func (this *MyQueue) Peek() int {
+	for !this.Stack.Empty() {
+		this.Queue.Push(this.Stack.Pop())
+	}
+	return this.Queue.Peek()
+}
+
+
+/** Returns whether the queue is empty. */
+func (this *MyQueue) Empty() bool {
+	return this.Stack.Empty() && this.Queue.Empty()
+}
+
+
+/**
+ * Your MyQueue object will be instantiated and called as such:
+ * obj := Constructor();
+ * obj.Push(x);
+ * param_2 := obj.Pop();
+ * param_3 := obj.Peek();
+ * param_4 := obj.Empty();
+ */
+
+
+type MyStack []int
+
+
+func (s MyStack) Size() int {
+	return len(s)
+}
+
+
+func (s MyStack) Empty() bool {
+	return len(s) == 0
+}
+
+
+func (s MyStack) Peek() int {
+	return s[len(s)-1]
+}
+
+
+func (s *MyStack) Push(x int) {
+	*s = append(*s, x)
+}
+
+
+func (s *MyStack) Pop() int {
+	n := len(*s)
+	top := (*s)[n-1]
+	*s = (*s)[:n-1]
+	return top
+}
+

+ 20 - 0
easy/234.palindrome-linked-list.go

@@ -0,0 +1,20 @@
+/**
+ * Definition for singly-linked list.
+ * type ListNode struct {
+ *     Val int
+ *     Next *ListNode
+ * }
+ */
+func isPalindrome(head *ListNode) bool {
+	l1, l2 := make([]int, 0), make([]int, 0)
+	n := 0
+	for curr := head; curr != nil; curr, n = curr.Next, n+1 {
+		l1, l2 = append(l1, curr.Val), append(l2, curr.Val)
+	}
+	for i := 0; i < n/2; i++ {
+		if l1[i] != l2[n-1-i] {
+			return false
+		}
+	}
+	return true
+}

+ 22 - 0
easy/235.lowest-common-ancestor-of-a-binary-search-tree.js

@@ -0,0 +1,22 @@
+/**
+ * Definition for a binary tree node.
+ * function TreeNode(val) {
+ *     this.val = val;
+ *     this.left = this.right = null;
+ * }
+ */
+/**
+ * @param {TreeNode} root
+ * @param {TreeNode} p
+ * @param {TreeNode} q
+ * @return {TreeNode}
+ */
+var lowestCommonAncestor = function(root, p, q) {
+	let curr = root
+	for (;;) {
+		if (p.val === curr.val || q.val === curr.val) return curr
+		if (curr.val < p.val && curr.val < q.val) curr = curr.right
+		else if (p.val < curr.val && q.val < curr.val) curr = curr.left
+		else return curr
+	}
+}