Skip to content

Commit bf83d15

Browse files
committed
.
1 parent b1dc124 commit bf83d15

File tree

199 files changed

+10633
-208
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

199 files changed

+10633
-208
lines changed

Hot100/README.md

-4
This file was deleted.

README.md

+237-33
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int solve(int num) {
3+
int count = 0;
4+
5+
while(num >= 5){
6+
num -= 5;
7+
count += 2;
8+
num += 2;
9+
}
10+
11+
while(num >= 3){
12+
num -= 3;
13+
count += 1;
14+
num += 1;
15+
}
16+
17+
return count;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 TreeNode swapTree(TreeNode root) {
12+
dfs(root);
13+
return root;
14+
}
15+
16+
private void dfs(TreeNode root){
17+
if(root == null){
18+
return;
19+
}
20+
TreeNode temp = root.left;
21+
root.left = root.right;
22+
root.right = temp;
23+
24+
dfs(root.left);
25+
dfs(root.right);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Solution {
2+
public int cardGame(int[] cardA, int[] cardB) {
3+
Arrays.sort(cardA);
4+
Arrays.sort(cardB); // 李的牌,排不排序无所谓
5+
6+
boolean[] used = new boolean[cardA.length]; // 未使用:false,已使用:true
7+
8+
int winCount = 0;
9+
10+
for(int i = 0; i < cardB.length; i++){
11+
int target = cardB[i]; // 李的出牌
12+
int index = binarySearchLarger(cardA, target, used);
13+
if(target < cardA[index]){
14+
winCount++;
15+
}
16+
}
17+
18+
return winCount;
19+
}
20+
21+
// 如果是找 min(num > target), 则使用left。如果是找 max(num < target),则将下面的换成 if( cardA[mid] >= target, right = mid - 1, 并使用 right
22+
private int binarySearchLarger(int[] cardA, int target, boolean[] used){ // 找 argmin(cardA[index] > target),若不存在,则返回最小可利用的元素
23+
int left = 0;
24+
int right = cardA.length - 1;
25+
26+
while(left <= right){ // 找 argmin(cardA[index] > target)
27+
int mid = (left + right) / 2;
28+
if(cardA[mid] <= target){
29+
left = mid + 1;
30+
}else{
31+
right = mid - 1;
32+
}
33+
}
34+
35+
while(left < cardA.length && used[left] == true){ // 若被使用过,则向右寻找
36+
left++;
37+
}
38+
39+
if(left == cardA.length){ // 若越界了,说明没有比 target 大的(未被使用过的)元素了
40+
left = 0; // 因为没有比 target 大的元素了,那就拿一个最小的元素和 target 去对比吧 (田忌赛马)
41+
while(left < cardA.length && used[left] == true){
42+
left++;
43+
}
44+
}
45+
46+
used[left] = true; // 标志为使用过
47+
48+
return left;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* 正确思路,按最短桥的思路,从三个国家往外扩展,3个bfs
3+
* O(row * col)
4+
*/
5+
6+
/**
7+
* 方法超时
8+
* O(row^2 * col^2)
9+
*/
10+
class Solution {
11+
public int miniumDistance(int[][] grid) {
12+
int minDistance = Integer.MAX_VALUE;
13+
14+
int row = grid.length;
15+
int col = grid[0].length;
16+
17+
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 上下左右
18+
19+
for(int i = 0; i < row; i++){
20+
for(int j = 0; j < col; j++){
21+
if(grid[i][j] == 0){
22+
int distance = 1;
23+
int curDistanceA = Integer.MAX_VALUE; // 当前公共领土到三个国家的 max(min(distance))
24+
int curDistanceB = Integer.MAX_VALUE;
25+
int curDistanceC = Integer.MAX_VALUE;
26+
27+
boolean[][] visited = new boolean[row][col]; // 判断是否访问过
28+
29+
Queue<int[]> queue = new LinkedList<>();
30+
31+
queue.offer(new int[]{i, j});
32+
33+
visited[i][j] = true;
34+
35+
pointA: while(!queue.isEmpty()){
36+
int size = queue.size();
37+
while(size-- > 0){
38+
int[] loc = queue.poll();
39+
int x = loc[0];
40+
int y = loc[1];
41+
for(int[] direction: directions){
42+
int new_x = x + direction[0];
43+
int new_y = y + direction[1];
44+
if(new_x >= 0 && new_x < row && new_y >= 0 && new_y < col){
45+
if(visited[new_x][new_y] == false){
46+
queue.offer(new int[]{new_x, new_y});
47+
visited[new_x][new_y] = true;
48+
49+
if(grid[new_x][new_y] == 1 && curDistanceA == Integer.MAX_VALUE){
50+
curDistanceA = distance;
51+
}else if(grid[new_x][new_y] == 2 && curDistanceB == Integer.MAX_VALUE){
52+
curDistanceB = distance;
53+
}else if(grid[new_x][new_y] == 3 && curDistanceC == Integer.MAX_VALUE){
54+
curDistanceC = distance;
55+
}
56+
if(curDistanceA != Integer.MAX_VALUE && curDistanceB != Integer.MAX_VALUE && curDistanceC != Integer.MAX_VALUE){
57+
break pointA;
58+
}
59+
}
60+
}
61+
}
62+
63+
}
64+
distance++;
65+
}
66+
67+
distance = Math.max(Math.max(curDistanceA, curDistanceB), curDistanceC); // 最大距离
68+
minDistance = Math.min(minDistance, distance); // 最小
69+
}
70+
}
71+
}
72+
73+
return minDistance;
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode linkedListGame(ListNode head, int num) {
11+
// 没做出来
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public boolean checkBracketSequence(String origin) {
3+
int left = 0; // 左括号的剩余个数
4+
5+
int len = origin.length();
6+
7+
for(int i = 0; i < len; i++){
8+
if(origin.charAt(i) == '('){ // 如果是左括号
9+
left++;
10+
}else if(origin.charAt(i) == ')'){ // 如果是右括号
11+
left--; // 需要使用一个左括号进行匹配
12+
}else{
13+
int index = i; // 当前位置
14+
int num = Integer.parseInt("" + origin.charAt(i)); // 将字符数字转换成整型
15+
while((i + 1) < len && origin.charAt(i + 1) >= '0' && origin.charAt(i + 1) <= '9'){ // 如果下一个字符还是数字
16+
num = num * 10 + Integer.parseInt("" + origin.charAt(i + 1));
17+
i++;
18+
}
19+
if(origin.charAt(index - 1) == '('){ // 如果上一个字符是左括号
20+
left = left - 1 + num; // 则加上当前的个数(num),并减去之前在if中误认为的左括号(数字前面的),因为数字前面的左括号是标识符,不算个数
21+
}else{
22+
left = left + 1 - num; // 同理,之前else if中的右括号多算了一个,把多减去的补回来,并减去真实的右括号的数量(num)
23+
}
24+
}
25+
26+
if(left < 0){ // 如果左括号不够了
27+
return false;
28+
}
29+
}
30+
31+
if(left != 0){ // 如果最终右括号比左括号多了
32+
return false;
33+
}
34+
35+
return true;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
int count;
12+
int val;
13+
public int countNodes(TreeNode root, int val) {
14+
count = 0;
15+
this.val = val;
16+
dfs(root);
17+
return count;
18+
}
19+
private void dfs(TreeNode root){
20+
if(root == null){
21+
return;
22+
}
23+
if(root.val == val){
24+
count++;
25+
}
26+
dfs(root.left);
27+
dfs(root.right);
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
int count = 0;
12+
public int countInconsistentNode(TreeNode root1, TreeNode root2) {
13+
count = 0;
14+
dfs(root1, root2);
15+
return count;
16+
}
17+
18+
private void dfs(TreeNode root1, TreeNode root2){
19+
if(root1 == null && root2 == null){ // 都为空
20+
return;
21+
}
22+
if(root1 == null || root2 == null){ // 一个为空,一个不为空时,不同情况等于该不为空的结点,及其子孙结点
23+
if(root1 == null){
24+
count += countNode(root2);
25+
}else{
26+
count += countNode(root1);
27+
}
28+
return;
29+
}
30+
if(root1.val != root2.val){ // 都不为空,但值不相等时,是两个"不同"
31+
count += 2;
32+
}
33+
dfs(root1.left, root2.left);
34+
dfs(root1.right, root2.right);
35+
}
36+
37+
private int countNode(TreeNode node){ // 返回当前结点所在的子树的结点数量
38+
if(node == null){
39+
return 0;
40+
}
41+
int leftCount = countNode(node.left);
42+
int rightCount = countNode(node.right);
43+
return leftCount + rightCount + 1;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
PriorityQueue<Integer> pq;
12+
public int[] solve(TreeNode root, int[][] operations) {
13+
// 最大堆
14+
pq = new PriorityQueue(new Comparator<Integer>(){
15+
@Override
16+
public int compare(Integer val1, Integer val2){
17+
return -(val1.intValue() - val2.intValue());
18+
}
19+
});
20+
21+
dfs(root, pq); // O(n * log(n))
22+
23+
int len = operations.length;
24+
int[] ans = new int[len];
25+
26+
for(int i = 0; i < len; i++){
27+
int[] operation = operations[i];
28+
if(operation[0] == 0){
29+
// 将当前值按顺序加入最大堆里
30+
pq.offer(operation[1]);
31+
}else{ // 删除最大元素
32+
pq.poll();
33+
}
34+
ans[i] = pq.peek(); // 获取最大元素
35+
}
36+
37+
return ans;
38+
}
39+
40+
private void dfs(TreeNode root, PriorityQueue<Integer> pq){
41+
if(root == null){
42+
return;
43+
}
44+
pq.offer(root.val);
45+
dfs(root.left, pq);
46+
dfs(root.right, pq);
47+
}
48+
}

0 commit comments

Comments
 (0)