dengxinyi 6 years ago
parent
commit
dcb53be829
2 changed files with 95 additions and 0 deletions
  1. 72 0
      medium/622.design-circular-queue.go
  2. 23 0
      medium/623.add-one-row-to-tree.go

+ 72 - 0
medium/622.design-circular-queue.go

@@ -0,0 +1,72 @@
+type MyCircularQueue struct {
+	queue  []int
+	size   int
+	length int
+	beg    int
+	end    int
+}
+
+/** Initialize your data structure here. Set the size of the queue to be k. */
+func Constructor(k int) (queue MyCircularQueue) {
+	queue.queue = make([]int, k)
+	queue.size = k
+	return
+}
+
+/** Insert an element into the circular queue. Return true if the operation is successful. */
+func (this *MyCircularQueue) EnQueue(value int) bool {
+	if this.IsFull() {
+		return false
+	}
+	this.queue[this.end] = value
+	this.end = (this.end + 1) % this.size
+	this.length++
+	return true
+}
+
+/** Delete an element from the circular queue. Return true if the operation is successful. */
+func (this *MyCircularQueue) DeQueue() bool {
+	if this.IsEmpty() {
+		return false
+	}
+	this.beg = (this.beg + 1) % this.size
+	this.length--
+	return true
+}
+
+/** Get the front item from the queue. */
+func (this *MyCircularQueue) Front() int {
+	if this.IsEmpty() {
+		return -1
+	}
+	return this.queue[this.beg]
+}
+
+/** Get the last item from the queue. */
+func (this *MyCircularQueue) Rear() int {
+	if this.IsEmpty() {
+		return -1
+	}
+	return this.queue[(this.end+this.size-1)%this.size]
+}
+
+/** Checks whether the circular queue is empty or not. */
+func (this *MyCircularQueue) IsEmpty() bool {
+	return this.length == 0
+}
+
+/** Checks whether the circular queue is full or not. */
+func (this *MyCircularQueue) IsFull() bool {
+	return this.length == this.size
+}
+
+/**
+ * Your MyCircularQueue object will be instantiated and called as such:
+ * obj := Constructor(k);
+ * param_1 := obj.EnQueue(value);
+ * param_2 := obj.DeQueue();
+ * param_3 := obj.Front();
+ * param_4 := obj.Rear();
+ * param_5 := obj.IsEmpty();
+ * param_6 := obj.IsFull();
+ */

+ 23 - 0
medium/623.add-one-row-to-tree.go

@@ -0,0 +1,23 @@
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func addOneRow(root *TreeNode, v int, d int) *TreeNode {
+	if root == nil {
+		return root
+	} else if d == 1 {
+		return &TreeNode{v, root, nil}
+	} else if d == 2 {
+		root.Left = &TreeNode{v, root.Left, nil}
+		root.Right = &TreeNode{v, nil, root.Right}
+		return root
+	}
+	root.Left = addOneRow(root.Left, v, d-1)
+	root.Right = addOneRow(root.Right, v, d-1)
+	return root
+}
+