| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,19 @@ | |||
| 1 | + /** | ||
| 2 | + * Definition for a binary tree node. | ||
| 3 | + * public class TreeNode { | ||
| 4 | + * int val; | ||
| 5 | + * TreeNode left; | ||
| 6 | + * TreeNode right; | ||
| 7 | + * TreeNode(int x) { val = x; } | ||
| 8 | + * } | ||
| 9 | + */ | ||
| 10 | + class Solution { | ||
| 11 | + public int maxDepth(TreeNode root) { | ||
| 12 | + if(root == null){ | ||
| 13 | + return 0; | ||
| 14 | + } | ||
| 15 | + int left = maxDepth(root.left)+1; | ||
| 16 | + int right = maxDepth(root.right)+1; | ||
| 17 | + return Math.max(left,right); | ||
| 18 | + } | ||
| 19 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,53 @@ | |||
| 1 | + public class KthLargest { | ||
| 2 | + private int k; | ||
| 3 | + private int[] nums; | ||
| 4 | + | ||
| 5 | + public KthLargest(int k, int[] nums) { | ||
| 6 | + this.nums = new int[k + 1]; | ||
| 7 | + this.k = k; | ||
| 8 | + this.count = 0; | ||
| 9 | + for (int i = 0; i < nums.length; i++) { | ||
| 10 | + this.add(nums[i]); | ||
| 11 | + } | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + private int count; | ||
| 15 | + | ||
| 16 | + public int add(int val) { | ||
| 17 | + if (count == k && val < nums[1]) { | ||
| 18 | + return nums[1]; | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + if (count >= k) { | ||
| 22 | + nums[1] = val; | ||
| 23 | + int i = 1; | ||
| 24 | + while (2 * i <= count) { | ||
| 25 | + int j = 2; | ||
| 26 | + if (j < count && nums[j] > nums[j + 1]) j++; | ||
| 27 | + if (nums[1] <= nums[j]) break; | ||
| 28 | + swap(nums, i, j); | ||
| 29 | + i = j; | ||
| 30 | + } | ||
| 31 | + } else { | ||
| 32 | + nums[++count] = val; | ||
| 33 | + while (count > 1 && nums[count] < nums[count / 2]) { | ||
| 34 | + swap(nums, count, count / 2); | ||
| 35 | + count = count / 2; | ||
| 36 | + } | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + return nums[1]; | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + private void swap(int[] a, int i, int j) { | ||
| 43 | + int origin = a[j]; | ||
| 44 | + a[j] = a[i]; | ||
| 45 | + a[i] = origin; | ||
| 46 | + } | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + /** | ||
| 50 | + * Your KthLargest object will be instantiated and called as such: | ||
| 51 | + * KthLargest obj = new KthLargest(k, nums); | ||
| 52 | + * int param_1 = obj.add(val); | ||
| 53 | + */ | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments