Merge branch 'master' of https://gitee.com/huangge1199_admin/leet-code
Conflicts: src/main/java/leetcode/editor/cn/all.json
This commit is contained in:
commit
cb5444c057
@ -1,6 +1,4 @@
|
|||||||
package com.code.leet.contest1;
|
package com.code.leet.contest.contest1;
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
public class Solution1 {
|
public class Solution1 {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
@ -1,4 +1,4 @@
|
|||||||
package com.code.leet.contest1;
|
package com.code.leet.contest.contest1;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.code.leet.contest1;
|
package com.code.leet.contest.contest1;
|
||||||
|
|
||||||
import com.code.leet.entiy.ListNode;
|
import com.code.leet.entiy.ListNode;
|
||||||
|
|
@ -1,6 +1,4 @@
|
|||||||
package com.code.leet.contest1;
|
package com.code.leet.contest.contest1;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
public class Solution4 {
|
public class Solution4 {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
@ -1,4 +1,4 @@
|
|||||||
package com.code.leet.contest1;
|
package com.code.leet.contest.contest1;
|
||||||
|
|
||||||
public class Solution5 {
|
public class Solution5 {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
@ -1,4 +1,4 @@
|
|||||||
package com.code.leet.contest1;
|
package com.code.leet.contest.contest1;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
@ -1,4 +1,4 @@
|
|||||||
package com.code.leet.contest2;
|
package com.code.leet.contest.contest2;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
@ -1,6 +1,4 @@
|
|||||||
package com.code.leet.contest2;
|
package com.code.leet.contest.contest2;
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
public class Solution2 {
|
public class Solution2 {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
@ -1,6 +1,4 @@
|
|||||||
package com.code.leet.contest2;
|
package com.code.leet.contest.contest2;
|
||||||
|
|
||||||
import com.code.leet.entiy.ListNode;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
@ -1,4 +1,4 @@
|
|||||||
package com.code.leet.contest2;
|
package com.code.leet.contest.contest2;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.code.leet.contest2;
|
package com.code.leet.contest.contest2;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
@ -1,7 +1,6 @@
|
|||||||
package com.code.leet.contest2;
|
package com.code.leet.contest.contest2;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class Solution6 {
|
public class Solution6 {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
@ -1,4 +1,4 @@
|
|||||||
package com.code.leet.contest3;
|
package com.code.leet.contest.contest3;
|
||||||
|
|
||||||
import com.code.leet.entiy.TreeNode;
|
import com.code.leet.entiy.TreeNode;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.code.leet.contest3;
|
package com.code.leet.contest.contest3;
|
||||||
|
|
||||||
import com.code.leet.entiy.TreeNode;
|
import com.code.leet.entiy.TreeNode;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.code.leet.contest3;
|
package com.code.leet.contest.contest3;
|
||||||
|
|
||||||
import com.code.leet.entiy.TreeNode;
|
import com.code.leet.entiy.TreeNode;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.code.leet.contest3;
|
package com.code.leet.contest.contest3;
|
||||||
|
|
||||||
import com.code.leet.entiy.TreeNode;
|
import com.code.leet.entiy.TreeNode;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.code.leet.contest3;
|
package com.code.leet.contest.contest3;
|
||||||
|
|
||||||
import com.code.leet.entiy.TreeNode;
|
import com.code.leet.entiy.TreeNode;
|
||||||
|
|
50
src/main/java/com/code/leet/contest/contest4/Solution1.java
Normal file
50
src/main/java/com/code/leet/contest/contest4/Solution1.java
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package com.code.leet.contest.contest4;
|
||||||
|
|
||||||
|
import javafx.util.Pair;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class Solution1 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solution1 solution = new Solution1();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int countIslands(int[][] grid) {
|
||||||
|
boolean[][] use = new boolean[grid.length][grid[0].length];
|
||||||
|
int count = 0;
|
||||||
|
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
||||||
|
for (int i = 0; i < grid.length; i++) {
|
||||||
|
for (int j = 0; j < grid[0].length; j++) {
|
||||||
|
if(!use[i][j]){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
use[i][j] = true;
|
||||||
|
if (grid[i][j] == 1) {
|
||||||
|
queue.add(new Pair<>(i, j));
|
||||||
|
use[i][j] = true;
|
||||||
|
count++;
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
Pair<Integer, Integer> pair = queue.poll();
|
||||||
|
for (int k = -1; k < 2; k++) {
|
||||||
|
for (int l = -1; l < 2; l++) {
|
||||||
|
if (k == 0 && l == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int x = k + pair.getKey();
|
||||||
|
int y = l + pair.getValue();
|
||||||
|
if (x < 0 || x >= grid.length || y < 0 || y > grid[0].length || use[x][y]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
use[x][y] = true;
|
||||||
|
if (grid[x][y] == 1) {
|
||||||
|
queue.add(new Pair<>(x,y));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
83
src/main/java/com/code/leet/contest/contest4/Solution2.java
Normal file
83
src/main/java/com/code/leet/contest/contest4/Solution2.java
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
package com.code.leet.contest.contest4;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class Solution2 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solution2 solution = new Solution2();
|
||||||
|
System.out.println(solution.countPlans(new int[]{31,18,-11,13},20));
|
||||||
|
}
|
||||||
|
|
||||||
|
// public int countPlans(int[] nums, int target) {
|
||||||
|
// Arrays.sort(nums);
|
||||||
|
// if (nums[0] > target) {
|
||||||
|
// return 0;
|
||||||
|
// }
|
||||||
|
// if (nums[0] >= 0 || nums[nums.length - 1] <= 0) {
|
||||||
|
// return count(0, nums.length, target, nums);
|
||||||
|
// }
|
||||||
|
// int c0 = 0;
|
||||||
|
// int index = 0;
|
||||||
|
// for (int i = 0; i < nums.length; i++) {
|
||||||
|
// if (nums[i] == 0) {
|
||||||
|
// c0++;
|
||||||
|
// } else if (nums[i] > 0) {
|
||||||
|
// index = i;
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// int count = 0;
|
||||||
|
// for (int i = 0; i < index - c0; i++) {
|
||||||
|
// count += countPlans(Arrays.copyOfRange(nums, i + 1, nums.length), target - nums[i]);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// count += count(index, nums.length, target, nums);
|
||||||
|
// return count;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// private int count(int start, int end, int target, int[] nums) {
|
||||||
|
// Queue<Integer> queue = new LinkedList<>();
|
||||||
|
// queue.add(nums[start]);
|
||||||
|
// int sum = nums[start];
|
||||||
|
// int count = 0;
|
||||||
|
// for (int i = start + 1; i < end; i++) {
|
||||||
|
// sum += nums[i];
|
||||||
|
// queue.add(nums[i]);
|
||||||
|
// while (!queue.isEmpty() && sum > target) {
|
||||||
|
// sum -= queue.poll();
|
||||||
|
// }
|
||||||
|
// if (sum == target) {
|
||||||
|
// count++;
|
||||||
|
// }
|
||||||
|
// if (queue.isEmpty()) {
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return count;
|
||||||
|
// }
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
public int countPlans(int[] nums, int target) {
|
||||||
|
Arrays.sort(nums);
|
||||||
|
count(nums, target, 0, 0);
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void count(int[] nums, int target, int sum, int index) {
|
||||||
|
if (sum == target) {
|
||||||
|
count++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (sum > target) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (index == nums.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int count = 0;
|
||||||
|
for (int i = index; i < nums.length; i++) {
|
||||||
|
count(nums, target, sum + nums[i], index + 1);
|
||||||
|
count(nums, target, sum, index + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
44
src/main/java/com/code/leet/contest/contest4/Solution3.java
Normal file
44
src/main/java/com/code/leet/contest/contest4/Solution3.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package com.code.leet.contest.contest4;
|
||||||
|
|
||||||
|
import com.code.leet.entiy.TreeNode;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class Solution3 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solution3 solution = new Solution3();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[][] solve(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Queue<TreeNode> queue = new LinkedList<>();
|
||||||
|
queue.add(root);
|
||||||
|
List<List<Integer>> list = new ArrayList<>();
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
int size = queue.size();
|
||||||
|
List<Integer> temp = new ArrayList<>();
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
TreeNode node = queue.poll();
|
||||||
|
temp.add(node.val);
|
||||||
|
if (node.right != null) {
|
||||||
|
queue.add(node.right);
|
||||||
|
}
|
||||||
|
if (node.left != null) {
|
||||||
|
queue.add(node.left);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
list.add(temp);
|
||||||
|
}
|
||||||
|
int[][] arr = new int[list.size()][];
|
||||||
|
for (int i = 0; i < list.size(); i++) {
|
||||||
|
List<Integer> temp = list.get(i);
|
||||||
|
arr[i] = new int[temp.size()];
|
||||||
|
for (int j = 0; j < temp.size(); j++) {
|
||||||
|
arr[i][j] = temp.get(j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
}
|
58
src/main/java/com/code/leet/contest/contest4/Solution4.java
Normal file
58
src/main/java/com/code/leet/contest/contest4/Solution4.java
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package com.code.leet.contest.contest4;
|
||||||
|
|
||||||
|
import javafx.util.Pair;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class Solution4 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solution4 solution = new Solution4();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDistance(String[] maze) {
|
||||||
|
int[] xIndex = new int[]{-1, 1, 0, 0};
|
||||||
|
int[] yIndex = new int[]{0, 0, -1, 1};
|
||||||
|
char[][] chs = new char[maze.length][maze[0].length()];
|
||||||
|
for (int i = 0; i < maze.length; i++) {
|
||||||
|
chs[i] = maze[i].toCharArray();
|
||||||
|
}
|
||||||
|
boolean[][] use = new boolean[maze.length][maze[0].length()];
|
||||||
|
Pair<Integer, Integer> start = null;
|
||||||
|
for (int i = 0; i < chs.length; i++) {
|
||||||
|
for (int j = 0; j < chs[0].length; j++) {
|
||||||
|
if (chs[i][j] == 'X') {
|
||||||
|
use[i][j] = true;
|
||||||
|
}
|
||||||
|
if (chs[i][j] == 'S') {
|
||||||
|
use[i][j] = true;
|
||||||
|
start = new Pair<>(i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
||||||
|
queue.add(start);
|
||||||
|
int count = 0;
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
int size = queue.size();
|
||||||
|
count++;
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
Pair<Integer, Integer> pair = queue.poll();
|
||||||
|
for (int j = 0; j < 4; j++) {
|
||||||
|
int x = pair.getKey() + xIndex[j];
|
||||||
|
int y = pair.getValue() + yIndex[j];
|
||||||
|
if (x < 0 || x >= chs.length || y < 0 || y >= chs[0].length || use[x][y]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (chs[x][y] == '.') {
|
||||||
|
queue.add(new Pair<>(x, y));
|
||||||
|
use[x][y] = true;
|
||||||
|
}
|
||||||
|
if(chs[x][y]=='E'){
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
40
src/main/java/com/code/leet/contest/contest4/Solution5.java
Normal file
40
src/main/java/com/code/leet/contest/contest4/Solution5.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package com.code.leet.contest.contest4;
|
||||||
|
|
||||||
|
import javafx.util.Pair;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class Solution5 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solution5 solution = new Solution5();
|
||||||
|
solution.shiftMatrix(new int[][]{{1,2,3},{4,5,6},{7,8,9}},new int[][]{{3,1,9},{4,5,2},{7,8,6}});
|
||||||
|
}
|
||||||
|
|
||||||
|
public int shiftMatrix(int[][] source, int[][] target) {
|
||||||
|
Map<Integer, Pair<Integer, Integer>[]> map = new HashMap<>();
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
for (int j = 0; j < 3; j++) {
|
||||||
|
if (source[i][j] != target[i][j]) {
|
||||||
|
Pair<Integer,Integer> pair = new Pair<>(i,j);
|
||||||
|
Pair[] temp = map.getOrDefault(source[i][j], new Pair[2]);
|
||||||
|
temp[0] = pair;
|
||||||
|
map.put(source[i][j],temp);
|
||||||
|
temp = map.getOrDefault(target[i][j], new Pair[2]);
|
||||||
|
temp[1] = pair;
|
||||||
|
map.put(target[i][j],temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int max = 0;
|
||||||
|
for (int key : map.keySet()) {
|
||||||
|
Pair<Integer, Integer>[] pairs = map.get(key);
|
||||||
|
int x = pairs[1].getKey() - pairs[0].getKey();
|
||||||
|
int y = pairs[1].getValue() - pairs[0].getValue();
|
||||||
|
x = x >= 0 ? x : x + 3;
|
||||||
|
y = y >= 0 ? y : y + 3;
|
||||||
|
max = Math.max(x + y, max);
|
||||||
|
}
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
}
|
68
src/main/java/com/code/leet/contest/contest4/Solution6.java
Normal file
68
src/main/java/com/code/leet/contest/contest4/Solution6.java
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
package com.code.leet.contest.contest4;
|
||||||
|
|
||||||
|
import javafx.util.Pair;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class Solution6 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solution6 solution = new Solution6();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDistance(int[][] grid) {
|
||||||
|
int[] xI = new int[]{-1, 1, 0, 0};
|
||||||
|
int[] yIndex = new int[]{0, 0, -1, 1};
|
||||||
|
Queue<Pair<Integer, Integer>> load = new LinkedList<>();
|
||||||
|
boolean[][] use = new boolean[grid.length][grid[0].length];
|
||||||
|
boolean bl = false;
|
||||||
|
for (int i = 0; i < grid.length; i++) {
|
||||||
|
for (int j = 0; j < grid[0].length; j++) {
|
||||||
|
if (grid[i][j] == 1 && !use[i][j]) {
|
||||||
|
bl = true;
|
||||||
|
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
||||||
|
queue.add(new Pair<>(i, j));
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
Pair<Integer, Integer> pair = queue.poll();
|
||||||
|
load.add(new Pair<>(pair.getKey(), pair.getValue()));
|
||||||
|
for (int k = 0; k < 4; k++) {
|
||||||
|
int x = pair.getKey() + xI[k];
|
||||||
|
int y = pair.getValue() + yIndex[k];
|
||||||
|
if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || use[x][y]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (grid[x][y] == 1) {
|
||||||
|
queue.add(new Pair<>(x, y));
|
||||||
|
use[x][y] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (bl) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int count = 0;
|
||||||
|
while (!load.isEmpty()) {
|
||||||
|
int size = load.size();
|
||||||
|
count++;
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
Pair<Integer, Integer> pair = load.poll();
|
||||||
|
for (int k = 0; k < 4; k++) {
|
||||||
|
int x = pair.getKey() + xI[k];
|
||||||
|
int y = pair.getValue() + yIndex[k];
|
||||||
|
if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || use[x][y]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (grid[x][y] == 1) {
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
load.add(new Pair<>(x, y));
|
||||||
|
use[x][y] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
74
src/main/java/com/code/leet/doubleWeek/SolutionD58.java
Normal file
74
src/main/java/com/code/leet/doubleWeek/SolutionD58.java
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
package com.code.leet.doubleWeek;
|
||||||
|
|
||||||
|
public class SolutionD58 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SolutionD58 solution = new SolutionD58();
|
||||||
|
// System.out.println(solution.minimumPerimeter(1000000000));
|
||||||
|
System.out.println(solution.countSpecialSubsequences(new int[]{0, 1, 2, 0, 1, 2}));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean isThree(int n) {
|
||||||
|
int flag = 0;
|
||||||
|
if (n <= 2) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int num = n / 2;
|
||||||
|
for (int i = 3; i < num; i++) {
|
||||||
|
if (n % i == 0) {
|
||||||
|
if (flag == 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
flag = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return flag == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long numberOfWeeks(int[] milestones) {
|
||||||
|
long max = 0;
|
||||||
|
long sum = 0;
|
||||||
|
for (int milestone : milestones) {
|
||||||
|
sum += milestone;
|
||||||
|
max = Math.max(max, milestone);
|
||||||
|
}
|
||||||
|
return sum / max >= 2 ? sum : (sum - max) * 2 + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long minimumPerimeter(long neededApples) {
|
||||||
|
if (neededApples == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (neededApples < 12) {
|
||||||
|
return 8;
|
||||||
|
}
|
||||||
|
long n = 2;
|
||||||
|
long cur = 12;
|
||||||
|
long sum = 12;
|
||||||
|
while (sum < neededApples) {
|
||||||
|
cur = cur + 12 * (n + 1);
|
||||||
|
sum += cur;
|
||||||
|
n += 2;
|
||||||
|
}
|
||||||
|
return 4 * n;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int countSpecialSubsequences(int[] nums) {
|
||||||
|
long[] result = new long[3];
|
||||||
|
long[] counts = new long[nums.length];
|
||||||
|
for (int i = nums.length - 1; i >= 0; i--) {
|
||||||
|
int num = nums[i];
|
||||||
|
if (num == 1) {
|
||||||
|
counts[i] = result[2];
|
||||||
|
result[num] += counts[i];
|
||||||
|
} else if (num == 0) {
|
||||||
|
result[num] = result[1] * result[2];
|
||||||
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// return (int) (result[0][2] % 100000007);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,104 @@
|
|||||||
|
//在一棵无限的二叉树上,每个节点都有两个子节点,树中的节点 逐行 依次按 “之” 字形进行标记。
|
||||||
|
//
|
||||||
|
// 如下图所示,在奇数行(即,第一行、第三行、第五行……)中,按从左到右的顺序进行标记;
|
||||||
|
//
|
||||||
|
// 而偶数行(即,第二行、第四行、第六行……)中,按从右到左的顺序进行标记。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 给你树上某一个节点的标号 label,请你返回从根节点到该标号为 label 节点的路径,该路径是由途经的节点标号所组成的。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
// 输入:label = 14
|
||||||
|
//输出:[1,3,4,14]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
// 输入:label = 26
|
||||||
|
//输出:[1,2,6,10,26]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= label <= 10^6
|
||||||
|
//
|
||||||
|
// Related Topics 树 数学 二叉树
|
||||||
|
// 👍 144 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
//1104:二叉树寻路
|
||||||
|
class PathInZigzagLabelledBinaryTree {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new PathInZigzagLabelledBinaryTree().new Solution();
|
||||||
|
solution.pathInZigZagTree(14);
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public List<Integer> pathInZigZagTree(int label) {
|
||||||
|
int max = 1;
|
||||||
|
int index = 1;
|
||||||
|
while (max * 2 < label) {
|
||||||
|
max = max * 2;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
List<Integer> list = new ArrayList<>();
|
||||||
|
list.add(label);
|
||||||
|
if (max != label) {
|
||||||
|
index--;
|
||||||
|
}
|
||||||
|
max = max * 3;
|
||||||
|
for (int i = index; i > 0; i--) {
|
||||||
|
label = (max - 1 - label) / 2;
|
||||||
|
list.add(label);
|
||||||
|
max /= 2;
|
||||||
|
}
|
||||||
|
Collections.reverse(list);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// public List<Integer> pathInZigZagTree(int label) {
|
||||||
|
// int row = 1, rowStart = 1;
|
||||||
|
// while (rowStart * 2 <= label) {
|
||||||
|
// row++;
|
||||||
|
// rowStart *= 2;
|
||||||
|
// }
|
||||||
|
// if (row % 2 == 0) {
|
||||||
|
// label = getReverse(label, row);
|
||||||
|
// }
|
||||||
|
// List<Integer> path = new ArrayList<Integer>();
|
||||||
|
// while (row > 0) {
|
||||||
|
// if (row % 2 == 0) {
|
||||||
|
// path.add(getReverse(label, row));
|
||||||
|
// } else {
|
||||||
|
// path.add(label);
|
||||||
|
// }
|
||||||
|
// row--;
|
||||||
|
// label >>= 1;
|
||||||
|
// }
|
||||||
|
// Collections.reverse(path);
|
||||||
|
// return path;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public int getReverse(int label, int row) {
|
||||||
|
// return (1 << row - 1) + (1 << row) - 1 - label;
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
<p>在一棵无限的二叉树上,每个节点都有两个子节点,树中的节点 <strong>逐行</strong> 依次按 “之” 字形进行标记。</p>
|
||||||
|
|
||||||
|
<p>如下图所示,在奇数行(即,第一行、第三行、第五行……)中,按从左到右的顺序进行标记;</p>
|
||||||
|
|
||||||
|
<p>而偶数行(即,第二行、第四行、第六行……)中,按从右到左的顺序进行标记。</p>
|
||||||
|
|
||||||
|
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/06/28/tree.png" style="height: 138px; width: 300px;"></p>
|
||||||
|
|
||||||
|
<p>给你树上某一个节点的标号 <code>label</code>,请你返回从根节点到该标号为 <code>label</code> 节点的路径,该路径是由途经的节点标号所组成的。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>label = 14
|
||||||
|
<strong>输出:</strong>[1,3,4,14]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>label = 26
|
||||||
|
<strong>输出:</strong>[1,2,6,10,26]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= label <= 10^6</code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>树</li><li>数学</li><li>二叉树</li></div></div>\n<div><li>👍 144</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user