dengxinyi 6 years ago
parent
commit
8164466fcb

+ 40 - 0
easy/427.construct-quad-tree.java

@@ -0,0 +1,40 @@
+/*
+// Definition for a QuadTree node.
+class Node {
+    public boolean val;
+    public boolean isLeaf;
+    public Node topLeft;
+    public Node topRight;
+    public Node bottomLeft;
+    public Node bottomRight;
+
+    public Node() {}
+
+    public Node(boolean _val,boolean _isLeaf,Node _topLeft,Node _topRight,Node _bottomLeft,Node _bottomRight) {
+        val = _val;
+        isLeaf = _isLeaf;
+        topLeft = _topLeft;
+        topRight = _topRight;
+        bottomLeft = _bottomLeft;
+        bottomRight = _bottomRight;
+    }
+};
+*/
+class Solution {
+    public Node construct(int[][] grid) {
+        return construct(grid, 0, 0, grid.length);
+    }
+
+    private Node construct(int[][] grid, int x, int y, int n) {
+        if (n == 1)
+            return new Node(grid[y][x] == 1, true, null, null, null, null);
+        n /= 2;
+        Node tl = construct(grid, x, y, n);
+        Node tr = construct(grid, x + n, y, n);
+        Node bl = construct(grid, x, y + n, n);
+        Node br = construct(grid, x + n, y + n, n);
+        if (tl.isLeaf && tr.isLeaf && bl.isLeaf && br.isLeaf && tl.val == tr.val && tr.val == bl.val && bl.val == br.val)
+            return new Node(tl.val, true, null, null, null, null);
+        return new Node(true, false, tl, tr, bl, br);
+    }
+}

+ 38 - 0
easy/429.n-ary-tree-level-order-traversal.java

@@ -0,0 +1,38 @@
+/*
+// Definition for a Node.
+class Node {
+    public int val;
+    public List<Node> children;
+
+    public Node() {}
+
+    public Node(int _val,List<Node> _children) {
+        val = _val;
+        children = _children;
+    }
+};
+*/
+class Solution {
+    public List<List<Integer>> levelOrder(Node root) {
+        LinkedList<List<Integer>> list = new LinkedList<>();
+        if (root == null) return list;
+        LinkedList<Node> currLV = new LinkedList<>();
+        currLV.add(root);
+        while (currLV.size() != 0) {
+            LinkedList<Integer> li = new LinkedList<>();
+            for (Node node : currLV) {
+                li.add(node.val);
+            }
+            list.add(li);
+            LinkedList<Node> nextLV = new LinkedList<>();
+            while (currLV.size() != 0) {
+                Node curr = currLV.poll();
+                for (Node node : curr.children) {
+                    if (node != null) nextLV.add(node);
+                }
+            }
+            currLV = nextLV;
+        }
+        return list;
+    }
+}

+ 23 - 0
medium/406.queue-reconstruction-by-height.go

@@ -0,0 +1,23 @@
+type ints [][]int
+
+func (is ints) Len() int { return len(is) }
+func (is ints) Less(i, j int) bool {
+	if is[i][0] == is[j][0] {
+		return is[i][1] < is[j][1]
+	}
+	return is[i][0] > is[j][0]
+}
+func (is ints) Swap(i, j int) { is[i], is[j] = is[j], is[i] }
+
+func reconstructQueue(people [][]int) [][]int {
+	sort.Sort(ints(people)) // Sort + insert
+	n := len(people)
+	for i := 0; i < n; i++ {
+		if people[i][1] != i {
+			tmp := people[i]
+			copy(people[tmp[1]+1:i+1], people[tmp[1]:i])
+			people[tmp[1]] = tmp
+		}
+	}
+	return people
+}

+ 18 - 0
medium/413.arithmetic-slices.go

@@ -0,0 +1,18 @@
+func numberOfArithmeticSlices(A []int) (res int) {
+	n := len(A)
+	if n <= 2 {
+		return 0
+	}
+	diff, cnt := A[1]-A[0], 2
+	for i := 2; i < n; i++ {
+		if A[i]-A[i-1] == diff {
+			cnt++
+		} else {
+			// 1 + ... + cnt-2
+			res += (cnt - 1) * (cnt - 2) / 2
+			diff = A[i] - A[i-1]
+			cnt = 2
+		}
+	}
+	return res + (cnt-1)*(cnt-2)/2
+}

+ 30 - 0
medium/416.partition-equal-subset-sum.go

@@ -0,0 +1,30 @@
+type ints []int
+
+func (is ints) Len() int           { return len(is) }
+func (is ints) Less(i, j int) bool { return is[j] < is[i] }
+func (is ints) Swap(i, j int)      { is[i], is[j] = is[j], is[i] }
+
+func canPartition(nums []int) (ans bool) {
+	sum := 0
+	for _, i := range nums {
+		sum += i
+	}
+	if sum&1 == 1 {
+		return false
+	}
+	sort.Sort(ints(nums))
+	dfs(nums, len(nums), sum/2, 0, 0, 0, &ans)
+	return
+}
+
+func dfs(nums []int, n, sum, i, p1, p2 int, ans *bool) {
+	if *ans == true || sum < p1 || sum < p2 {
+		return
+	} else if i == n {
+		*ans = p1 == p2
+		return
+	}
+	dfs(nums, n, sum, i+1, p1+nums[i], p2, ans)
+	dfs(nums, n, sum, i+1, p1, p2+nums[i], ans)
+}
+

+ 31 - 0
medium/417.pacific-atlantic-water-flow.go

@@ -0,0 +1,31 @@
+var dx []int = []int{1, 0, -1, 0}
+var dy []int = []int{0, -1, 0, 1}
+
+func pacificAtlantic(matrix [][]int) (res [][]int) {
+	m, n := len(matrix), len(matrix[0])
+	ocean := make([][]int, m)
+	for i := 0; i < m; i++ {
+		ocean[i] = make([]int, n)
+	}
+	for i := 0; i < m; i++ { // b10: pacific, b01: atlantic
+		ocean[i][0] |= 2
+		ocean[i][n-1] |= 1
+	}
+	for i := 0; i < n; i++ {
+		ocean[0][i] |= 2
+		ocean[m-1][i] |= 1
+	}
+	dfs(matrix, 0, 0, ocean)
+	for i := 0; i < m; i++ {
+		for j := 0; j < m; j++ {
+			if ocean[i][j] == 3 {
+				res = append(res, []int{j, i})
+			}
+		}
+	}
+	return
+}
+
+func dfs(matrix [][]int, x, y int, ocean [][]int) {
+	
+}