Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
3c28d2a686
72
src/main/java/contest/y2022/m7/Dw83.java
Normal file
72
src/main/java/contest/y2022/m7/Dw83.java
Normal file
@ -0,0 +1,72 @@
|
||||
package contest.y2022.m7;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Dw83 {
|
||||
public static void main(String[] args) {
|
||||
Dw83 solution = new Dw83();
|
||||
}
|
||||
|
||||
public int shortestSequence(int[] rolls, int k) {
|
||||
int cnt = 1;
|
||||
boolean[] uses = new boolean[k + 1];
|
||||
int use = k;
|
||||
for (int i = 0; i < rolls.length; i++) {
|
||||
if (!uses[rolls[i]]) {
|
||||
uses[rolls[i]] = true;
|
||||
use--;
|
||||
}
|
||||
if (use == 0) {
|
||||
cnt++;
|
||||
Arrays.fill(uses, false);
|
||||
use = k;
|
||||
}
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
|
||||
public long zeroFilledSubarray(int[] nums) {
|
||||
long cnt = 0;
|
||||
int[] cnts = new int[nums.length];
|
||||
if (nums[0] == 0) {
|
||||
cnts[0] = 1;
|
||||
cnt = 1;
|
||||
}
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
if (nums[i] == 0) {
|
||||
cnts[i] = cnts[i - 1] + 1;
|
||||
cnt += cnts[i];
|
||||
}
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
|
||||
public String bestHand(int[] ranks, char[] suits) {
|
||||
boolean bl = true;
|
||||
int max = 1;
|
||||
int cnt = 1;
|
||||
Arrays.sort(ranks);
|
||||
for (int i = 1; i < 5; i++) {
|
||||
if (suits[i] != suits[i - 1]) {
|
||||
bl = false;
|
||||
}
|
||||
if (ranks[i] == ranks[i - 1]) {
|
||||
cnt++;
|
||||
} else {
|
||||
max = Math.max(max, cnt);
|
||||
cnt = 1;
|
||||
}
|
||||
}
|
||||
max = Math.max(max, cnt);
|
||||
if (bl) {
|
||||
return "Flush";
|
||||
} else if (max >= 3) {
|
||||
return "Three of a Kind";
|
||||
} else if (max == 2) {
|
||||
return "Pair";
|
||||
} else {
|
||||
return "High Card";
|
||||
}
|
||||
}
|
||||
}
|
32
src/main/java/contest/y2022/m7/NumberContainers.java
Normal file
32
src/main/java/contest/y2022/m7/NumberContainers.java
Normal file
@ -0,0 +1,32 @@
|
||||
package contest.y2022.m7;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class NumberContainers {
|
||||
Map<Integer, Integer> nums;
|
||||
Map<Integer, TreeSet<Integer>> map;
|
||||
|
||||
public NumberContainers() {
|
||||
nums = new HashMap<>();
|
||||
map = new HashMap<>();
|
||||
}
|
||||
|
||||
public void change(int index, int number) {
|
||||
if (nums.containsKey(index)) {
|
||||
map.get(nums.get(index)).remove(index);
|
||||
}
|
||||
nums.put(index, number);
|
||||
TreeSet<Integer> list = map.getOrDefault(number, new TreeSet<>());
|
||||
list.add(index);
|
||||
map.put(number, list);
|
||||
}
|
||||
|
||||
public int find(int number) {
|
||||
TreeSet<Integer> list = map.getOrDefault(number, new TreeSet<>());
|
||||
if (list.size() > 0) {
|
||||
return list.first();
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
118
src/main/java/contest/y2022/m9/Week311.java
Normal file
118
src/main/java/contest/y2022/m9/Week311.java
Normal file
@ -0,0 +1,118 @@
|
||||
package contest.y2022.m9;
|
||||
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Week311 {
|
||||
public static void main(String[] args) {
|
||||
Week311 soluytion = new Week311();
|
||||
//TreeNode root = new TreeNode(Arrays.asList(2, 3, 5, 8, 13, 21, 34));
|
||||
TreeNode root = new TreeNode(Arrays.asList(0, 1, 2, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2));
|
||||
soluytion.reverseOddLevels(root);
|
||||
}
|
||||
|
||||
class Trie {
|
||||
private Trie[] children;
|
||||
private int cnt;
|
||||
|
||||
public Trie() {
|
||||
children = new Trie[26];
|
||||
cnt = 0;
|
||||
}
|
||||
|
||||
public void insert(String word) {
|
||||
Trie node = this;
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
char ch = word.charAt(i);
|
||||
int index = ch - 'a';
|
||||
if (node.children[index] == null) {
|
||||
node.children[index] = new Trie();
|
||||
}
|
||||
node = node.children[index];
|
||||
node.cnt += 1;
|
||||
}
|
||||
}
|
||||
|
||||
private int search(String prefix) {
|
||||
Trie node = this;
|
||||
int cnt = 0;
|
||||
for (int i = 0; i < prefix.length(); i++) {
|
||||
char ch = prefix.charAt(i);
|
||||
int index = ch - 'a';
|
||||
if (node.children[index] == null) {
|
||||
break;
|
||||
}
|
||||
node = node.children[index];
|
||||
cnt += node.cnt;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
}
|
||||
|
||||
public int[] sumPrefixScores(String[] words) {
|
||||
Trie trie = new Trie();
|
||||
for (String word : words) {
|
||||
trie.insert(word);
|
||||
}
|
||||
int[] arrs = new int[words.length];
|
||||
for (int i = 0; i < words.length; i++) {
|
||||
arrs[i] = trie.search(words[i]);
|
||||
}
|
||||
return arrs;
|
||||
}
|
||||
|
||||
public TreeNode reverseOddLevels(TreeNode root) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
while (!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll();
|
||||
list.add(node.val);
|
||||
if (node.left != null) {
|
||||
queue.add(node.left);
|
||||
queue.add(node.right);
|
||||
}
|
||||
}
|
||||
root = new TreeNode(list.get(0));
|
||||
queue.add(root);
|
||||
int n = 1;
|
||||
int min = 0;
|
||||
int max = 0;
|
||||
int cnt = 1;
|
||||
int mul = 1;
|
||||
while (cnt < list.size()) {
|
||||
min = max + 1;
|
||||
max += mul * 2;
|
||||
cnt += mul * 2;
|
||||
if (n % 2 == 0) {
|
||||
for (int i = min; i <= max; i = i + 2) {
|
||||
TreeNode node = queue.poll();
|
||||
TreeNode left = new TreeNode(list.get(i));
|
||||
TreeNode right = new TreeNode(list.get(i + 1));
|
||||
node.left = left;
|
||||
node.right = right;
|
||||
queue.add(left);
|
||||
queue.add(right);
|
||||
}
|
||||
} else {
|
||||
for (int i = max; i >= min; i = i - 2) {
|
||||
TreeNode node = queue.poll();
|
||||
TreeNode left = new TreeNode(list.get(i));
|
||||
TreeNode right = new TreeNode(list.get(i - 1));
|
||||
node.left = left;
|
||||
node.right = right;
|
||||
queue.add(left);
|
||||
queue.add(right);
|
||||
}
|
||||
}
|
||||
n++;
|
||||
mul *= 2;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
public int smallestEvenMultiple(int n) {
|
||||
return n % 2 == 0 ? n : n * 2;
|
||||
}
|
||||
}
|
@ -39,20 +39,21 @@
|
||||
// 👍 1 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//5963:反转两次的数字
|
||||
class ANumberAfterADoubleReversal{
|
||||
class ANumberAfterADoubleReversal {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new ANumberAfterADoubleReversal().new Solution();
|
||||
}
|
||||
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public boolean isSameAfterReversals(int num) {
|
||||
return num == 0 || num % 10 > 0;
|
||||
class Solution {
|
||||
public boolean isSameAfterReversals(int num) {
|
||||
return num == 0 || num % 10 > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -45,53 +45,54 @@ package leetcode.editor.cn;
|
||||
import com.code.leet.entiy.ListNode;
|
||||
|
||||
//2:两数相加
|
||||
public class AddTwoNumbers{
|
||||
public class AddTwoNumbers {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new AddTwoNumbers().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
|
||||
int sum = 0;
|
||||
ListNode temp = l1;
|
||||
while (temp != null) {
|
||||
sum = l2 == null ? temp.val + sum : temp.val + l2.val + sum;
|
||||
temp.val = sum % 10;
|
||||
sum = sum / 10;
|
||||
|
||||
if (l2 != null && temp.next == null && l2.next == null && sum > 0) {
|
||||
temp.next = new ListNode(sum);
|
||||
sum = 0;
|
||||
l2 = null;
|
||||
} else if (l2 != null && temp.next == null && l2.next != null) {
|
||||
temp.next = l2.next;
|
||||
l2 = null;
|
||||
} else if (l2 != null) {
|
||||
l2 = l2.next;
|
||||
} else {
|
||||
if (temp.next == null && sum > 0) {
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
|
||||
int sum = 0;
|
||||
ListNode temp = l1;
|
||||
while (temp != null) {
|
||||
sum = l2 == null ? temp.val + sum : temp.val + l2.val + sum;
|
||||
temp.val = sum % 10;
|
||||
sum = sum / 10;
|
||||
|
||||
if (l2 != null && temp.next == null && l2.next == null && sum > 0) {
|
||||
temp.next = new ListNode(sum);
|
||||
sum = 0;
|
||||
l2 = null;
|
||||
} else if (l2 != null && temp.next == null && l2.next != null) {
|
||||
temp.next = l2.next;
|
||||
l2 = null;
|
||||
} else if (l2 != null) {
|
||||
l2 = l2.next;
|
||||
} else {
|
||||
if (temp.next == null && sum > 0) {
|
||||
temp.next = new ListNode(sum);
|
||||
sum = 0;
|
||||
}
|
||||
l2 = null;
|
||||
}
|
||||
l2 = null;
|
||||
temp = temp.next;
|
||||
}
|
||||
temp = temp.next;
|
||||
return l1;
|
||||
}
|
||||
return l1;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -54,13 +54,13 @@ public class AdvantageShuffle {
|
||||
int[] copy = Arrays.copyOf(A, length);
|
||||
Arrays.sort(copy);
|
||||
int i = 0, j = 0;
|
||||
while(i < A.length) {
|
||||
if(copy[i] > pairs[j].getValue()){
|
||||
while (i < A.length) {
|
||||
if (copy[i] > pairs[j].getValue()) {
|
||||
A[pairs[j].getKey()] = copy[i];
|
||||
i++;
|
||||
j++;
|
||||
} else {
|
||||
A[pairs[(A.length-1) - (i-j)].getKey()] = copy[i];
|
||||
A[pairs[(A.length - 1) - (i - j)].getKey()] = copy[i];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ public class AllNodesDistanceKInBinaryTree {
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
// private Stack<TreeNode> stack = new Stack<>();
|
||||
// private Stack<TreeNode> stack = new Stack<>();
|
||||
// List<Integer> list = new ArrayList<>();
|
||||
//
|
||||
// public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {
|
||||
@ -110,7 +110,7 @@ public class AllNodesDistanceKInBinaryTree {
|
||||
// dfs(root.left, index + 1, k);
|
||||
// dfs(root.right, index + 1, k);
|
||||
// }
|
||||
Map<Integer, TreeNode> parents = new HashMap<Integer, TreeNode>();
|
||||
Map<Integer, TreeNode> parents = new HashMap<Integer, TreeNode>();
|
||||
List<Integer> ans = new ArrayList<Integer>();
|
||||
|
||||
public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {
|
||||
|
@ -57,34 +57,34 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
//446:等差数列划分 II - 子序列
|
||||
class ArithmeticSlicesIiSubsequence{
|
||||
class ArithmeticSlicesIiSubsequence {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new ArithmeticSlicesIiSubsequence().new Solution();
|
||||
}
|
||||
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int numberOfArithmeticSlices(int[] nums) {
|
||||
class Solution {
|
||||
public int numberOfArithmeticSlices(int[] nums) {
|
||||
|
||||
int count = 0;
|
||||
int length = nums.length;
|
||||
Map<Long, Integer>[] f = new Map[length];
|
||||
for (int i = 0; i < length; ++i) {
|
||||
f[i] = new HashMap<>();
|
||||
}
|
||||
for (int i = 0; i < length; ++i) {
|
||||
for (int j = 0; j < i; ++j) {
|
||||
long d = (long) nums[i] - nums[j];
|
||||
int cnt = f[j].getOrDefault(d, 0);
|
||||
count += cnt;
|
||||
f[i].put(d, f[i].getOrDefault(d, 0) + cnt + 1);
|
||||
int count = 0;
|
||||
int length = nums.length;
|
||||
Map<Long, Integer>[] f = new Map[length];
|
||||
for (int i = 0; i < length; ++i) {
|
||||
f[i] = new HashMap<>();
|
||||
}
|
||||
for (int i = 0; i < length; ++i) {
|
||||
for (int j = 0; j < i; ++j) {
|
||||
long d = (long) nums[i] - nums[j];
|
||||
int cnt = f[j].getOrDefault(d, 0);
|
||||
count += cnt;
|
||||
f[i].put(d, f[i].getOrDefault(d, 0) + cnt + 1);
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -46,12 +46,12 @@ class ArrangingCoins {
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int arrangeCoins(long n) {
|
||||
if(n==0){
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
}
|
||||
double number = (Math.sqrt(1+8*n)-1)/2;
|
||||
double number = (Math.sqrt(1 + 8 * n) - 1) / 2;
|
||||
|
||||
return (int)(Math.floor(number));
|
||||
return (int) (Math.floor(number));
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
@ -58,11 +58,11 @@ class AssignCookies {
|
||||
int gi = 0;
|
||||
int si = 0;
|
||||
while (gi < g.length && si < s.length) {
|
||||
if(s[si]>=g[gi]){
|
||||
if (s[si] >= g[gi]) {
|
||||
count++;
|
||||
si++;
|
||||
gi++;
|
||||
}else{
|
||||
} else {
|
||||
si++;
|
||||
}
|
||||
}
|
||||
|
@ -50,41 +50,41 @@ package leetcode.editor.cn;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class AsteroidCollision{
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new AsteroidCollision().new Solution();
|
||||
}
|
||||
public class AsteroidCollision {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new AsteroidCollision().new Solution();
|
||||
}
|
||||
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int[] asteroidCollision(int[] asteroids) {
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
for (int num : asteroids) {
|
||||
boolean isIn = true;
|
||||
while (!stack.isEmpty() && num < 0 && stack.peek() > 0) {
|
||||
if (num + stack.peek() < 0) {
|
||||
stack.pop();
|
||||
} else if (num + stack.peek() > 0) {
|
||||
isIn = false;
|
||||
break;
|
||||
} else {
|
||||
isIn = false;
|
||||
stack.pop();
|
||||
break;
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int[] asteroidCollision(int[] asteroids) {
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
for (int num : asteroids) {
|
||||
boolean isIn = true;
|
||||
while (!stack.isEmpty() && num < 0 && stack.peek() > 0) {
|
||||
if (num + stack.peek() < 0) {
|
||||
stack.pop();
|
||||
} else if (num + stack.peek() > 0) {
|
||||
isIn = false;
|
||||
break;
|
||||
} else {
|
||||
isIn = false;
|
||||
stack.pop();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isIn) {
|
||||
stack.push(num);
|
||||
}
|
||||
}
|
||||
if (isIn) {
|
||||
stack.push(num);
|
||||
int size = stack.size();
|
||||
asteroids = new int[size];
|
||||
for (int i = size - 1; i >= 0; i--) {
|
||||
asteroids[i] = stack.pop();
|
||||
}
|
||||
return asteroids;
|
||||
}
|
||||
int size = stack.size();
|
||||
asteroids = new int[size];
|
||||
for (int i = size - 1; i >= 0; i--) {
|
||||
asteroids[i] = stack.pop();
|
||||
}
|
||||
return asteroids;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
||||
|
@ -44,50 +44,51 @@ import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
//637:二叉树的层平均值
|
||||
public class AverageOfLevelsInBinaryTree{
|
||||
public class AverageOfLevelsInBinaryTree {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new AverageOfLevelsInBinaryTree().new Solution();
|
||||
// TO TEST
|
||||
}
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* public class TreeNode {
|
||||
* int val;
|
||||
* TreeNode left;
|
||||
* TreeNode right;
|
||||
* TreeNode() {}
|
||||
* TreeNode(int val) { this.val = val; }
|
||||
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
* this.val = val;
|
||||
* this.left = left;
|
||||
* this.right = right;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public List<Double> averageOfLevels(TreeNode root) {
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
List<Double> result = new ArrayList<>();
|
||||
while (!queue.isEmpty()){
|
||||
int size = queue.size();
|
||||
double sum = 0f;
|
||||
for (int i = 0; i < size; i++) {
|
||||
TreeNode node = queue.poll();
|
||||
if(node.left!=null){
|
||||
queue.add(node.left);
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* public class TreeNode {
|
||||
* int val;
|
||||
* TreeNode left;
|
||||
* TreeNode right;
|
||||
* TreeNode() {}
|
||||
* TreeNode(int val) { this.val = val; }
|
||||
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
* this.val = val;
|
||||
* this.left = left;
|
||||
* this.right = right;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public List<Double> averageOfLevels(TreeNode root) {
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
List<Double> result = new ArrayList<>();
|
||||
while (!queue.isEmpty()) {
|
||||
int size = queue.size();
|
||||
double sum = 0f;
|
||||
for (int i = 0; i < size; i++) {
|
||||
TreeNode node = queue.poll();
|
||||
if (node.left != null) {
|
||||
queue.add(node.left);
|
||||
}
|
||||
if (node.right != null) {
|
||||
queue.add(node.right);
|
||||
}
|
||||
sum += node.val;
|
||||
}
|
||||
if(node.right!=null){
|
||||
queue.add(node.right);
|
||||
}
|
||||
sum+= node.val;
|
||||
result.add(sum / size);
|
||||
}
|
||||
result.add(sum/size);
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ public class BackspaceStringCompare {
|
||||
int length = str.length();
|
||||
for (char ch : str.toCharArray()) {
|
||||
if (ch == '#') {
|
||||
if(!stack.isEmpty()) {
|
||||
if (!stack.isEmpty()) {
|
||||
stack.pop();
|
||||
}
|
||||
} else {
|
||||
|
@ -45,45 +45,46 @@ package leetcode.editor.cn;
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
//110:平衡二叉树
|
||||
public class BalancedBinaryTree{
|
||||
public class BalancedBinaryTree {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new BalancedBinaryTree().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* public class TreeNode {
|
||||
* int val;
|
||||
* TreeNode left;
|
||||
* TreeNode right;
|
||||
* TreeNode() {}
|
||||
* TreeNode(int val) { this.val = val; }
|
||||
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
* this.val = val;
|
||||
* this.left = left;
|
||||
* this.right = right;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public boolean isBalanced(TreeNode root) {
|
||||
if (root == null) {
|
||||
return true;
|
||||
} else {
|
||||
return Math.abs(getHeight(root.left) - getHeight(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
|
||||
}
|
||||
}
|
||||
|
||||
public int getHeight(TreeNode root) {
|
||||
if (root == null) {
|
||||
return 0;
|
||||
} else {
|
||||
return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* public class TreeNode {
|
||||
* int val;
|
||||
* TreeNode left;
|
||||
* TreeNode right;
|
||||
* TreeNode() {}
|
||||
* TreeNode(int val) { this.val = val; }
|
||||
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
* this.val = val;
|
||||
* this.left = left;
|
||||
* this.right = right;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public boolean isBalanced(TreeNode root) {
|
||||
if (root == null) {
|
||||
return true;
|
||||
} else {
|
||||
return Math.abs(getHeight(root.left) - getHeight(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
|
||||
}
|
||||
}
|
||||
|
||||
public int getHeight(TreeNode root) {
|
||||
if (root == null) {
|
||||
return 0;
|
||||
} else {
|
||||
return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -33,14 +33,15 @@ package leetcode.editor.cn;
|
||||
import java.util.Stack;
|
||||
|
||||
//面试题30:包含min函数的栈
|
||||
public class BaoHanMinhanShuDeZhanLcof{
|
||||
public class BaoHanMinhanShuDeZhanLcof {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
// Solution solution = new BaoHanMinhanShuDeZhanLcof().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class MinStack {
|
||||
class MinStack {
|
||||
Stack<Integer> stack;
|
||||
Stack<Integer> min;
|
||||
|
||||
@ -70,7 +71,7 @@ class MinStack {
|
||||
public int min() {
|
||||
return min.peek();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your MinStack object will be instantiated and called as such:
|
||||
|
@ -63,7 +63,7 @@ public class BasicCalculatorIi {
|
||||
int num = 0;
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
char op = '+';
|
||||
s=s.replace(" ","");
|
||||
s = s.replace(" ", "");
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char ch = s.charAt(i);
|
||||
if (Character.isDigit(ch)) {
|
||||
|
@ -23,40 +23,41 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
//145:二叉树的后序遍历
|
||||
public class BinaryTreePostorderTraversal{
|
||||
public class BinaryTreePostorderTraversal {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new BinaryTreePostorderTraversal().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* public class TreeNode {
|
||||
* int val;
|
||||
* TreeNode left;
|
||||
* TreeNode right;
|
||||
* TreeNode() {}
|
||||
* TreeNode(int val) { this.val = val; }
|
||||
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
* this.val = val;
|
||||
* this.left = left;
|
||||
* this.right = right;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public List<Integer> postorderTraversal(TreeNode root) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
if(root==null){
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* public class TreeNode {
|
||||
* int val;
|
||||
* TreeNode left;
|
||||
* TreeNode right;
|
||||
* TreeNode() {}
|
||||
* TreeNode(int val) { this.val = val; }
|
||||
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
* this.val = val;
|
||||
* this.left = left;
|
||||
* this.right = right;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public List<Integer> postorderTraversal(TreeNode root) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
if (root == null) {
|
||||
return list;
|
||||
}
|
||||
list.addAll(postorderTraversal(root.left));
|
||||
list.addAll(postorderTraversal(root.right));
|
||||
list.add(root.val);
|
||||
return list;
|
||||
}
|
||||
list.addAll(postorderTraversal(root.left));
|
||||
list.addAll(postorderTraversal(root.right));
|
||||
list.add(root.val);
|
||||
return list;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -34,7 +34,7 @@ public class BinaryTreeZigzagLevelOrderTraversal {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new BinaryTreeZigzagLevelOrderTraversal().new Solution();
|
||||
List<Integer> list = Arrays.asList(3,9,20,null,null,15,7);
|
||||
List<Integer> list = Arrays.asList(3, 9, 20, null, null, 15, 7);
|
||||
solution.zigzagLevelOrder(new TreeNode(list));
|
||||
}
|
||||
//力扣代码
|
||||
@ -56,7 +56,8 @@ public class BinaryTreeZigzagLevelOrderTraversal {
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {List<List<Integer>> ans = new LinkedList<List<Integer>>();
|
||||
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
|
||||
List<List<Integer>> ans = new LinkedList<List<Integer>>();
|
||||
if (root == null) {
|
||||
return ans;
|
||||
}
|
||||
|
@ -26,51 +26,53 @@
|
||||
// 👍 43 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//面试题 08.14:布尔运算
|
||||
public class BooleanEvaluationLcci{
|
||||
public class BooleanEvaluationLcci {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new BooleanEvaluationLcci().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int countEval(String s, int result) {
|
||||
if (s.length() == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (s.length() == 1) {
|
||||
return (s.charAt(0) - '0') == result ? 1 : 0;
|
||||
}
|
||||
char[] ch = s.toCharArray();
|
||||
int[][][] dp = new int[ch.length][ch.length][2];
|
||||
for (int i = 0; i < ch.length; i++) {
|
||||
if (ch[i] == '0' || ch[i] == '1') {
|
||||
dp[i][i][ch[i] - '0'] = 1;
|
||||
class Solution {
|
||||
public int countEval(String s, int result) {
|
||||
if (s.length() == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
for (int len = 2; len <= ch.length; len += 2) {
|
||||
for (int start = 0; start <= ch.length - len; start += 2) {
|
||||
int end = start + len;
|
||||
for (int k = start + 1; k <= end - 1; k += 2) {
|
||||
if (ch[k] == '&') {
|
||||
dp[start][end][0] += dp[start][k - 1][0] * dp[k + 1][end][0] + dp[start][k - 1][0] * dp[k + 1][end][1] + dp[start][k - 1][1] * dp[k + 1][end][0];
|
||||
dp[start][end][1] += dp[start][k - 1][1] * dp[k + 1][end][1];
|
||||
}
|
||||
if (ch[k] == '|') {
|
||||
dp[start][end][0] += dp[start][k - 1][0] * dp[k + 1][end][0];
|
||||
dp[start][end][1] += dp[start][k - 1][0] * dp[k + 1][end][1] + dp[start][k - 1][1] * dp[k + 1][end][0] + dp[start][k - 1][1] * dp[k + 1][end][1];
|
||||
}
|
||||
if (ch[k] == '^') {
|
||||
dp[start][end][0] += dp[start][k - 1][0] * dp[k + 1][end][0] + dp[start][k - 1][1] * dp[k + 1][end][1];
|
||||
dp[start][end][1] += dp[start][k - 1][1] * dp[k + 1][end][0] + dp[start][k - 1][0] * dp[k + 1][end][1];
|
||||
if (s.length() == 1) {
|
||||
return (s.charAt(0) - '0') == result ? 1 : 0;
|
||||
}
|
||||
char[] ch = s.toCharArray();
|
||||
int[][][] dp = new int[ch.length][ch.length][2];
|
||||
for (int i = 0; i < ch.length; i++) {
|
||||
if (ch[i] == '0' || ch[i] == '1') {
|
||||
dp[i][i][ch[i] - '0'] = 1;
|
||||
}
|
||||
}
|
||||
for (int len = 2; len <= ch.length; len += 2) {
|
||||
for (int start = 0; start <= ch.length - len; start += 2) {
|
||||
int end = start + len;
|
||||
for (int k = start + 1; k <= end - 1; k += 2) {
|
||||
if (ch[k] == '&') {
|
||||
dp[start][end][0] += dp[start][k - 1][0] * dp[k + 1][end][0] + dp[start][k - 1][0] * dp[k + 1][end][1] + dp[start][k - 1][1] * dp[k + 1][end][0];
|
||||
dp[start][end][1] += dp[start][k - 1][1] * dp[k + 1][end][1];
|
||||
}
|
||||
if (ch[k] == '|') {
|
||||
dp[start][end][0] += dp[start][k - 1][0] * dp[k + 1][end][0];
|
||||
dp[start][end][1] += dp[start][k - 1][0] * dp[k + 1][end][1] + dp[start][k - 1][1] * dp[k + 1][end][0] + dp[start][k - 1][1] * dp[k + 1][end][1];
|
||||
}
|
||||
if (ch[k] == '^') {
|
||||
dp[start][end][0] += dp[start][k - 1][0] * dp[k + 1][end][0] + dp[start][k - 1][1] * dp[k + 1][end][1];
|
||||
dp[start][end][1] += dp[start][k - 1][1] * dp[k + 1][end][0] + dp[start][k - 1][0] * dp[k + 1][end][1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[0][ch.length - 1][result];
|
||||
}
|
||||
return dp[0][ch.length - 1][result];
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -89,7 +89,7 @@ public class BuildAnArrayWithStackOperations {
|
||||
if (!stack.isEmpty() && stack.peek() == i) {
|
||||
list.add("Push");
|
||||
stack.pop();
|
||||
if(stack.isEmpty()){
|
||||
if (stack.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
|
@ -38,25 +38,26 @@
|
||||
// Related Topics 数组 模拟 👍 2 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//1920:基于排列构建数组
|
||||
class BuildArrayFromPermutation{
|
||||
class BuildArrayFromPermutation {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new BuildArrayFromPermutation().new Solution();
|
||||
}
|
||||
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int[] buildArray(int[] nums) {
|
||||
int size = nums.length;
|
||||
int[] arr = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
arr[i] = nums[nums[i]];
|
||||
class Solution {
|
||||
public int[] buildArray(int[] nums) {
|
||||
int size = nums.length;
|
||||
int[] arr = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
arr[i] = nums[nums[i]];
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -44,7 +44,7 @@ public class Ccw6C7 {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new Ccw6C7().new Solution();
|
||||
solution.paintingPlan(2,2);
|
||||
solution.paintingPlan(2, 2);
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
|
@ -98,7 +98,7 @@ class CheapestFlightsWithinKStops {
|
||||
if (arr[0] != dst) {
|
||||
use.add(arr[0]);
|
||||
queue.add(new QueueEntity(arr[0], queueEntity.sum + arr[1], use));
|
||||
use.remove((Integer)arr[0]);
|
||||
use.remove((Integer) arr[0]);
|
||||
}
|
||||
sums[arr[0]] = queueEntity.sum + arr[1];
|
||||
}
|
||||
|
@ -58,38 +58,39 @@
|
||||
// 👍 4 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//5948:判断一个括号字符串是否有效
|
||||
class CheckIfAParenthesesStringCanBeValid{
|
||||
class CheckIfAParenthesesStringCanBeValid {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CheckIfAParenthesesStringCanBeValid().new Solution();
|
||||
}
|
||||
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public boolean canBeValid(String s, String locked) {
|
||||
int n = s.length();
|
||||
int min = 0;
|
||||
int max = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (locked.charAt(i) == '0') {
|
||||
min = Math.max(min - 1, 0);
|
||||
max += 1;
|
||||
} else if (s.charAt(i) == '(') {
|
||||
min += 1;
|
||||
max += 1;
|
||||
} else {
|
||||
min = Math.max(min - 1, 0);
|
||||
max -= 1;
|
||||
if (max < 0) {
|
||||
return false;
|
||||
class Solution {
|
||||
public boolean canBeValid(String s, String locked) {
|
||||
int n = s.length();
|
||||
int min = 0;
|
||||
int max = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (locked.charAt(i) == '0') {
|
||||
min = Math.max(min - 1, 0);
|
||||
max += 1;
|
||||
} else if (s.charAt(i) == '(') {
|
||||
min += 1;
|
||||
max += 1;
|
||||
} else {
|
||||
min = Math.max(min - 1, 0);
|
||||
max -= 1;
|
||||
if (max < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return min == 0 && max % 2 == 0;
|
||||
}
|
||||
return min == 0 && max % 2 == 0;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -30,30 +30,31 @@
|
||||
// Related Topics 哈希表 字符串 计数 👍 0 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//1941:检查是否所有字符出现次数相同
|
||||
class CheckIfAllCharactersHaveEqualNumberOfOccurrences{
|
||||
class CheckIfAllCharactersHaveEqualNumberOfOccurrences {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CheckIfAllCharactersHaveEqualNumberOfOccurrences().new Solution();
|
||||
}
|
||||
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public boolean areOccurrencesEqual(String s) {
|
||||
int[] arr = new int[26];
|
||||
for (char ch : s.toCharArray()) {
|
||||
arr[ch - 'a']++;
|
||||
}
|
||||
int num = arr[s.charAt(0) - 'a'];
|
||||
for (int j : arr) {
|
||||
if (j > 0 && j != num) {
|
||||
return false;
|
||||
class Solution {
|
||||
public boolean areOccurrencesEqual(String s) {
|
||||
int[] arr = new int[26];
|
||||
for (char ch : s.toCharArray()) {
|
||||
arr[ch - 'a']++;
|
||||
}
|
||||
int num = arr[s.charAt(0) - 'a'];
|
||||
for (int j : arr) {
|
||||
if (j > 0 && j != num) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -40,7 +40,7 @@ public class CheckIfItIsAStraightLine {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new CheckIfItIsAStraightLine().new Solution();
|
||||
// TO TEST
|
||||
TwoArray twoArray = new TwoArray("[[0,1],[1,3],[-4,-7],[5,11]]",true);
|
||||
TwoArray twoArray = new TwoArray("[[0,1],[1,3],[-4,-7],[5,11]]", true);
|
||||
System.out.println(solution.checkStraightLine(twoArray.getArr()));
|
||||
}
|
||||
|
||||
|
@ -38,26 +38,27 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
//1832:判断句子是否为全字母句
|
||||
public class CheckIfTheSentenceIsPangram{
|
||||
public class CheckIfTheSentenceIsPangram {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CheckIfTheSentenceIsPangram().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public boolean checkIfPangram(String sentence) {
|
||||
List<Character> list = new ArrayList<>();
|
||||
int num = 0;
|
||||
for (char ch : sentence.toCharArray()) {
|
||||
if (ch >= 'a' && ch <= 'z' && !list.contains(ch)) {
|
||||
list.add(ch);
|
||||
num++;
|
||||
class Solution {
|
||||
public boolean checkIfPangram(String sentence) {
|
||||
List<Character> list = new ArrayList<>();
|
||||
int num = 0;
|
||||
for (char ch : sentence.toCharArray()) {
|
||||
if (ch >= 'a' && ch <= 'z' && !list.contains(ch)) {
|
||||
list.add(ch);
|
||||
num++;
|
||||
}
|
||||
}
|
||||
return num == 26;
|
||||
}
|
||||
return num==26;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -57,15 +57,17 @@
|
||||
// 👍 4 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//1880:检查某单词是否等于两单词之和
|
||||
public class CheckIfWordEqualsSummationOfTwoWords{
|
||||
public class CheckIfWordEqualsSummationOfTwoWords {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CheckIfWordEqualsSummationOfTwoWords().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
class Solution {
|
||||
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
|
||||
return trans(firstWord) + trans(secondWord) == trans(targetWord);
|
||||
}
|
||||
@ -80,7 +82,7 @@ class Solution {
|
||||
}
|
||||
return Integer.parseInt(numStr.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -46,27 +46,28 @@ package leetcode.editor.cn;
|
||||
import java.util.*;
|
||||
|
||||
//LCP 07:传递信息
|
||||
public class ChuanDiXinXi{
|
||||
public class ChuanDiXinXi {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new ChuanDiXinXi().new Solution();
|
||||
System.out.println(solution.numWays(3,new int[][]{{0,2},{2,1}},2));
|
||||
System.out.println(solution.numWays(5,new int[][]{{0,2},{2,1},{3,4},{2,3},{1,4},{2,0},{0,4}},3));
|
||||
System.out.println(solution.numWays(3, new int[][]{{0, 2}, {2, 1}}, 2));
|
||||
System.out.println(solution.numWays(5, new int[][]{{0, 2}, {2, 1}, {3, 4}, {2, 3}, {1, 4}, {2, 0}, {0, 4}}, 3));
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int numWays(int n, int[][] relation, int k) {
|
||||
int[][] dp =new int[k+1][n];
|
||||
dp[0][0] =1;
|
||||
for (int i = 0; i < k; i++) {
|
||||
for (int[] ints : relation) {
|
||||
dp[i + 1][ints[1]] += dp[i][ints[0]];
|
||||
class Solution {
|
||||
public int numWays(int n, int[][] relation, int k) {
|
||||
int[][] dp = new int[k + 1][n];
|
||||
dp[0][0] = 1;
|
||||
for (int i = 0; i < k; i++) {
|
||||
for (int[] ints : relation) {
|
||||
dp[i + 1][ints[1]] += dp[i][ints[0]];
|
||||
}
|
||||
}
|
||||
return dp[k][n - 1];
|
||||
}
|
||||
return dp[k][n-1];
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -78,12 +78,12 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
//133:克隆图
|
||||
class CloneGraph{
|
||||
class CloneGraph {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CloneGraph().new Solution();
|
||||
}
|
||||
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/*
|
||||
@ -106,26 +106,27 @@ class Node {
|
||||
}
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
private HashMap<Node, Node> use = new HashMap<>();
|
||||
public Node cloneGraph(Node node) {
|
||||
if (node == null) {
|
||||
return node;
|
||||
}
|
||||
class Solution {
|
||||
private HashMap<Node, Node> use = new HashMap<>();
|
||||
|
||||
if (use.containsKey(node)) {
|
||||
return use.get(node);
|
||||
}
|
||||
public Node cloneGraph(Node node) {
|
||||
if (node == null) {
|
||||
return node;
|
||||
}
|
||||
|
||||
Node cloneNode = new Node(node.val, new ArrayList());
|
||||
use.put(node, cloneNode);
|
||||
if (use.containsKey(node)) {
|
||||
return use.get(node);
|
||||
}
|
||||
|
||||
for (Node neighbor: node.children) {
|
||||
cloneNode.children.add(cloneGraph(neighbor));
|
||||
Node cloneNode = new Node(node.val, new ArrayList());
|
||||
use.put(node, cloneNode);
|
||||
|
||||
for (Node neighbor : node.children) {
|
||||
cloneNode.children.add(cloneGraph(neighbor));
|
||||
}
|
||||
return cloneNode;
|
||||
}
|
||||
return cloneNode;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -52,9 +52,9 @@ public class CoinChange2 {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CoinChange2().new Solution();
|
||||
System.out.println(solution.change(5,new int[]{1,2,5}));
|
||||
System.out.println(solution.change(3,new int[]{2}));
|
||||
System.out.println(solution.change(10,new int[]{10}));
|
||||
System.out.println(solution.change(5, new int[]{1, 2, 5}));
|
||||
System.out.println(solution.change(3, new int[]{2}));
|
||||
System.out.println(solution.change(10, new int[]{10}));
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
|
@ -95,7 +95,7 @@ public class CombinationSum {
|
||||
use.add(candidates[i]);
|
||||
backtrack(candidates, i, use, sum + candidates[i], target);
|
||||
use.remove(use.size() - 1);
|
||||
if(bl){
|
||||
if (bl) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -51,7 +51,8 @@ public class Combinations {
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
List<List<Integer>> list = new ArrayList<>();
|
||||
// public List<List<Integer>> combine(int n, int k) {
|
||||
|
||||
// public List<List<Integer>> combine(int n, int k) {
|
||||
// int[] nums = new int[n];
|
||||
// for (int i = 0; i < n; i++) {
|
||||
// nums[i] = i + 1;
|
||||
|
@ -75,7 +75,7 @@ class CompareVersionNumbers {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CompareVersionNumbers().new Solution();
|
||||
solution.compareVersion("0.1","1.1");
|
||||
solution.compareVersion("0.1", "1.1");
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
|
@ -22,37 +22,38 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
//剑指 Offer 06:从尾到头打印链表
|
||||
class CongWeiDaoTouDaYinLianBiaoLcof{
|
||||
class CongWeiDaoTouDaYinLianBiaoLcof {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CongWeiDaoTouDaYinLianBiaoLcof().new Solution();
|
||||
}
|
||||
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) { val = x; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public int[] reversePrint(ListNode head) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
while (head != null) {
|
||||
list.add(head.val);
|
||||
head = head.next;
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) { val = x; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public int[] reversePrint(ListNode head) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
while (head != null) {
|
||||
list.add(head.val);
|
||||
head = head.next;
|
||||
}
|
||||
int size = list.size();
|
||||
int[] arr = new int[size];
|
||||
for (int i = size - 1; i >= 0; i--) {
|
||||
arr[i] = list.get(size - 1 - i);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
int size = list.size();
|
||||
int[] arr = new int[size];
|
||||
for (int i = size - 1; i >= 0; i--) {
|
||||
arr[i] = list.get(size - 1 - i);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -54,7 +54,7 @@ class ContainsDuplicate {
|
||||
public boolean containsDuplicate(int[] nums) {
|
||||
Arrays.sort(nums);
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
if(nums[i]==nums[i-1]){
|
||||
if (nums[i] == nums[i - 1]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -54,32 +54,33 @@ package leetcode.editor.cn;
|
||||
import com.code.leet.entiy.ListNode;
|
||||
|
||||
//1290:二进制链表转整数
|
||||
public class ConvertBinaryNumberInALinkedListToInteger{
|
||||
public class ConvertBinaryNumberInALinkedListToInteger {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new ConvertBinaryNumberInALinkedListToInteger().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) { val = x; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public int getDecimalValue(ListNode head) {
|
||||
int num = 0;
|
||||
while (head != null) {
|
||||
num <<= 1;
|
||||
num |= head.val;
|
||||
head = head.next;
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) { val = x; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public int getDecimalValue(ListNode head) {
|
||||
int num = 0;
|
||||
while (head != null) {
|
||||
num <<= 1;
|
||||
num |= head.val;
|
||||
head = head.next;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -38,46 +38,47 @@ package leetcode.editor.cn;
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
//108:将有序数组转换为二叉搜索树
|
||||
class ConvertSortedArrayToBinarySearchTree{
|
||||
class ConvertSortedArrayToBinarySearchTree {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new ConvertSortedArrayToBinarySearchTree().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* public class TreeNode {
|
||||
* int val;
|
||||
* TreeNode left;
|
||||
* TreeNode right;
|
||||
* TreeNode() {}
|
||||
* TreeNode(int val) { this.val = val; }
|
||||
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
* this.val = val;
|
||||
* this.left = left;
|
||||
* this.right = right;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
|
||||
public TreeNode sortedArrayToBST(int[] nums) {
|
||||
return sortedArrayToBST(nums, 0, nums.length);
|
||||
}
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* public class TreeNode {
|
||||
* int val;
|
||||
* TreeNode left;
|
||||
* TreeNode right;
|
||||
* TreeNode() {}
|
||||
* TreeNode(int val) { this.val = val; }
|
||||
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
* this.val = val;
|
||||
* this.left = left;
|
||||
* this.right = right;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
|
||||
private TreeNode sortedArrayToBST(int[] nums, int start, int end) {
|
||||
if (start == end) {
|
||||
return null;
|
||||
public TreeNode sortedArrayToBST(int[] nums) {
|
||||
return sortedArrayToBST(nums, 0, nums.length);
|
||||
}
|
||||
int mid = (start + end) >>> 1;
|
||||
TreeNode root = new TreeNode(nums[mid]);
|
||||
root.left = sortedArrayToBST(nums, start, mid);
|
||||
root.right = sortedArrayToBST(nums, mid + 1, end);
|
||||
|
||||
return root;
|
||||
private TreeNode sortedArrayToBST(int[] nums, int start, int end) {
|
||||
if (start == end) {
|
||||
return null;
|
||||
}
|
||||
int mid = (start + end) >>> 1;
|
||||
TreeNode root = new TreeNode(nums[mid]);
|
||||
root.left = sortedArrayToBST(nums, start, mid);
|
||||
root.right = sortedArrayToBST(nums, mid + 1, end);
|
||||
|
||||
return root;
|
||||
}
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -74,7 +74,7 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
//138:复制带随机指针的链表
|
||||
public class CopyListWithRandomPointer{
|
||||
public class CopyListWithRandomPointer {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CopyListWithRandomPointer().new Solution();
|
||||
@ -96,26 +96,26 @@ class Node {
|
||||
}
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public Node copyRandomList(Node head) {
|
||||
Map<Node, Node> map = new HashMap<>();
|
||||
return copy(head, map);
|
||||
}
|
||||
class Solution {
|
||||
public Node copyRandomList(Node head) {
|
||||
Map<Node, Node> map = new HashMap<>();
|
||||
return copy(head, map);
|
||||
}
|
||||
|
||||
private Node copy(Node head, Map<Node, Node> map) {
|
||||
if (head == null) {
|
||||
return null;
|
||||
private Node copy(Node head, Map<Node, Node> map) {
|
||||
if (head == null) {
|
||||
return null;
|
||||
}
|
||||
if (map.containsKey(head)) {
|
||||
return map.get(head);
|
||||
}
|
||||
Node node = new Node(head.val);
|
||||
map.put(head, node);
|
||||
node.next = copy(head.next, map);
|
||||
node.random = copy(head.random, map);
|
||||
return node;
|
||||
}
|
||||
if (map.containsKey(head)) {
|
||||
return map.get(head);
|
||||
}
|
||||
Node node = new Node(head.val);
|
||||
map.put(head, node);
|
||||
node.next = copy(head.next, map);
|
||||
node.random = copy(head.random, map);
|
||||
return node;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -48,26 +48,27 @@
|
||||
// Related Topics 数组 前缀和 👍 218 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//1109:航班预订统计
|
||||
class CorporateFlightBookings{
|
||||
class CorporateFlightBookings {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CorporateFlightBookings().new Solution();
|
||||
}
|
||||
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int[] corpFlightBookings(int[][] bookings, int n) {
|
||||
int[] result = new int[n];
|
||||
for (int[] booking : bookings) {
|
||||
for (int j = booking[0] - 1; j < booking[1]; j++) {
|
||||
result[j] += booking[2];
|
||||
class Solution {
|
||||
public int[] corpFlightBookings(int[][] bookings, int n) {
|
||||
int[] result = new int[n];
|
||||
for (int[] booking : bookings) {
|
||||
for (int j = booking[0] - 1; j < booking[1]; j++) {
|
||||
result[j] += booking[2];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -42,14 +42,15 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
//1814:统计一个数组中好对子的数目
|
||||
public class CountNicePairsInAnArray{
|
||||
public class CountNicePairsInAnArray {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CountNicePairsInAnArray().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
class Solution {
|
||||
public int countNicePairs(int[] nums) {
|
||||
Map<Integer, Long> map = new HashMap<>();
|
||||
for (int num : nums) {
|
||||
@ -67,14 +68,15 @@ class Solution {
|
||||
count += value * (value - 1) / 2;
|
||||
}
|
||||
}
|
||||
return (int)(count % (Math.pow(10, 9) + 7));
|
||||
return (int) (count % (Math.pow(10, 9) + 7));
|
||||
}
|
||||
private int revert(int num){
|
||||
String str = ""+num;
|
||||
|
||||
private int revert(int num) {
|
||||
String str = "" + num;
|
||||
str = new StringBuilder(str).reverse().toString();
|
||||
return Integer.parseInt(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -34,7 +34,7 @@ class CountOfSmallerNumbersAfterSelf {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CountOfSmallerNumbersAfterSelf().new Solution();
|
||||
System.out.println(solution.countSmaller(new int[]{1,-3,-2}));
|
||||
System.out.println(solution.countSmaller(new int[]{1, -3, -2}));
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
|
@ -28,31 +28,32 @@
|
||||
// Related Topics 数学 枚举 👍 5 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//1925:统计平方和三元组的数目
|
||||
class CountSquareSumTriples{
|
||||
class CountSquareSumTriples {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CountSquareSumTriples().new Solution();
|
||||
}
|
||||
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int countTriples(int n) {
|
||||
int count = 0;
|
||||
for (int i = n; i > 0; i--) {
|
||||
for (int j = i - 1; j > 0; j--) {
|
||||
int sum = i * i - j * j;
|
||||
int num = (int) Math.sqrt(sum);
|
||||
if (sum == num * num) {
|
||||
count++;
|
||||
class Solution {
|
||||
public int countTriples(int n) {
|
||||
int count = 0;
|
||||
for (int i = n; i > 0; i--) {
|
||||
for (int j = i - 1; j > 0; j--) {
|
||||
int sum = i * i - j * j;
|
||||
int num = (int) Math.sqrt(sum);
|
||||
if (sum == num * num) {
|
||||
count++;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -56,11 +56,11 @@ public class CountVowelsPermutation {
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int countVowelPermutation(int n) {
|
||||
int mod = (int)(1e9+7);
|
||||
int mod = (int) (1e9 + 7);
|
||||
long[][] nums = new long[n][5];
|
||||
Arrays.fill(nums[0], 1);
|
||||
for (int i = 1; i < n; i++) {
|
||||
nums[i][0] = (nums[i - 1][1] + nums[i - 1][2]+nums[i-1][4]) % mod;
|
||||
nums[i][0] = (nums[i - 1][1] + nums[i - 1][2] + nums[i - 1][4]) % mod;
|
||||
nums[i][1] = (nums[i - 1][0] + nums[i - 1][2]) % mod;
|
||||
nums[i][2] = (nums[i - 1][1] + nums[i - 1][3]) % mod;
|
||||
nums[i][3] = nums[i - 1][2];
|
||||
@ -70,7 +70,7 @@ public class CountVowelsPermutation {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
result += nums[n - 1][i];
|
||||
}
|
||||
return (int)(result % mod);
|
||||
return (int) (result % mod);
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
@ -62,7 +62,7 @@ public class CountingBits {
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int[] countBits(int n) {
|
||||
int[] arrs = new int[n+1];
|
||||
int[] arrs = new int[n + 1];
|
||||
for (int i = 0; i <= n; i++) {
|
||||
arrs[i] = Integer.bitCount(i);
|
||||
}
|
||||
|
@ -75,11 +75,11 @@ public class CousinsInBinaryTree {
|
||||
*/
|
||||
class Solution {
|
||||
public boolean isCousins(TreeNode root, int x, int y) {
|
||||
int[][] paraent = getParent(root, x, y, new int[2][2], 1,-1);
|
||||
int[][] paraent = getParent(root, x, y, new int[2][2], 1, -1);
|
||||
return paraent[0][0] != paraent[1][0] && paraent[0][1] == paraent[1][1];
|
||||
}
|
||||
|
||||
private int[][] getParent(TreeNode root, int x, int y, int[][] paraent, int deep,int before) {
|
||||
private int[][] getParent(TreeNode root, int x, int y, int[][] paraent, int deep, int before) {
|
||||
if (paraent[1][1] > 0) {
|
||||
return paraent;
|
||||
}
|
||||
@ -95,13 +95,13 @@ public class CousinsInBinaryTree {
|
||||
}
|
||||
}
|
||||
if (root.left != null) {
|
||||
paraent = getParent(root.left, x, y, paraent, deep + 1,root.val);
|
||||
paraent = getParent(root.left, x, y, paraent, deep + 1, root.val);
|
||||
}
|
||||
if (paraent[1][1] > 0) {
|
||||
return paraent;
|
||||
}
|
||||
if (root.right != null) {
|
||||
paraent = getParent(root.right, x, y, paraent, deep + 1,root.val);
|
||||
paraent = getParent(root.right, x, y, paraent, deep + 1, root.val);
|
||||
}
|
||||
return paraent;
|
||||
}
|
||||
|
@ -63,11 +63,11 @@ public class CutOffTreesForGolfEvent {
|
||||
Solution solution = new CutOffTreesForGolfEvent().new Solution();
|
||||
// TO TEST
|
||||
List<List<Integer>> forest = Arrays.asList(
|
||||
Arrays.asList(54581641,64080174,24346381,69107959)
|
||||
, Arrays.asList(86374198,61363882,68783324,79706116)
|
||||
, Arrays.asList(668150, 92178815,89819108,94701471)
|
||||
, Arrays.asList(83920491,22724204,46281641,47531096)
|
||||
, Arrays.asList(89078499,18904913,25462145,60813308));
|
||||
Arrays.asList(54581641, 64080174, 24346381, 69107959)
|
||||
, Arrays.asList(86374198, 61363882, 68783324, 79706116)
|
||||
, Arrays.asList(668150, 92178815, 89819108, 94701471)
|
||||
, Arrays.asList(83920491, 22724204, 46281641, 47531096)
|
||||
, Arrays.asList(89078499, 18904913, 25462145, 60813308));
|
||||
solution.cutOffTree(forest);
|
||||
}
|
||||
|
||||
|
@ -100,7 +100,7 @@ public class DecodedStringAtIndex {
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = length-1; i >= 0; --i) {
|
||||
for (int i = length - 1; i >= 0; --i) {
|
||||
K %= size;
|
||||
if (K == 0 && Character.isLetter(S.charAt(i))) {
|
||||
return Character.toString(S.charAt(i));
|
||||
|
@ -74,7 +74,7 @@ public class DeleteColumnsToMakeSorted {
|
||||
int count = 0;
|
||||
for (int i = 0; i < strs[0].length(); i++) {
|
||||
for (int j = 1; j < strs.length; j++) {
|
||||
if(strs[j].charAt(i)<strs[j-1].charAt(i)){
|
||||
if (strs[j].charAt(i) < strs[j - 1].charAt(i)) {
|
||||
count++;
|
||||
break;
|
||||
}
|
||||
|
@ -22,27 +22,28 @@ package leetcode.editor.cn;
|
||||
import com.code.leet.entiy.ListNode;
|
||||
|
||||
//面试题 02.03:删除中间节点
|
||||
public class DeleteMiddleNodeLcci{
|
||||
public class DeleteMiddleNodeLcci {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new DeleteMiddleNodeLcci().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) { val = x; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public void deleteNode(ListNode node) {
|
||||
node.val = node.next.val;
|
||||
node.next = node.next.next;
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) { val = x; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public void deleteNode(ListNode node) {
|
||||
node.val = node.next.val;
|
||||
node.next = node.next.next;
|
||||
}
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -40,27 +40,28 @@ package leetcode.editor.cn;
|
||||
import com.code.leet.entiy.ListNode;
|
||||
|
||||
//237:删除链表中的节点
|
||||
public class DeleteNodeInALinkedList{
|
||||
public class DeleteNodeInALinkedList {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new DeleteNodeInALinkedList().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) { val = x; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public void deleteNode(ListNode node) {
|
||||
node.val = node.next.val;
|
||||
node.next = node.next.next;
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) { val = x; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public void deleteNode(ListNode node) {
|
||||
node.val = node.next.val;
|
||||
node.next = node.next.next;
|
||||
}
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -82,7 +82,7 @@ public class DesignAStackWithIncrementOperation {
|
||||
}
|
||||
|
||||
public int pop() {
|
||||
if(index==0){
|
||||
if (index == 0) {
|
||||
return -1;
|
||||
}
|
||||
index--;
|
||||
|
@ -69,14 +69,15 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
//1797:设计一个验证系统
|
||||
public class DesignAuthenticationManager{
|
||||
public class DesignAuthenticationManager {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
// Solution solution = new DesignAuthenticationManager().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class AuthenticationManager {
|
||||
class AuthenticationManager {
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
int timeToLive;
|
||||
|
||||
@ -96,14 +97,14 @@ class AuthenticationManager {
|
||||
|
||||
public int countUnexpiredTokens(int currentTime) {
|
||||
int num = 0;
|
||||
for (String key:map.keySet()) {
|
||||
if(map.get(key)>currentTime){
|
||||
for (String key : map.keySet()) {
|
||||
if (map.get(key) > currentTime) {
|
||||
num++;
|
||||
}
|
||||
}
|
||||
return num;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your AuthenticationManager object will be instantiated and called as such:
|
||||
|
@ -82,7 +82,7 @@ public class DesignFrontMiddleBackQueue {
|
||||
}
|
||||
|
||||
public void pushMiddle(int val) {
|
||||
list.add(list.size()/2, val);
|
||||
list.add(list.size() / 2, val);
|
||||
}
|
||||
|
||||
public void pushBack(int val) {
|
||||
@ -100,14 +100,14 @@ public class DesignFrontMiddleBackQueue {
|
||||
if (list.isEmpty()) {
|
||||
return -1;
|
||||
}
|
||||
return list.remove((list.size()-1) / 2);
|
||||
return list.remove((list.size() - 1) / 2);
|
||||
}
|
||||
|
||||
public int popBack() {
|
||||
if (list.isEmpty()) {
|
||||
return -1;
|
||||
}
|
||||
return list.remove(list.size()-1);
|
||||
return list.remove(list.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -51,46 +51,51 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
//705:设计哈希集合
|
||||
public class DesignHashset{
|
||||
public class DesignHashset {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
// Solution solution = new DesignHashset().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class MyHashSet {
|
||||
class MyHashSet {
|
||||
List<Integer> list;
|
||||
|
||||
/** Initialize your data structure here. */
|
||||
public MyHashSet() {
|
||||
list = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void add(int key) {
|
||||
if(list.contains(key)){
|
||||
return;
|
||||
/**
|
||||
* Initialize your data structure here.
|
||||
*/
|
||||
public MyHashSet() {
|
||||
list = new ArrayList<>();
|
||||
}
|
||||
list.add(key);
|
||||
}
|
||||
|
||||
public void remove(int key) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if(list.get(i)==key){
|
||||
list.remove(i);
|
||||
|
||||
public void add(int key) {
|
||||
if (list.contains(key)) {
|
||||
return;
|
||||
}
|
||||
list.add(key);
|
||||
}
|
||||
|
||||
public void remove(int key) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (list.get(i) == key) {
|
||||
list.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns true if this set contains the specified element */
|
||||
public boolean contains(int key) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if(list.get(i)==key){
|
||||
return true;
|
||||
|
||||
/**
|
||||
* Returns true if this set contains the specified element
|
||||
*/
|
||||
public boolean contains(int key) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (list.get(i) == key) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your MyHashSet object will be instantiated and called as such:
|
||||
|
@ -68,7 +68,7 @@ class DestinationCity {
|
||||
for (List<String> path : paths) {
|
||||
if (!use.contains(path.get(1))) {
|
||||
result.add(path.get(1));
|
||||
}else {
|
||||
} else {
|
||||
use.remove(path.get(1));
|
||||
}
|
||||
if (!result.contains(path.get(0))) {
|
||||
|
@ -44,24 +44,26 @@
|
||||
// 👍 4 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//1812:判断国际象棋棋盘中一个格子的颜色
|
||||
public class DetermineColorOfAChessboardSquare{
|
||||
public class DetermineColorOfAChessboardSquare {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new DetermineColorOfAChessboardSquare().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public boolean squareIsWhite(String coordinates) {
|
||||
char[] chars = coordinates.toCharArray();
|
||||
if ((chars[0] - chars[1]) % 2 == 0) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
class Solution {
|
||||
public boolean squareIsWhite(String coordinates) {
|
||||
char[] chars = coordinates.toCharArray();
|
||||
if ((chars[0] - chars[1]) % 2 == 0) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -65,40 +65,40 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
//1921:消灭怪物的最大数量
|
||||
class EliminateMaximumNumberOfMonsters{
|
||||
class EliminateMaximumNumberOfMonsters {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new EliminateMaximumNumberOfMonsters().new Solution();
|
||||
}
|
||||
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int eliminateMaximum(int[] dist, int[] speed) {
|
||||
int size = dist.length;
|
||||
int[] times = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
int time = dist[i] / speed[i];
|
||||
times[i] = dist[i] % speed[i] == 0 ? time - 1 : time;
|
||||
}
|
||||
Arrays.sort(times);
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
map.put(times[i], map.getOrDefault(times[i], 0) + 1);
|
||||
}
|
||||
int index = 1;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if(map.containsKey(i)){
|
||||
if(map.get(i)>index){
|
||||
return i+1;
|
||||
}
|
||||
index-=map.get(i);
|
||||
class Solution {
|
||||
public int eliminateMaximum(int[] dist, int[] speed) {
|
||||
int size = dist.length;
|
||||
int[] times = new int[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
int time = dist[i] / speed[i];
|
||||
times[i] = dist[i] % speed[i] == 0 ? time - 1 : time;
|
||||
}
|
||||
index++;
|
||||
Arrays.sort(times);
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
map.put(times[i], map.getOrDefault(times[i], 0) + 1);
|
||||
}
|
||||
int index = 1;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (map.containsKey(i)) {
|
||||
if (map.get(i) > index) {
|
||||
return i + 1;
|
||||
}
|
||||
index -= map.get(i);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -38,7 +38,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
//690:员工的重要性
|
||||
public class EmployeeImportance{
|
||||
public class EmployeeImportance {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new EmployeeImportance().new Solution();
|
||||
@ -54,24 +54,26 @@ class Employee {
|
||||
};
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
Map<Integer,Employee> map = new HashMap<>();
|
||||
public int getImportance(List<Employee> employees, int id) {
|
||||
for(Employee employee:employees){
|
||||
map.put(employee.id,employee);
|
||||
class Solution {
|
||||
Map<Integer, Employee> map = new HashMap<>();
|
||||
|
||||
public int getImportance(List<Employee> employees, int id) {
|
||||
for (Employee employee : employees) {
|
||||
map.put(employee.id, employee);
|
||||
}
|
||||
return dfs(id);
|
||||
}
|
||||
return dfs(id);
|
||||
}
|
||||
private int dfs(int id){
|
||||
Employee employee = map.get(id);
|
||||
int total = employee.importance;
|
||||
List<Integer> subordinates = employee.subordinates;
|
||||
for(int subordinaty:subordinates){
|
||||
total+=dfs(subordinaty);
|
||||
|
||||
private int dfs(int id) {
|
||||
Employee employee = map.get(id);
|
||||
int total = employee.importance;
|
||||
List<Integer> subordinates = employee.subordinates;
|
||||
for (int subordinaty : subordinates) {
|
||||
total += dfs(subordinaty);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -88,37 +88,37 @@ public class EvaluateReversePolishNotation {
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int evalRPN(String[] tokens) {
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
String op = "+-*/";
|
||||
int result = 0;
|
||||
for (String str : tokens) {
|
||||
if (op.contains(str) && !stack.isEmpty()) {
|
||||
int op2 = stack.pop();
|
||||
int op1 = stack.pop();
|
||||
switch (str) {
|
||||
case "+":
|
||||
result = op1 + op2;
|
||||
break;
|
||||
case "-":
|
||||
result = op1 - op2;
|
||||
break;
|
||||
case "*":
|
||||
result = op1 * op2;
|
||||
break;
|
||||
case "/":
|
||||
result = op1 / op2;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
stack.push(result);
|
||||
} else {
|
||||
stack.push(Integer.valueOf(str));
|
||||
public int evalRPN(String[] tokens) {
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
String op = "+-*/";
|
||||
int result = 0;
|
||||
for (String str : tokens) {
|
||||
if (op.contains(str) && !stack.isEmpty()) {
|
||||
int op2 = stack.pop();
|
||||
int op1 = stack.pop();
|
||||
switch (str) {
|
||||
case "+":
|
||||
result = op1 + op2;
|
||||
break;
|
||||
case "-":
|
||||
result = op1 - op2;
|
||||
break;
|
||||
case "*":
|
||||
result = op1 * op2;
|
||||
break;
|
||||
case "/":
|
||||
result = op1 / op2;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
stack.push(result);
|
||||
} else {
|
||||
stack.push(Integer.valueOf(str));
|
||||
}
|
||||
return stack.pop();
|
||||
}
|
||||
return stack.pop();
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
|
@ -48,7 +48,7 @@ public class ExcelSheetColumnTitle {
|
||||
StringBuilder result = new StringBuilder();
|
||||
while (columnNumber > 0) {
|
||||
int num = (columnNumber - 1) % 26 + 1;
|
||||
result.insert(0, (char)((num - 1) + 'A'));
|
||||
result.insert(0, (char) ((num - 1) + 'A'));
|
||||
columnNumber = (columnNumber - num) / 26;
|
||||
}
|
||||
return result.toString();
|
||||
|
@ -24,38 +24,39 @@ package leetcode.editor.cn;
|
||||
import com.code.leet.entiy.ListNode;
|
||||
|
||||
//剑指 Offer 24:反转链表
|
||||
public class FanZhuanLianBiaoLcof{
|
||||
public class FanZhuanLianBiaoLcof {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new FanZhuanLianBiaoLcof().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) { val = x; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode reverseList(ListNode head) {
|
||||
ListNode newHead = null;
|
||||
ListNode p = head;
|
||||
ListNode pPrev = null;
|
||||
while (p != null) {
|
||||
ListNode temp = p.next;
|
||||
if (temp == null) {
|
||||
newHead = p;
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) { val = x; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode reverseList(ListNode head) {
|
||||
ListNode newHead = null;
|
||||
ListNode p = head;
|
||||
ListNode pPrev = null;
|
||||
while (p != null) {
|
||||
ListNode temp = p.next;
|
||||
if (temp == null) {
|
||||
newHead = p;
|
||||
}
|
||||
p.next = pPrev;
|
||||
pPrev = p;
|
||||
p = temp;
|
||||
}
|
||||
p.next = pPrev;
|
||||
pPrev = p;
|
||||
p = temp;
|
||||
return newHead;
|
||||
}
|
||||
return newHead;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -54,7 +54,7 @@ class FindAllAnagramsInAString {
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public List<Integer> findAnagrams(String s, String p) {
|
||||
if(p.length()>s.length()){
|
||||
if (p.length() > s.length()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
int[] pchs = new int[26];
|
||||
|
@ -48,7 +48,7 @@ public class FindFirstAndLastPositionOfElementInSortedArray {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new FindFirstAndLastPositionOfElementInSortedArray().new Solution();
|
||||
solution.searchRange(new int[]{5,7,7,8,8,10},8);//3,4
|
||||
solution.searchRange(new int[]{5, 7, 7, 8, 8, 10}, 8);//3,4
|
||||
solution.searchRange(new int[]{5, 7, 7, 8, 8, 10}, 6);//-1,-1
|
||||
solution.searchRange(new int[]{2, 2}, 3);//-1,-1
|
||||
solution.searchRange(new int[]{1, 4}, 4);//1,1
|
||||
@ -92,7 +92,7 @@ public class FindFirstAndLastPositionOfElementInSortedArray {
|
||||
mid /= 2;
|
||||
}
|
||||
}
|
||||
if(nums[mid] == target){
|
||||
if (nums[mid] == target) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -45,31 +45,32 @@
|
||||
// 👍 2 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//1979:找出数组的最大公约数
|
||||
class FindGreatestCommonDivisorOfArray{
|
||||
class FindGreatestCommonDivisorOfArray {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new FindGreatestCommonDivisorOfArray().new Solution();
|
||||
}
|
||||
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int findGCD(int[] nums) {
|
||||
int max = Integer.MIN_VALUE;
|
||||
int min = Integer.MAX_VALUE;
|
||||
for (int num : nums) {
|
||||
max = Math.max(max, num);
|
||||
min = Math.min(min, num);
|
||||
}
|
||||
for (int i = min; i >= 1; i--) {
|
||||
if (max % i == 0 && min % i == 0) {
|
||||
return i;
|
||||
class Solution {
|
||||
public int findGCD(int[] nums) {
|
||||
int max = Integer.MIN_VALUE;
|
||||
int min = Integer.MAX_VALUE;
|
||||
for (int num : nums) {
|
||||
max = Math.max(max, num);
|
||||
min = Math.min(min, num);
|
||||
}
|
||||
for (int i = min; i >= 1; i--) {
|
||||
if (max % i == 0 && min % i == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -54,29 +54,30 @@ import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
//436:寻找右区间
|
||||
class FindRightInterval{
|
||||
class FindRightInterval {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new FindRightInterval().new Solution();
|
||||
TwoArray twoArray = new TwoArray("[[1,4],[2,3],[3,4]]",true);
|
||||
TwoArray twoArray = new TwoArray("[[1,4],[2,3],[3,4]]", true);
|
||||
solution.findRightInterval(twoArray.getArr());
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int[] findRightInterval(int[][] intervals) {
|
||||
TreeMap<Integer, Integer> map = new TreeMap<>();
|
||||
for (int i = 0; i < intervals.length; i++) {
|
||||
map.put(intervals[i][0], i);
|
||||
class Solution {
|
||||
public int[] findRightInterval(int[][] intervals) {
|
||||
TreeMap<Integer, Integer> map = new TreeMap<>();
|
||||
for (int i = 0; i < intervals.length; i++) {
|
||||
map.put(intervals[i][0], i);
|
||||
}
|
||||
int[] result = new int[intervals.length];
|
||||
for (int i = 0; i < intervals.length; i++) {
|
||||
Integer key = map.higherKey(intervals[i][1] - 1);
|
||||
result[i] = key == null ? -1 : map.get(key);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
int[] result = new int[intervals.length];
|
||||
for(int i = 0; i < intervals.length; i++){
|
||||
Integer key = map.higherKey(intervals[i][1]-1);
|
||||
result[i] = key == null?-1:map.get(key);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -35,28 +35,28 @@
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//389:找不同
|
||||
public class FindTheDifference{
|
||||
public class FindTheDifference {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new FindTheDifference().new Solution();
|
||||
|
||||
|
||||
}
|
||||
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public char findTheDifference(String s, String t) {
|
||||
int[] chs = new int[26];
|
||||
for (char ch:s.toCharArray()){
|
||||
chs[ch-'a']++;
|
||||
}
|
||||
for (char ch:t.toCharArray()){
|
||||
if(chs[ch-'a']==0){
|
||||
return ch;
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public char findTheDifference(String s, String t) {
|
||||
int[] chs = new int[26];
|
||||
for (char ch : s.toCharArray()) {
|
||||
chs[ch - 'a']++;
|
||||
}
|
||||
chs[ch-'a']--;
|
||||
for (char ch : t.toCharArray()) {
|
||||
if (chs[ch - 'a'] == 0) {
|
||||
return ch;
|
||||
}
|
||||
chs[ch - 'a']--;
|
||||
}
|
||||
return 'a';
|
||||
}
|
||||
return 'a';
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -39,33 +39,34 @@ package leetcode.editor.cn;
|
||||
import java.util.Stack;
|
||||
|
||||
//1673:找出最具竞争力的子序列
|
||||
public class FindTheMostCompetitiveSubsequence{
|
||||
public class FindTheMostCompetitiveSubsequence {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new FindTheMostCompetitiveSubsequence().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int[] mostCompetitive(int[] nums, int k) {
|
||||
int size = nums.length;
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
while (!stack.isEmpty()&&nums[i]< stack.peek()&&k-stack.size()<size-i){
|
||||
stack.pop();
|
||||
class Solution {
|
||||
public int[] mostCompetitive(int[] nums, int k) {
|
||||
int size = nums.length;
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
while (!stack.isEmpty() && nums[i] < stack.peek() && k - stack.size() < size - i) {
|
||||
stack.pop();
|
||||
}
|
||||
if (stack.size() < k) {
|
||||
stack.add(nums[i]);
|
||||
}
|
||||
}
|
||||
if (stack.size() < k) {
|
||||
stack.add(nums[i]);
|
||||
int[] result = new int[k];
|
||||
while (k > 0) {
|
||||
result[k - 1] = stack.pop();
|
||||
k--;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
int[] result = new int[k];
|
||||
while (k>0){
|
||||
result[k-1]=stack.pop();
|
||||
k--;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -44,31 +44,31 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
//1980:找出不同的二进制字符串
|
||||
class FindUniqueBinaryString{
|
||||
class FindUniqueBinaryString {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new FindUniqueBinaryString().new Solution();
|
||||
}
|
||||
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public String findDifferentBinaryString(String[] nums) {
|
||||
int n = nums.length;
|
||||
List<String> list = Arrays.asList(nums);
|
||||
int max = (int) Math.pow(2, n);
|
||||
for (int i = max - 1; i >= 0; i--) {
|
||||
if (!list.contains(Integer.toBinaryString(i))) {
|
||||
String temp = Integer.toBinaryString(i);
|
||||
while (temp.length() < n) {
|
||||
temp = "0" + temp;
|
||||
class Solution {
|
||||
public String findDifferentBinaryString(String[] nums) {
|
||||
int n = nums.length;
|
||||
List<String> list = Arrays.asList(nums);
|
||||
int max = (int) Math.pow(2, n);
|
||||
for (int i = max - 1; i >= 0; i--) {
|
||||
if (!list.contains(Integer.toBinaryString(i))) {
|
||||
String temp = Integer.toBinaryString(i);
|
||||
while (temp.length() < n) {
|
||||
temp = "0" + temp;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -83,7 +83,7 @@ class FindValidMatrixGivenRowAndColumnSums {
|
||||
int[][] arrs = new int[rowSum.length][colSum.length];
|
||||
for (int i = 0; i < rowSum.length; i++) {
|
||||
for (int j = 0; j < colSum.length; j++) {
|
||||
arrs[i][j] = Math.min(rowSum[i],colSum[j]);
|
||||
arrs[i][j] = Math.min(rowSum[i], colSum[j]);
|
||||
rowSum[i] -= arrs[i][j];
|
||||
colSum[j] -= arrs[i][j];
|
||||
}
|
||||
|
@ -65,14 +65,15 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
//1865:找出和为指定值的下标对
|
||||
public class FindingPairsWithACertainSum{
|
||||
public class FindingPairsWithACertainSum {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
// Solution solution = new FindingPairsWithACertainSum().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class FindSumPairs {
|
||||
class FindSumPairs {
|
||||
|
||||
int[] nums1;
|
||||
int[] nums2;
|
||||
@ -99,7 +100,7 @@ class FindSumPairs {
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your FindSumPairs object will be instantiated and called as such:
|
||||
|
@ -57,43 +57,44 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
//1817:查找用户活跃分钟数
|
||||
public class FindingTheUsersActiveMinutes{
|
||||
public class FindingTheUsersActiveMinutes {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new FindingTheUsersActiveMinutes().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int[] findingUsersActiveMinutes(int[][] logs, int k) {
|
||||
Map<Integer, List<Integer>> map = new HashMap<>();
|
||||
int min = Integer.MAX_VALUE;
|
||||
for (int i = 0; i < logs.length; i++) {
|
||||
List<Integer> temp;
|
||||
min = Math.min(min, logs[i][0]);
|
||||
if (map.containsKey(logs[i][0])) {
|
||||
temp = map.get(logs[i][0]);
|
||||
if (!temp.contains(logs[i][1])) {
|
||||
class Solution {
|
||||
public int[] findingUsersActiveMinutes(int[][] logs, int k) {
|
||||
Map<Integer, List<Integer>> map = new HashMap<>();
|
||||
int min = Integer.MAX_VALUE;
|
||||
for (int i = 0; i < logs.length; i++) {
|
||||
List<Integer> temp;
|
||||
min = Math.min(min, logs[i][0]);
|
||||
if (map.containsKey(logs[i][0])) {
|
||||
temp = map.get(logs[i][0]);
|
||||
if (!temp.contains(logs[i][1])) {
|
||||
temp.add(logs[i][1]);
|
||||
}
|
||||
map.put(logs[i][0], temp);
|
||||
} else {
|
||||
temp = new ArrayList<>();
|
||||
temp.add(logs[i][1]);
|
||||
map.put(logs[i][0], temp);
|
||||
}
|
||||
map.put(logs[i][0], temp);
|
||||
} else {
|
||||
temp = new ArrayList<>();
|
||||
temp.add(logs[i][1]);
|
||||
map.put(logs[i][0], temp);
|
||||
}
|
||||
int[] result = new int[k];
|
||||
for (int i = 0; i < k; i++) {
|
||||
result[i] = 0;
|
||||
}
|
||||
for (int key : map.keySet()) {
|
||||
int index = map.get(key).size();
|
||||
result[index - 1] = result[index - 1] + 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
int[] result = new int[k];
|
||||
for (int i = 0; i < k; i++) {
|
||||
result[i] = 0;
|
||||
}
|
||||
for (int key : map.keySet()) {
|
||||
int index = map.get(key).size();
|
||||
result[index-1] = result[index-1] + 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -45,7 +45,7 @@ public class FirstMissingPositive {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new FirstMissingPositive().new Solution();
|
||||
solution.firstMissingPositive(new int[]{1,2,0});
|
||||
solution.firstMissingPositive(new int[]{1, 2, 0});
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
|
@ -23,34 +23,35 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
//387:字符串中的第一个唯一字符
|
||||
class FirstUniqueCharacterInAString{
|
||||
class FirstUniqueCharacterInAString {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new FirstUniqueCharacterInAString().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int firstUniqChar(String s) {
|
||||
class Solution {
|
||||
public int firstUniqChar(String s) {
|
||||
// for (int i = 0; i < s.length(); i++) {
|
||||
// if(!s.substring(i+1).contains(""+s.charAt(i))&&!s.substring(0,i).contains(""+s.charAt(i))){
|
||||
// return i;
|
||||
// }
|
||||
// }
|
||||
// return -1;
|
||||
Map<Character, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i < s.length(); ++i) {
|
||||
char ch = s.charAt(i);
|
||||
map.put(ch, map.getOrDefault(ch, 0) + 1);
|
||||
}
|
||||
for (int i = 0; i < s.length(); ++i) {
|
||||
if (map.get(s.charAt(i)) == 1) {
|
||||
return i;
|
||||
Map<Character, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i < s.length(); ++i) {
|
||||
char ch = s.charAt(i);
|
||||
map.put(ch, map.getOrDefault(ch, 0) + 1);
|
||||
}
|
||||
for (int i = 0; i < s.length(); ++i) {
|
||||
if (map.get(s.charAt(i)) == 1) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -81,7 +81,7 @@ public class FlattenNestedListIterator {
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return index< list.size();
|
||||
return index < list.size();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -49,8 +49,8 @@ public class FlipGame {
|
||||
public List<String> generatePossibleNextMoves(String currentState) {
|
||||
List<String> list = new ArrayList<>();
|
||||
for (int i = 1; i < currentState.length(); i++) {
|
||||
if(currentState.charAt(i-1)=='+'&¤tState.charAt(i)=='+'){
|
||||
list.add(currentState.substring(0,i-1)+"--"+currentState.substring(i+1));
|
||||
if (currentState.charAt(i - 1) == '+' && currentState.charAt(i) == '+') {
|
||||
list.add(currentState.substring(0, i - 1) + "--" + currentState.substring(i + 1));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
|
@ -66,11 +66,11 @@ class FourSum {
|
||||
if (i > 0 && nums[i] == nums[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
sum = (long)nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3];
|
||||
sum = (long) nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3];
|
||||
if (sum > target) {
|
||||
break;
|
||||
}
|
||||
sum = (long)nums[i] + nums[length - 3] + nums[length - 2] + nums[length - 1];
|
||||
sum = (long) nums[i] + nums[length - 3] + nums[length - 2] + nums[length - 1];
|
||||
if (sum < target) {
|
||||
continue;
|
||||
}
|
||||
@ -78,18 +78,18 @@ class FourSum {
|
||||
if (j > i + 1 && nums[j] == nums[j - 1]) {
|
||||
continue;
|
||||
}
|
||||
sum = (long)nums[i] + nums[j] + nums[j + 1] + nums[j + 2];
|
||||
sum = (long) nums[i] + nums[j] + nums[j + 1] + nums[j + 2];
|
||||
if (sum > target) {
|
||||
break;
|
||||
}
|
||||
sum = (long)nums[i] + nums[j] + nums[length - 2] + nums[length - 1];
|
||||
sum = (long) nums[i] + nums[j] + nums[length - 2] + nums[length - 1];
|
||||
if (sum < target) {
|
||||
continue;
|
||||
}
|
||||
int start = j + 1;
|
||||
int end = length - 1;
|
||||
while (start < end) {
|
||||
sum = (long)nums[i] + nums[j] + nums[start] + nums[end];
|
||||
sum = (long) nums[i] + nums[j] + nums[start] + nums[end];
|
||||
if (sum == target) {
|
||||
result.add(Arrays.asList(nums[i], nums[j], nums[start], nums[end]));
|
||||
while (start < end && nums[start] == nums[start + 1]) {
|
||||
|
@ -28,37 +28,39 @@
|
||||
// 👍 25 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//LCP 28:采购方案
|
||||
public class FourXy4Wx{
|
||||
public class FourXy4Wx {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new FourXy4Wx().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int purchasePlans(int[] nums, int target) {
|
||||
int[] sort = new int[target];
|
||||
long[] count = new long[target];
|
||||
for (int num : nums) {
|
||||
if (num < target) {
|
||||
sort[num] += 1;
|
||||
class Solution {
|
||||
public int purchasePlans(int[] nums, int target) {
|
||||
int[] sort = new int[target];
|
||||
long[] count = new long[target];
|
||||
for (int num : nums) {
|
||||
if (num < target) {
|
||||
sort[num] += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
long sum = 0;
|
||||
for (int i = 1; i < target; i++) {
|
||||
sum += sort[i];
|
||||
count[i] = sum;
|
||||
}
|
||||
long result = 0;
|
||||
for (int num : nums) {
|
||||
if (target > num) {
|
||||
result += num <= target - num ? count[target - num] - 1 : count[target - num];
|
||||
long sum = 0;
|
||||
for (int i = 1; i < target; i++) {
|
||||
sum += sort[i];
|
||||
count[i] = sum;
|
||||
}
|
||||
long result = 0;
|
||||
for (int num : nums) {
|
||||
if (target > num) {
|
||||
result += num <= target - num ? count[target - num] - 1 : count[target - num];
|
||||
}
|
||||
}
|
||||
return (int) (result / 2 % (Math.pow(10, 9) + 7));
|
||||
}
|
||||
return (int) (result / 2 % (Math.pow(10, 9) + 7));
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -49,28 +49,29 @@ package leetcode.editor.cn;
|
||||
import java.util.Arrays;
|
||||
|
||||
//5739:最高频元素的频数
|
||||
public class FrequencyOfTheMostFrequentElement{
|
||||
public class FrequencyOfTheMostFrequentElement {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new FrequencyOfTheMostFrequentElement().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int maxFrequency(int[] nums, int k) {
|
||||
Arrays.sort(nums);
|
||||
int ans = 1;
|
||||
for(int i = 1, j = 0, sum = 0; i < nums.length; i += 1){
|
||||
sum += (nums[i] - nums[i - 1]) * (i - j);
|
||||
while(sum > k){
|
||||
sum -= nums[i] - nums[j];
|
||||
j += 1;
|
||||
class Solution {
|
||||
public int maxFrequency(int[] nums, int k) {
|
||||
Arrays.sort(nums);
|
||||
int ans = 1;
|
||||
for (int i = 1, j = 0, sum = 0; i < nums.length; i += 1) {
|
||||
sum += (nums[i] - nums[i - 1]) * (i - j);
|
||||
while (sum > k) {
|
||||
sum -= nums[i] - nums[j];
|
||||
j += 1;
|
||||
}
|
||||
ans = Math.max(ans, i - j + 1);
|
||||
}
|
||||
ans = Math.max(ans, i - j + 1);
|
||||
return ans;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -44,7 +44,7 @@ class GenerateParentheses {
|
||||
class Solution {
|
||||
public List<String> generateParenthesis(int n) {
|
||||
List<String> result = new ArrayList<>();
|
||||
dfs(n,0,"",result);
|
||||
dfs(n, 0, "", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -60,45 +60,46 @@ import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
//1878:矩阵中最大的三个菱形和
|
||||
public class GetBiggestThreeRhombusSumsInAGrid{
|
||||
public class GetBiggestThreeRhombusSumsInAGrid {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new GetBiggestThreeRhombusSumsInAGrid().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int[] getBiggestThree(int[][] grid) {
|
||||
Set<Integer> set = new HashSet<>();
|
||||
new TreeSet<>(Comparator.reverseOrder());
|
||||
int xLength = grid.length;
|
||||
int yLength = grid[0].length;
|
||||
int size = (Math.min(xLength, yLength) + 1) / 2;
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (int j = i; j < xLength - i; j++) {
|
||||
for (int k = i; k < yLength - i; k++) {
|
||||
int sum = 0;
|
||||
for (int x = j - i; x <= j + i; x++) {
|
||||
int cha = i - Math.abs(j - x);
|
||||
if (Math.abs(j - x) == i) {
|
||||
sum += grid[x][k + cha];
|
||||
} else {
|
||||
sum += grid[x][k - cha] + grid[x][k + cha];
|
||||
class Solution {
|
||||
public int[] getBiggestThree(int[][] grid) {
|
||||
Set<Integer> set = new HashSet<>();
|
||||
new TreeSet<>(Comparator.reverseOrder());
|
||||
int xLength = grid.length;
|
||||
int yLength = grid[0].length;
|
||||
int size = (Math.min(xLength, yLength) + 1) / 2;
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (int j = i; j < xLength - i; j++) {
|
||||
for (int k = i; k < yLength - i; k++) {
|
||||
int sum = 0;
|
||||
for (int x = j - i; x <= j + i; x++) {
|
||||
int cha = i - Math.abs(j - x);
|
||||
if (Math.abs(j - x) == i) {
|
||||
sum += grid[x][k + cha];
|
||||
} else {
|
||||
sum += grid[x][k - cha] + grid[x][k + cha];
|
||||
}
|
||||
}
|
||||
set.add(sum);
|
||||
}
|
||||
set.add(sum);
|
||||
}
|
||||
}
|
||||
List<Integer> list = set.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
|
||||
int length = Math.min(list.size(), 3);
|
||||
int[] result = new int[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
result[i] = list.get(i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
List<Integer> list = set.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
|
||||
int length = Math.min(list.size(), 3);
|
||||
int[] result = new int[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
result[i] = list.get(i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -44,7 +44,7 @@ public class GreatestCommonDivisorOfStrings {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new GreatestCommonDivisorOfStrings().new Solution();
|
||||
solution.gcdOfStrings("TAUXXTAUXXTAUXXTAUXXTAUXX","TAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXX");
|
||||
solution.gcdOfStrings("TAUXXTAUXXTAUXXTAUXXTAUXX", "TAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXX");
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
|
@ -39,33 +39,33 @@ package leetcode.editor.cn;
|
||||
import java.util.*;
|
||||
|
||||
//49:字母异位词分组
|
||||
class GroupAnagrams{
|
||||
class GroupAnagrams {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new GroupAnagrams().new Solution();
|
||||
solution.groupAnagrams(new String[]{"eat","tea","tan","ate","nat","bat"});
|
||||
solution.groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"});
|
||||
}
|
||||
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public List<List<String>> groupAnagrams(String[] strs) {
|
||||
Map<String,List<String>> map = new HashMap<>();
|
||||
for(String s:strs){
|
||||
char[] chs = s.toCharArray();
|
||||
Arrays.sort(chs);
|
||||
String str = Arrays.toString(chs);
|
||||
List<String> list = map.getOrDefault(str,new ArrayList<>());
|
||||
list.add(s);
|
||||
map.put(str,new ArrayList<>(list));
|
||||
class Solution {
|
||||
public List<List<String>> groupAnagrams(String[] strs) {
|
||||
Map<String, List<String>> map = new HashMap<>();
|
||||
for (String s : strs) {
|
||||
char[] chs = s.toCharArray();
|
||||
Arrays.sort(chs);
|
||||
String str = Arrays.toString(chs);
|
||||
List<String> list = map.getOrDefault(str, new ArrayList<>());
|
||||
list.add(s);
|
||||
map.put(str, new ArrayList<>(list));
|
||||
}
|
||||
List<List<String>> result = new ArrayList<>();
|
||||
for (String key : map.keySet()) {
|
||||
result.add(new ArrayList<>(map.get(key)));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
List<List<String>> result = new ArrayList<>();
|
||||
for (String key:map.keySet()){
|
||||
result.add(new ArrayList<>(map.get(key)));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -26,30 +26,31 @@ package leetcode.editor.cn;
|
||||
import java.util.*;
|
||||
|
||||
//面试题 10.02:变位词组
|
||||
class GroupAnagramsLcci{
|
||||
class GroupAnagramsLcci {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new GroupAnagramsLcci().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public List<List<String>> groupAnagrams(String[] strs) {
|
||||
Map<String,List<String>> map = new HashMap<>();
|
||||
for (String str:strs){
|
||||
char[] chars = str.toCharArray();
|
||||
Arrays.sort(chars);
|
||||
List<String> list = map.getOrDefault(new String(chars),new ArrayList<>());
|
||||
list.add(str);
|
||||
map.put(new String(chars),list);
|
||||
class Solution {
|
||||
public List<List<String>> groupAnagrams(String[] strs) {
|
||||
Map<String, List<String>> map = new HashMap<>();
|
||||
for (String str : strs) {
|
||||
char[] chars = str.toCharArray();
|
||||
Arrays.sort(chars);
|
||||
List<String> list = map.getOrDefault(new String(chars), new ArrayList<>());
|
||||
list.add(str);
|
||||
map.put(new String(chars), list);
|
||||
}
|
||||
List<List<String>> result = new ArrayList<>();
|
||||
for (String key : map.keySet()) {
|
||||
result.add(map.get(key));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
List<List<String>> result = new ArrayList<>();
|
||||
for (String key: map.keySet()){
|
||||
result.add(map.get(key));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -29,7 +29,7 @@ public class HIndex {
|
||||
//测试代码
|
||||
Solution solution = new HIndex().new Solution();
|
||||
//3
|
||||
System.out.println(solution.hIndex(new int[]{3,0,6,1,5}));
|
||||
System.out.println(solution.hIndex(new int[]{3, 0, 6, 1, 5}));
|
||||
//0
|
||||
System.out.println(solution.hIndex(new int[]{0}));
|
||||
}
|
||||
|
@ -64,12 +64,13 @@ public class HouseRobberIi {
|
||||
}
|
||||
return Math.max(range(Arrays.copyOfRange(nums, 0, length - 1)), range(Arrays.copyOfRange(nums, 1, length)));
|
||||
}
|
||||
|
||||
public int range(int[] nums) {
|
||||
int length = nums.length;
|
||||
int start = nums[0],end = Math.max(nums[0],nums[1]);
|
||||
int start = nums[0], end = Math.max(nums[0], nums[1]);
|
||||
for (int i = 2; i < length; i++) {
|
||||
int temp = end;
|
||||
end = Math.max(start+nums[i],end);
|
||||
end = Math.max(start + nums[i], end);
|
||||
start = temp;
|
||||
}
|
||||
return end;
|
||||
|
@ -47,27 +47,27 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
//1365:有多少小于当前数字的数字
|
||||
public class HowManyNumbersAreSmallerThanTheCurrentNumber{
|
||||
public class HowManyNumbersAreSmallerThanTheCurrentNumber {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new HowManyNumbersAreSmallerThanTheCurrentNumber().new Solution();
|
||||
|
||||
|
||||
}
|
||||
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int[] smallerNumbersThanCurrent(int[] nums) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
list.add(nums[i]);
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int[] smallerNumbersThanCurrent(int[] nums) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
list.add(nums[i]);
|
||||
}
|
||||
Collections.sort(list);
|
||||
int[] arrs = new int[nums.length];
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
arrs[i] = list.indexOf(nums[i]);
|
||||
}
|
||||
return arrs;
|
||||
}
|
||||
Collections.sort(list);
|
||||
int[] arrs = new int[nums.length];
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
arrs[i] = list.indexOf(nums[i]);
|
||||
}
|
||||
return arrs;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -67,24 +67,25 @@ package leetcode.editor.cn;
|
||||
import java.util.Stack;
|
||||
|
||||
//1410:HTML 实体解析器
|
||||
public class HtmlEntityParser{
|
||||
public class HtmlEntityParser {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new HtmlEntityParser().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public String entityParser(String text) {
|
||||
return text.
|
||||
replace(""", "\"").
|
||||
replace("'", "'").
|
||||
replace(">", ">").
|
||||
replace("<", "<").
|
||||
replace("⁄", "/").
|
||||
replace("&", "&");
|
||||
class Solution {
|
||||
public String entityParser(String text) {
|
||||
return text.
|
||||
replace(""", "\"").
|
||||
replace("'", "'").
|
||||
replace(">", ">").
|
||||
replace("<", "<").
|
||||
replace("⁄", "/").
|
||||
replace("&", "&");
|
||||
}
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -91,11 +91,11 @@ public class ImplementQueueUsingStacks {
|
||||
* Push element x to the back of queue.
|
||||
*/
|
||||
public void push(int x) {
|
||||
while (!stack1.empty()){
|
||||
while (!stack1.empty()) {
|
||||
stack2.push(stack1.pop());
|
||||
}
|
||||
stack1.push(x);
|
||||
while (!stack2.isEmpty()){
|
||||
while (!stack2.isEmpty()) {
|
||||
stack1.push(stack2.pop());
|
||||
}
|
||||
}
|
||||
|
@ -114,7 +114,7 @@ public class ImplementTriePrefixTree {
|
||||
if (trie.son[index] == null) {
|
||||
return false;
|
||||
}
|
||||
if (i == size-1 && !trie.son[index].isEnd) {
|
||||
if (i == size - 1 && !trie.son[index].isEnd) {
|
||||
return false;
|
||||
}
|
||||
trie = trie.son[index];
|
||||
|
@ -39,7 +39,7 @@ class IncreasingSubsequences {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new IncreasingSubsequences().new Solution();
|
||||
System.out.println(solution.findSubsequences(new int[]{4,6,7,7}));
|
||||
System.out.println(solution.findSubsequences(new int[]{4, 6, 7, 7}));
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
@ -47,12 +47,13 @@ class IncreasingSubsequences {
|
||||
class Solution {
|
||||
private List<Integer> path = new ArrayList<>();
|
||||
private List<List<Integer>> res = new ArrayList<>();
|
||||
|
||||
public List<List<Integer>> findSubsequences(int[] nums) {
|
||||
backtracking(nums,0);
|
||||
backtracking(nums, 0);
|
||||
return res;
|
||||
}
|
||||
|
||||
private void backtracking (int[] nums, int start) {
|
||||
private void backtracking(int[] nums, int start) {
|
||||
if (path.size() > 1) {
|
||||
res.add(new ArrayList<>(path));
|
||||
}
|
||||
|
@ -42,36 +42,38 @@
|
||||
// 👍 2 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//1860:增长的内存泄露
|
||||
public class IncrementalMemoryLeak{
|
||||
public class IncrementalMemoryLeak {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new IncrementalMemoryLeak().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int[] memLeak(int memory1, int memory2) {
|
||||
boolean isOne = memory1 >= memory2;
|
||||
int i = 1;
|
||||
while (true) {
|
||||
if (isOne) {
|
||||
if (memory1 < i) {
|
||||
break;
|
||||
class Solution {
|
||||
public int[] memLeak(int memory1, int memory2) {
|
||||
boolean isOne = memory1 >= memory2;
|
||||
int i = 1;
|
||||
while (true) {
|
||||
if (isOne) {
|
||||
if (memory1 < i) {
|
||||
break;
|
||||
}
|
||||
memory1 -= i;
|
||||
} else {
|
||||
if (memory2 < i) {
|
||||
break;
|
||||
}
|
||||
memory2 -= i;
|
||||
}
|
||||
memory1 -= i;
|
||||
} else {
|
||||
if (memory2 < i) {
|
||||
break;
|
||||
}
|
||||
memory2 -= i;
|
||||
i++;
|
||||
isOne = memory1 >= memory2;
|
||||
}
|
||||
i++;
|
||||
isOne = memory1 >= memory2;
|
||||
return new int[]{i, memory1, memory2};
|
||||
}
|
||||
return new int[]{i,memory1,memory2};
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -35,54 +35,55 @@ package leetcode.editor.cn;
|
||||
import com.code.leet.entiy.ListNode;
|
||||
|
||||
//147:对链表进行插入排序
|
||||
public class InsertionSortList{
|
||||
public class InsertionSortList {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new InsertionSortList().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode insertionSortList(ListNode head) {
|
||||
if (head == null || head.next == null) {
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode insertionSortList(ListNode head) {
|
||||
if (head == null || head.next == null) {
|
||||
return head;
|
||||
}
|
||||
ListNode current = head.next;
|
||||
head.next = null;
|
||||
while (current != null) {
|
||||
ListNode temp = head;
|
||||
ListNode next = current.next;
|
||||
current.next = null;
|
||||
if (current.val < temp.val) {
|
||||
head = current;
|
||||
head.next = temp;
|
||||
} else {
|
||||
while (temp.next != null) {
|
||||
if (current.val < temp.next.val) {
|
||||
current.next = temp.next;
|
||||
temp.next = current;
|
||||
break;
|
||||
} else {
|
||||
temp = temp.next;
|
||||
}
|
||||
}
|
||||
temp.next = temp.next == null ? current : temp.next;
|
||||
}
|
||||
current = next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
ListNode current = head.next;
|
||||
head.next = null;
|
||||
while (current != null) {
|
||||
ListNode temp = head;
|
||||
ListNode next = current.next;
|
||||
current.next = null;
|
||||
if (current.val < temp.val) {
|
||||
head = current;
|
||||
head.next = temp;
|
||||
} else {
|
||||
while (temp.next != null) {
|
||||
if (current.val < temp.next.val) {
|
||||
current.next = temp.next;
|
||||
temp.next = current;
|
||||
break;
|
||||
} else {
|
||||
temp = temp.next;
|
||||
}
|
||||
}
|
||||
temp.next = temp.next == null ? current : temp.next;
|
||||
}
|
||||
current = next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -70,30 +70,32 @@
|
||||
// 👍 593 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//12:整数转罗马数字
|
||||
public class IntegerToRoman{
|
||||
public class IntegerToRoman {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new IntegerToRoman().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public String intToRoman(int num) {
|
||||
int[] values = new int[]{1000,900,500,400,100,90,50,40,10,9,5,4,1};
|
||||
String[] labels = new String[]{"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < values.length && num > 0; i++) {
|
||||
int value = values[i];
|
||||
String label = labels[i];
|
||||
while (num >= value) {
|
||||
sb.append(label);
|
||||
num -= value;
|
||||
class Solution {
|
||||
public String intToRoman(int num) {
|
||||
int[] values = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
|
||||
String[] labels = new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < values.length && num > 0; i++) {
|
||||
int value = values[i];
|
||||
String label = labels[i];
|
||||
while (num >= value) {
|
||||
sb.append(label);
|
||||
num -= value;
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -39,34 +39,35 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
//350:两个数组的交集 II
|
||||
class IntersectionOfTwoArraysIi{
|
||||
class IntersectionOfTwoArraysIi {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new IntersectionOfTwoArraysIi().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int[] intersect(int[] nums1, int[] nums2) {
|
||||
Arrays.sort(nums1);
|
||||
Arrays.sort(nums2);
|
||||
List<Integer> list = new ArrayList<>();
|
||||
int index1 = 0;
|
||||
int index2 = 0;
|
||||
while(index1<nums1.length&&index2<nums2.length){
|
||||
if(nums1[index1]==nums2[index2]){
|
||||
list.add(nums1[index1]);
|
||||
index1++;
|
||||
index2++;
|
||||
}else if (nums1[index1]>nums2[index2]){
|
||||
index2++;
|
||||
}else{
|
||||
index1++;
|
||||
class Solution {
|
||||
public int[] intersect(int[] nums1, int[] nums2) {
|
||||
Arrays.sort(nums1);
|
||||
Arrays.sort(nums2);
|
||||
List<Integer> list = new ArrayList<>();
|
||||
int index1 = 0;
|
||||
int index2 = 0;
|
||||
while (index1 < nums1.length && index2 < nums2.length) {
|
||||
if (nums1[index1] == nums2[index2]) {
|
||||
list.add(nums1[index1]);
|
||||
index1++;
|
||||
index2++;
|
||||
} else if (nums1[index1] > nums2[index2]) {
|
||||
index2++;
|
||||
} else {
|
||||
index1++;
|
||||
}
|
||||
}
|
||||
return list.stream().mapToInt(Integer::intValue).toArray();
|
||||
}
|
||||
return list.stream().mapToInt(Integer::intValue).toArray();
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -74,46 +74,47 @@ package leetcode.editor.cn;
|
||||
import com.code.leet.entiy.ListNode;
|
||||
|
||||
//160:相交链表
|
||||
public class IntersectionOfTwoLinkedLists{
|
||||
public class IntersectionOfTwoLinkedLists {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new IntersectionOfTwoLinkedLists().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) {
|
||||
* val = x;
|
||||
* next = null;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
public class Solution {
|
||||
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
|
||||
ListNode pA = headA, pB = headB;
|
||||
if(pA == null || pB == null){
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) {
|
||||
* val = x;
|
||||
* next = null;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
public class Solution {
|
||||
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
|
||||
ListNode pA = headA, pB = headB;
|
||||
if (pA == null || pB == null) {
|
||||
return null;
|
||||
}
|
||||
while (pA != null || pB != null) {
|
||||
if (pA == null) {
|
||||
pA = headB;
|
||||
}
|
||||
if (pB == null) {
|
||||
pB = headA;
|
||||
}
|
||||
if (pA == pB) {
|
||||
return pA;
|
||||
}
|
||||
pA = pA.next;
|
||||
pB = pB.next;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
while (pA != null || pB != null) {
|
||||
if (pA == null) {
|
||||
pA = headB;
|
||||
}
|
||||
if (pB == null) {
|
||||
pB = headA;
|
||||
}
|
||||
if (pA == pB) {
|
||||
return pA;
|
||||
}
|
||||
pA = pA.next;
|
||||
pB = pB.next;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -17,46 +17,47 @@ package leetcode.editor.cn;
|
||||
import com.code.leet.entiy.ListNode;
|
||||
|
||||
//面试题 02.07:链表相交
|
||||
public class IntersectionOfTwoLinkedListsLcci{
|
||||
public class IntersectionOfTwoLinkedListsLcci {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new IntersectionOfTwoLinkedListsLcci().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) {
|
||||
* val = x;
|
||||
* next = null;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
public class Solution {
|
||||
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
|
||||
ListNode pA = headA, pB = headB;
|
||||
if(pA == null || pB == null){
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode(int x) {
|
||||
* val = x;
|
||||
* next = null;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
public class Solution {
|
||||
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
|
||||
ListNode pA = headA, pB = headB;
|
||||
if (pA == null || pB == null) {
|
||||
return null;
|
||||
}
|
||||
while (pA != null || pB != null) {
|
||||
if (pA == null) {
|
||||
pA = headB;
|
||||
}
|
||||
if (pB == null) {
|
||||
pB = headA;
|
||||
}
|
||||
if (pA == pB) {
|
||||
return pA;
|
||||
}
|
||||
pA = pA.next;
|
||||
pB = pB.next;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
while (pA != null || pB != null) {
|
||||
if (pA == null) {
|
||||
pA = headB;
|
||||
}
|
||||
if (pB == null) {
|
||||
pB = headA;
|
||||
}
|
||||
if (pA == pB) {
|
||||
return pA;
|
||||
}
|
||||
pA = pA.next;
|
||||
pB = pB.next;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -30,39 +30,40 @@ package leetcode.editor.cn;
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
//226:翻转二叉树
|
||||
public class InvertBinaryTree{
|
||||
public class InvertBinaryTree {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new InvertBinaryTree().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* public class TreeNode {
|
||||
* int val;
|
||||
* TreeNode left;
|
||||
* TreeNode right;
|
||||
* TreeNode() {}
|
||||
* TreeNode(int val) { this.val = val; }
|
||||
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
* this.val = val;
|
||||
* this.left = left;
|
||||
* this.right = right;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public TreeNode invertTree(TreeNode root) {
|
||||
if (root == null) {
|
||||
return null;
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* public class TreeNode {
|
||||
* int val;
|
||||
* TreeNode left;
|
||||
* TreeNode right;
|
||||
* TreeNode() {}
|
||||
* TreeNode(int val) { this.val = val; }
|
||||
* TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
* this.val = val;
|
||||
* this.left = left;
|
||||
* this.right = right;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public TreeNode invertTree(TreeNode root) {
|
||||
if (root == null) {
|
||||
return null;
|
||||
}
|
||||
TreeNode left = invertTree(root.left);
|
||||
root.left = invertTree(root.right);
|
||||
root.right = left;
|
||||
return root;
|
||||
}
|
||||
TreeNode left = invertTree(root.left);
|
||||
root.left = invertTree(root.right);
|
||||
root.right = left;
|
||||
return root;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -60,7 +60,7 @@ public class IsGraphBipartite {
|
||||
Solution solution = new IsGraphBipartite().new Solution();
|
||||
// TwoArray twoArray = new TwoArray("[[1,3],[0,2],[1,3],[0,2]]");
|
||||
System.out.println("-------------------------------");
|
||||
TwoArray twoArray = new TwoArray("[[],[2,4,6],[1,4,8,9],[7,8],[1,2,8,9],[6,9],[1,5,7,8,9],[3,6,9],[2,3,4,6,9],[2,4,5,6,7,8]]",false);
|
||||
TwoArray twoArray = new TwoArray("[[],[2,4,6],[1,4,8,9],[7,8],[1,2,8,9],[6,9],[1,5,7,8,9],[3,6,9],[2,3,4,6,9],[2,4,5,6,7,8]]", false);
|
||||
System.out.println(solution.isBipartite(twoArray.getArr()));
|
||||
}
|
||||
|
||||
|
@ -42,30 +42,31 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
//205:同构字符串
|
||||
public class IsomorphicStrings{
|
||||
public class IsomorphicStrings {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new IsomorphicStrings().new Solution();
|
||||
solution.isIsomorphic("badc","baba");
|
||||
solution.isIsomorphic("badc", "baba");
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public boolean isIsomorphic(String s, String t) {
|
||||
Map<Character,Character> sMap = new HashMap<>();
|
||||
Map<Character,Character> tMap = new HashMap<>();
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char sch = s.charAt(i);
|
||||
char tch = t.charAt(i);
|
||||
if((sMap.containsKey(sch)&&sMap.get(sch)!=tch)||(tMap.containsKey(tch)&&tMap.get(tch)!=sch)){
|
||||
return false;
|
||||
class Solution {
|
||||
public boolean isIsomorphic(String s, String t) {
|
||||
Map<Character, Character> sMap = new HashMap<>();
|
||||
Map<Character, Character> tMap = new HashMap<>();
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char sch = s.charAt(i);
|
||||
char tch = t.charAt(i);
|
||||
if ((sMap.containsKey(sch) && sMap.get(sch) != tch) || (tMap.containsKey(tch) && tMap.get(tch) != sch)) {
|
||||
return false;
|
||||
}
|
||||
sMap.put(sch, tch);
|
||||
tMap.put(tch, sch);
|
||||
}
|
||||
sMap.put(sch,tch);
|
||||
tMap.put(tch,sch);
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user