mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
Merge pull request #1514 from AronJudge/master
0977. 有序数组的平方 0018. 四数之和 0347 前K个高频元素 0104 二叉树最大深度
This commit is contained in:
@ -140,6 +140,11 @@ class Solution {
|
|||||||
|
|
||||||
for (int i = 0; i < nums.length; i++) {
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
|
||||||
|
// nums[i] > target 直接返回, 剪枝操作
|
||||||
|
if (nums[i] > 0 && nums[i] > target) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
if (i > 0 && nums[i - 1] == nums[i]) {
|
if (i > 0 && nums[i - 1] == nums[i]) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -294,14 +294,13 @@ class solution {
|
|||||||
/**
|
/**
|
||||||
* 递归法
|
* 递归法
|
||||||
*/
|
*/
|
||||||
public int maxdepth(treenode root) {
|
public int maxDepth(TreeNode root) {
|
||||||
if (root == null) {
|
if (root == null) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int leftdepth = maxdepth(root.left);
|
int leftDepth = maxDepth(root.left);
|
||||||
int rightdepth = maxdepth(root.right);
|
int rightDepth = maxDepth(root.right);
|
||||||
return math.max(leftdepth, rightdepth) + 1;
|
return Math.max(leftDepth, rightDepth) + 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -311,23 +310,23 @@ class solution {
|
|||||||
/**
|
/**
|
||||||
* 迭代法,使用层序遍历
|
* 迭代法,使用层序遍历
|
||||||
*/
|
*/
|
||||||
public int maxdepth(treenode root) {
|
public int maxDepth(TreeNode root) {
|
||||||
if(root == null) {
|
if(root == null) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
deque<treenode> deque = new linkedlist<>();
|
Deque<TreeNode> deque = new LinkedList<>();
|
||||||
deque.offer(root);
|
deque.offer(root);
|
||||||
int depth = 0;
|
int depth = 0;
|
||||||
while (!deque.isempty()) {
|
while (!deque.isEmpty()) {
|
||||||
int size = deque.size();
|
int size = deque.size();
|
||||||
depth++;
|
depth++;
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
treenode poll = deque.poll();
|
TreeNode node = deque.poll();
|
||||||
if (poll.left != null) {
|
if (node.left != null) {
|
||||||
deque.offer(poll.left);
|
deque.offer(node.left);
|
||||||
}
|
}
|
||||||
if (poll.right != null) {
|
if (node.right != null) {
|
||||||
deque.offer(poll.right);
|
deque.offer(node.right);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,13 +141,10 @@ class Solution {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
|
Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
|
||||||
// 根据map的value值正序排,相当于一个小顶堆
|
// 根据map的value值,构建于一个大顶堆(o1 - o2: 小顶堆, o2 - o1 : 大顶堆)
|
||||||
PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>((o1, o2) -> o1.getValue() - o2.getValue());
|
PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>((o1, o2) -> o2.getValue() - o1.getValue());
|
||||||
for (Map.Entry<Integer, Integer> entry : entries) {
|
for (Map.Entry<Integer, Integer> entry : entries) {
|
||||||
queue.offer(entry);
|
queue.offer(entry);
|
||||||
if (queue.size() > k) {
|
|
||||||
queue.poll();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for (int i = k - 1; i >= 0; i--) {
|
for (int i = k - 1; i >= 0; i--) {
|
||||||
result[i] = queue.poll().getKey();
|
result[i] = queue.poll().getKey();
|
||||||
|
@ -106,6 +106,7 @@ class Solution {
|
|||||||
int index = result.length - 1;
|
int index = result.length - 1;
|
||||||
while (left <= right) {
|
while (left <= right) {
|
||||||
if (nums[left] * nums[left] > nums[right] * nums[right]) {
|
if (nums[left] * nums[left] > nums[right] * nums[right]) {
|
||||||
|
// 正数的相对位置是不变的, 需要调整的是负数平方后的相对位置
|
||||||
result[index--] = nums[left] * nums[left];
|
result[index--] = nums[left] * nums[left];
|
||||||
++left;
|
++left;
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user