力扣idea插件更新

This commit is contained in:
huangge1199 2021-08-23 14:05:33 +08:00
parent 9da5eac11c
commit 1817762271
475 changed files with 4285 additions and 1 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@
/target/ /target/
/src/main/java/leetcode/editor/cn/*.txt /src/main/java/leetcode/editor/cn/*.txt
/src/main/java/leetcode/editor/cn/doc/solution/ /src/main/java/leetcode/editor/cn/doc/solution/
/src/main/java/leetcode/editor/cn/doc/submission/

View File

@ -25,6 +25,11 @@ public class Node {
this.random = random; this.random = random;
} }
public Node(int val, List<Node> children) {
this.val = val;
this.children = children;
}
public Node(int val, Node prev, Node next, Node child) { public Node(int val, Node prev, Node next, Node child) {
this.val = val; this.val = val;
this.prev = prev; this.prev = prev;

View File

@ -0,0 +1,122 @@
//给你一个有 n 个节点的 有向无环图DAG请你找出所有从节点 0 到节点 n-1 的路径并输出不要求按特定顺序
//
// 二维数组的第 i 个数组中的单元都表示有向图中 i 号节点所能到达的下一些节点空就是没有下一个结点了
//
// 译者注有向图是有方向的即规定了 ab 你就不能从 ba
//
//
//
// 示例 1
//
//
//
//
//输入graph = [[1,2],[3],[3],[]]
//输出[[0,1,3],[0,2,3]]
//解释有两条路径 0 -> 1 -> 3 0 -> 2 -> 3
//
//
// 示例 2
//
//
//
//
//输入graph = [[4,3,1],[3,2,4],[3],[4],[]]
//输出[[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]
//
//
// 示例 3
//
//
//输入graph = [[1],[]]
//输出[[0,1]]
//
//
// 示例 4
//
//
//输入graph = [[1,2,3],[2],[3],[]]
//输出[[0,1,2,3],[0,2,3],[0,3]]
//
//
// 示例 5
//
//
//输入graph = [[1,3],[2],[3],[]]
//输出[[0,1,2,3],[0,3]]
//
//
//
//
// 提示
//
//
// n == graph.length
// 2 <= n <= 15
// 0 <= graph[i][j] < n
// graph[i][j] != i不存在自环
// graph[i] 中的所有元素 互不相同
// 保证输入为 有向无环图DAG
//
// Related Topics 深度优先搜索 广度优先搜索 回溯 👍 143 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.List;
//797:所有可能的路径
class AllPathsFromSourceToTarget {
public static void main(String[] args) {
//测试代码
Solution solution = new AllPathsFromSourceToTarget().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
// List<List<Integer>> result = new ArrayList<>();
//
// public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
// path(graph, 0, new ArrayList<>());
// return result;
// }
//
// public void path(int[][] graph, int index, List<Integer> list) {
// list.add(index);
// if (index == graph.length) {
// result.add(new ArrayList<>(list));
// list.remove(list.size() - 1);
// return;
// }
// for (int i = 0; i < graph[index].length; i++) {
// path(graph, graph[index][i], list);
// }
// list.remove(list.size() - 1);
// }
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
return solve(graph, 0);
}
public List<List<Integer>> solve(int[][] graph, int node) {
int N = graph.length;
List<List<Integer>> ans = new ArrayList();
if (node == N - 1) {
List<Integer> path = new ArrayList();
path.add(N - 1);
ans.add(path);
return ans;
}
for (int nei : graph[node]) {
for (List<Integer> path : solve(graph, nei)) {
path.add(0, node);
ans.add(path);
}
}
return ans;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,62 @@
//给你一个 0 开始的排列 nums下标也从 0 开始请你构建一个 同样长度 的数组 ans 其中对于每个 i0 <= i < nums.
//length都满足 ans[i] = nums[nums[i]] 返回构建好的数组 ans
//
// 0 开始的排列 nums 是一个由 0 nums.length - 10 nums.length - 1 也包含在内的不同整数组成的数组
//
//
//
//
// 示例 1
//
// 输入nums = [0,2,1,5,3,4]
//输出[0,1,2,4,5,3]
//解释数组 ans 构建如下
//ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4
//]], nums[nums[5]]]
// = [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]
// = [0,1,2,4,5,3]
//
// 示例 2
//
// 输入nums = [5,0,1,2,3,4]
//输出[4,5,0,1,2,3]
//解释数组 ans 构建如下
//ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4
//]], nums[nums[5]]]
// = [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]]
// = [4,5,0,1,2,3]
//
//
//
// 提示
//
//
// 1 <= nums.length <= 1000
// 0 <= nums[i] < nums.length
// nums 中的元素 互不相同
//
// Related Topics 数组 模拟 👍 2 👎 0
package leetcode.editor.cn;
//1920:基于排列构建数组
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]];
}
return arr;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,59 @@
//给你一个字符串 s 如果 s 是一个 字符串请你返回 true 否则请返回 false
//
// 如果 s 中出现过的 所有 字符的出现次数 相同 那么我们称字符串 s 字符串
//
//
//
// 示例 1
//
// 输入s = "abacbc"
//输出true
//解释s 中出现过的字符为 'a''b' 'c' s 中所有字符均出现 2
//
//
// 示例 2
//
// 输入s = "aaabb"
//输出false
//解释s 中出现过的字符为 'a' 'b'
//'a' 出现了 3 'b' 出现了 2 两者出现次数不同
//
//
//
//
// 提示
//
//
// 1 <= s.length <= 1000
// s 只包含小写英文字母
//
// Related Topics 哈希表 字符串 计数 👍 0 👎 0
package leetcode.editor.cn;
//1941:检查是否所有字符出现次数相同
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;
}
}
return true;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,131 @@
//给你无向 连通 图中一个节点的引用请你返回该图的 深拷贝克隆
//
// 图中的每个节点都包含它的值 valint 和其邻居的列表list[Node]
//
// class Node {
// public int val;
// public List<Node> neighbors;
//}
//
//
//
// 测试用例格式
//
// 简单起见每个节点的值都和它的索引相同例如第一个节点值为 1val = 1第二个节点值为 2val = 2以此类推该图在测试用例中使用邻
//接列表表示
//
// 邻接列表 是用于表示有限图的无序列表的集合每个列表都描述了图中节点的邻居集
//
// 给定节点将始终是图中的第一个节点值为 1你必须将 给定节点的拷贝 作为对克隆图的引用返回
//
//
//
// 示例 1
//
//
//
// 输入adjList = [[2,4],[1,3],[2,4],[1,3]]
//输出[[2,4],[1,3],[2,4],[1,3]]
//解释
//图中有 4 个节点
//节点 1 的值是 1它有两个邻居节点 2 4
//节点 2 的值是 2它有两个邻居节点 1 3
//节点 3 的值是 3它有两个邻居节点 2 4
//节点 4 的值是 4它有两个邻居节点 1 3
//
//
// 示例 2
//
//
//
// 输入adjList = [[]]
//输出[[]]
//解释输入包含一个空列表该图仅仅只有一个值为 1 的节点它没有任何邻居
//
//
// 示例 3
//
// 输入adjList = []
//输出[]
//解释这个图是空的它不含任何节点
//
//
// 示例 4
//
//
//
// 输入adjList = [[2],[1]]
//输出[[2],[1]]
//
//
//
// 提示
//
//
// 节点数不超过 100
// 每个节点值 Node.val 都是唯一的1 <= Node.val <= 100
// 无向图是一个简单图这意味着图中没有重复的边也没有自环
// 由于图是无向的如果节点 p 是节点 q 的邻居那么节点 q 也必须是节点 p 的邻居
// 图是连通图你可以从给定节点访问到所有节点
//
// Related Topics 深度优先搜索 广度优先搜索 哈希表 👍 393 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.Node;
import java.util.ArrayList;
import java.util.HashMap;
//133:克隆图
class CloneGraph{
public static void main(String[] args) {
//测试代码
Solution solution = new CloneGraph().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
}
*/
class Solution {
private HashMap<Node, Node> use = new HashMap<>();
public Node cloneGraph(Node node) {
if (node == null) {
return node;
}
if (use.containsKey(node)) {
return use.get(node);
}
Node cloneNode = new Node(node.val, new ArrayList());
use.put(node, cloneNode);
for (Node neighbor: node.children) {
cloneNode.children.add(cloneGraph(neighbor));
}
return cloneNode;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,58 @@
//输入一个链表的头节点从尾到头反过来返回每个节点的值用数组返回
//
//
//
// 示例 1
//
// 输入head = [1,3,2]
//输出[2,3,1]
//
//
//
// 限制
//
// 0 <= 链表长度 <= 10000
// Related Topics 递归 链表 双指针 👍 173 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.ListNode;
import java.util.ArrayList;
import java.util.List;
//剑指 Offer 06:从尾到头打印链表
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;
}
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)
}

View File

@ -0,0 +1,58 @@
//一个 平方和三元组 (a,b,c) 指的是满足 a² + b² = c² 整数 三元组 ab c
//
// 给你一个整数 n 请你返回满足 1 <= a, b, c <= n 平方和三元组 的数目
//
//
//
// 示例 1
//
// 输入n = 5
//输出2
//解释平方和三元组为 (3,4,5) (4,3,5)
//
//
// 示例 2
//
// 输入n = 10
//输出4
//解释平方和三元组为 (3,4,5)(4,3,5)(6,8,10) (8,6,10)
//
//
//
//
// 提示
//
//
// 1 <= n <= 250
//
// Related Topics 数学 枚举 👍 5 👎 0
package leetcode.editor.cn;
//1925:统计平方和三元组的数目
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++;
}
}
}
return count;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,106 @@
//不使用任何内建的哈希表库设计一个哈希映射HashMap
//
// 实现 MyHashMap
//
//
// MyHashMap() 用空映射初始化对象
// void put(int key, int value) HashMap 插入一个键值对 (key, value) 如果 key 已经存在于映射中
//则更新其对应的值 value
// int get(int key) 返回特定的 key 所映射的 value 如果映射中不包含 key 的映射返回 -1
// void remove(key) 如果映射中存在 key 的映射则移除 key 和它所对应的 value
//
//
//
//
// 示例
//
//
//输入
//["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
//[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
//输出
//[null, null, null, 1, -1, null, 1, null, -1]
//
//解释
//MyHashMap myHashMap = new MyHashMap();
//myHashMap.put(1, 1); // myHashMap 现在为 [[1,1]]
//myHashMap.put(2, 2); // myHashMap 现在为 [[1,1], [2,2]]
//myHashMap.get(1); // 返回 1 myHashMap 现在为 [[1,1], [2,2]]
//myHashMap.get(3); // 返回 -1未找到myHashMap 现在为 [[1,1], [2,2]]
//myHashMap.put(2, 1); // myHashMap 现在为 [[1,1], [2,1]]更新已有的值
//myHashMap.get(2); // 返回 1 myHashMap 现在为 [[1,1], [2,1]]
//myHashMap.remove(2); // 删除键为 2 的数据myHashMap 现在为 [[1,1]]
//myHashMap.get(2); // 返回 -1未找到myHashMap 现在为 [[1,1]]
//
//
//
//
// 提示
//
//
// 0 <= key, value <= 10
// 最多调用 10 putget remove 方法
//
//
//
//
// 进阶你能否不使用内置的 HashMap 库解决此问题
// Related Topics 设计 数组 哈希表 链表 哈希函数 👍 218 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
//706:设计哈希映射
class DesignHashmap {
public static void main(String[] args) {
//测试代码
// Solution solution = new DesignHashmap().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class MyHashMap {
int[] arr;
/**
* Initialize your data structure here.
*/
public MyHashMap() {
arr = new int[1000001];
Arrays.fill(arr, -1);
}
/**
* value will always be non-negative.
*/
public void put(int key, int value) {
arr[key] = value;
}
/**
* Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
*/
public int get(int key) {
return arr[key];
}
/**
* Removes the mapping of the specified value key if this map contains a mapping for the key
*/
public void remove(int key) {
arr[key] = -1;
}
}
/**
* Your MyHashMap object will be instantiated and called as such:
* MyHashMap obj = new MyHashMap();
* obj.put(key,value);
* int param_2 = obj.get(key);
* obj.remove(key);
*/
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,104 @@
//你正在玩一款电子游戏在游戏中你需要保护城市免受怪物侵袭给你一个 下标从 0 开始 且长度为 n 的整数数组 dist 其中 dist[i] 是第 i
//个怪物与城市的 初始距离单位
//
// 怪物以 恒定 的速度走向城市给你一个长度为 n 的整数数组 speed 表示每个怪物的速度其中 speed[i] 是第 i 个怪物的速度单位/
//
//
// 怪物从 0 分钟 时开始移动你有一把武器并可以 选择 在每一分钟的开始时使用包括第 0 分钟但是你无法在一分钟的中间使用武器这种武器威力惊人
//一次可以消灭任一还活着的怪物
//
// 一旦任一怪物到达城市你就输掉了这场游戏如果某个怪物 在某一分钟开始时到达城市这会被视为 输掉 游戏在你可以使用武器之前游戏就会结束
//
// 返回在你输掉游戏前可以消灭的怪物的 最大 数量如果你可以在所有怪物到达城市前将它们全部消灭返回 n
//
//
//
// 示例 1
//
//
//输入dist = [1,3,4], speed = [1,1,1]
//输出3
//解释
// 0 分钟开始时怪物的距离是 [1,3,4]你消灭了第一个怪物
// 1 分钟开始时怪物的距离是 [X,2,3]你没有消灭任何怪物
// 2 分钟开始时怪物的距离是 [X,1,2]你消灭了第二个怪物
// 3 分钟开始时怪物的距离是 [X,X,1]你消灭了第三个怪物
//所有 3 个怪物都可以被消灭
//
// 示例 2
//
//
//输入dist = [1,1,2,3], speed = [1,1,1,1]
//输出1
//解释
// 0 分钟开始时怪物的距离是 [1,1,2,3]你消灭了第一个怪物
// 1 分钟开始时怪物的距离是 [X,0,1,2]你输掉了游戏
//你只能消灭 1 个怪物
//
//
// 示例 3
//
//
//输入dist = [3,2,4], speed = [5,3,2]
//输出1
//解释
// 0 分钟开始时怪物的距离是 [3,2,4]你消灭了第一个怪物
// 1 分钟开始时怪物的距离是 [X,0,2]你输掉了游戏
//你只能消灭 1 个怪物
//
//
//
//
// 提示
//
//
// n == dist.length == speed.length
// 1 <= n <= 10
// 1 <= dist[i], speed[i] <= 10
//
// Related Topics 贪心 数组 排序 👍 13 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
//1921:消灭怪物的最大数量
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);
}
index++;
}
return size;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,75 @@
//给你一个整数数组 nums 返回数组中最大数和最小数的 最大公约数
//
// 两个数的 最大公约数 是能够被两个数整除的最大正整数
//
//
//
// 示例 1
//
// 输入nums = [2,5,6,9,10]
//输出2
//解释
//nums 中最小的数是 2
//nums 中最大的数是 10
//2 10 的最大公约数是 2
//
//
// 示例 2
//
// 输入nums = [7,5,6,8,3]
//输出1
//解释
//nums 中最小的数是 3
//nums 中最大的数是 8
//3 8 的最大公约数是 1
//
//
// 示例 3
//
// 输入nums = [3,3]
//输出3
//解释
//nums 中最小的数是 3
//nums 中最大的数是 3
//3 3 的最大公约数是 3
//
//
//
//
// 提示
//
//
// 2 <= nums.length <= 1000
// 1 <= nums[i] <= 1000
//
// 👍 2 👎 0
package leetcode.editor.cn;
//1979:找出数组的最大公约数
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;
}
}
return 1;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,94 @@
//一个班级里有 n 个学生编号为 0 n - 1 每个学生会依次回答问题编号为 0 的学生先回答然后是编号为 1 的学生以此类推直到编号为
//n - 1 的学生然后老师会重复这个过程重新从编号为 0 的学生开始回答问题
//
// 给你一个长度为 n 且下标从 0 开始的整数数组 chalk 和一个整数 k 一开始粉笔盒里总共有 k 支粉笔当编号为 i 的学生回答问题时他会消耗
// chalk[i] 支粉笔如果剩余粉笔数量 严格小于 chalk[i] 那么学生 i 需要 补充 粉笔
//
// 请你返回需要 补充 粉笔的学生 编号
//
//
//
// 示例 1
//
// 输入chalk = [5,1,5], k = 22
//输出0
//解释学生消耗粉笔情况如下
//- 编号为 0 的学生使用 5 支粉笔然后 k = 17
//- 编号为 1 的学生使用 1 支粉笔然后 k = 16
//- 编号为 2 的学生使用 5 支粉笔然后 k = 11
//- 编号为 0 的学生使用 5 支粉笔然后 k = 6
//- 编号为 1 的学生使用 1 支粉笔然后 k = 5
//- 编号为 2 的学生使用 5 支粉笔然后 k = 0
//编号为 0 的学生没有足够的粉笔所以他需要补充粉笔
//
// 示例 2
//
// 输入chalk = [3,4,1,2], k = 25
//输出1
//解释学生消耗粉笔情况如下
//- 编号为 0 的学生使用 3 支粉笔然后 k = 22
//- 编号为 1 的学生使用 4 支粉笔然后 k = 18
//- 编号为 2 的学生使用 1 支粉笔然后 k = 17
//- 编号为 3 的学生使用 2 支粉笔然后 k = 15
//- 编号为 0 的学生使用 3 支粉笔然后 k = 12
//- 编号为 1 的学生使用 4 支粉笔然后 k = 8
//- 编号为 2 的学生使用 1 支粉笔然后 k = 7
//- 编号为 3 的学生使用 2 支粉笔然后 k = 5
//- 编号为 0 的学生使用 3 支粉笔然后 k = 2
//编号为 1 的学生没有足够的粉笔所以他需要补充粉笔
//
//
//
//
// 提示
//
//
// chalk.length == n
// 1 <= n <= 10
// 1 <= chalk[i] <= 10
// 1 <= k <= 10
//
// Related Topics 数组 二分查找 前缀和 模拟 👍 4 👎 0
package leetcode.editor.cn;
//1894:找到需要补充粉笔的学生编号
class FindTheStudentThatWillReplaceTheChalk{
public static void main(String[] args) {
//测试代码
Solution solution = new FindTheStudentThatWillReplaceTheChalk().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int chalkReplacer(int[] chalk, int k) {
int temp = k;
int length = chalk.length;
int[] free = new int[length];
for (int i = 0; i < length; i++) {
if (i == 0) {
free[i] = chalk[i];
} else {
free[i] += chalk[i] + free[i - 1];
}
temp -= chalk[i];
if (temp < 0) {
return i;
}
}
temp %= free[length - 1];
if (temp == 0) {
return 0;
}
for (int i = 0; i < length; i++) {
temp -= chalk[i];
if (temp < 0) {
return i;
}
}
return 0;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,74 @@
//给你一个字符串数组 nums 该数组由 n 互不相同 的二进制字符串组成且每个字符串长度都是 n 请你找出并返回一个长度为 n 没有出现
//nums 中的二进制字符串如果存在多种答案只需返回 任意一个 即可
//
//
//
// 示例 1
//
//
//输入nums = ["01","10"]
//输出"11"
//解释"11" 没有出现在 nums "00" 也是正确答案
//
//
// 示例 2
//
//
//输入nums = ["00","01"]
//输出"11"
//解释"11" 没有出现在 nums "10" 也是正确答案
//
//
// 示例 3
//
//
//输入nums = ["111","011","001"]
//输出"101"
//解释"101" 没有出现在 nums "000""010""100""110" 也是正确答案
//
//
//
// 提示
//
//
// n == nums.length
// 1 <= n <= 16
// nums[i].length == n
// nums[i] '0' '1'
//
// 👍 2 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
import java.util.List;
//1980:找出不同的二进制字符串
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;
}
return temp;
}
}
return null;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,60 @@
//给定两个数组编写一个函数来计算它们的交集
//
//
//
// 示例 1
//
// 输入nums1 = [1,2,2,1], nums2 = [2,2]
//输出[2]
//
//
// 示例 2
//
// 输入nums1 = [4,9,5], nums2 = [9,4,9,8,4]
//输出[9,4]
//
//
//
// 说明
//
//
// 输出结果中的每个元素一定是唯一的
// 我们可以不考虑输出结果的顺序
//
// Related Topics 数组 哈希表 双指针 二分查找 排序 👍 406 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
//349:两个数组的交集
class IntersectionOfTwoArrays {
public static void main(String[] args) {
//测试代码
Solution solution = new IntersectionOfTwoArrays().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set = new HashSet<>();
for (int num : nums1) {
set.add(num);
}
int index = 0;
for (int num : nums2) {
if (set.contains(num)) {
set.remove(num);
nums1[index] = num;
index++;
}
}
return Arrays.copyOf(nums1, index);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,86 @@
//给你一个字符串 num 该字符串表示一个大整数另给你一个长度为 10 下标从 0 开始 的整数数组 change 该数组将 0-9 中的每个数字映
//射到另一个数字更规范的说法是数字 d 映射为数字 change[d]
//
// 你可以选择 突变 num 的任一子字符串突变 子字符串意味着将每位数字 num[i] 替换为该数字在 change 中的映射也就是说 num[i]
// 替换为 change[num[i]]
//
// 请你找出在对 num 的任一子字符串执行突变操作也可以不执行可能得到的 最大整数 并用字符串表示返回
//
// 子字符串 是字符串中的一个连续序列
//
//
//
// 示例 1
//
// 输入num = "132", change = [9,8,5,0,3,6,4,2,6,8]
//输出"832"
//解释替换子字符串 "1"
//- 1 映射为 change[1] = 8
//因此 "132" 变为 "832"
//"832" 是可以构造的最大整数所以返回它的字符串表示
//
//
// 示例 2
//
// 输入num = "021", change = [9,4,3,5,7,2,1,9,0,6]
//输出"934"
//解释替换子字符串 "021"
//- 0 映射为 change[0] = 9
//- 2 映射为 change[2] = 3
//- 1 映射为 change[1] = 4
//因此"021" 变为 "934"
//"934" 是可以构造的最大整数所以返回它的字符串表示
//
//
// 示例 3
//
// 输入num = "5", change = [1,4,7,5,3,2,5,6,9,4]
//输出"5"
//解释"5" 已经是可以构造的最大整数所以返回它的字符串表示
//
//
//
//
// 提示
//
//
// 1 <= num.length <= 10
// num 仅由数字 0-9 组成
// change.length == 10
// 0 <= change[d] <= 9
//
// Related Topics 贪心 数组 字符串 👍 5 👎 0
package leetcode.editor.cn;
//1946:子字符串突变后可能得到的最大整数
class LargestNumberAfterMutatingSubstring{
public static void main(String[] args) {
//测试代码
Solution solution = new LargestNumberAfterMutatingSubstring().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String maximumNumber(String num, int[] change) {
StringBuilder str = new StringBuilder();
int flag = 0;
int i = 0;
for (; i < num.length(); i++) {
int n = num.charAt(i) - '0';
if (n > change[n] && flag == 1) {
break;
}
if (n < change[n]) {
str.append(change[n]);
flag = 1;
}else{
str.append(n);
}
}
return str + num.substring(i);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,99 @@
//有一份由 n 个问题组成的调查问卷每个问题的答案要么是 0no要么是 1yes
//
// 这份调查问卷被分发给 m 名学生和 m 名导师学生和导师的编号都是从 0 m - 1 学生的答案用一个二维整数数组 students 表示其中
//students[i] 是一个整数数组包含第 i 名学生对调查问卷给出的答案下标从 0 开始导师的答案用一个二维整数数组 mentors 表示其中
//mentors[j] 是一个整数数组包含第 j 名导师对调查问卷给出的答案下标从 0 开始
//
// 每个学生都会被分配给 一名 导师而每位导师也会分配到 一名 学生配对的学生与导师之间的兼容性评分等于学生和导师答案相同的次数
//
//
// 例如学生答案为[1, 0, 1] 而导师答案为 [0, 0, 1] 那么他们的兼容性评分为 2 因为只有第二个和第三个答案相同
//
//
// 请你找出最优的学生与导师的配对方案 最大程度上 提高 兼容性评分和
//
// 给你 students mentors 返回可以得到的 最大兼容性评分和
//
//
//
// 示例 1
//
// 输入students = [[1,1,0],[1,0,1],[0,0,1]], mentors = [[1,0,0],[0,0,1],[1,1,0]]
//输出8
//解释按下述方式分配学生和导师
//- 学生 0 分配给导师 2 兼容性评分为 3
//- 学生 1 分配给导师 0 兼容性评分为 2
//- 学生 2 分配给导师 1 兼容性评分为 3
//最大兼容性评分和为 3 + 2 + 3 = 8
//
// 示例 2
//
// 输入students = [[0,0],[0,0],[0,0]], mentors = [[1,1],[1,1],[1,1]]
//输出0
//解释任意学生与导师配对的兼容性评分都是 0
//
//
//
//
// 提示
//
//
// m == students.length == mentors.length
// n == students[i].length == mentors[j].length
// 1 <= m, n <= 8
// students[i][k] 0 1
// mentors[j][k] 0 1
//
// Related Topics 位运算 数组 动态规划 回溯 状态压缩 👍 12 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.List;
//1947:最大兼容性评分和
class MaximumCompatibilityScoreSum {
public static void main(String[] args) {
//测试代码
Solution solution = new MaximumCompatibilityScoreSum().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
private int max = 0;
public int maxCompatibilitySum(int[][] students, int[][] mentors) {
dfs(0, students, mentors, new ArrayList<>(), 0);
return max;
}
private void dfs(int index, int[][] students, int[][] mentors, List<Integer> use, int sum) {
if (index == students.length) {
max = Math.max(max, sum);
return;
}
int[] student = students[index];
for (int i = 0; i < mentors.length; i++) {
if (use.contains(i)) {
continue;
}
int[] mentor = mentors[i];
int count = 0;
for (int j = 0; j < student.length; j++) {
if (student[j] == mentor[j]) {
count++;
}
}
use.add(i);
sum += count;
dfs(index + 1, students, mentors, use, sum);
use.remove(use.size() - 1);
sum -= count;
}
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,74 @@
//给你一个 n x n 的整数方阵 matrix 你可以执行以下操作 任意次
//
//
// 选择 matrix 相邻 两个元素并将它们都 乘以 -1
//
//
// 如果两个元素有 公共边 那么它们就是 相邻
//
// 你的目的是 最大化 方阵元素的和请你在执行以上操作之后返回方阵的 最大
//
//
//
// 示例 1
//
// 输入matrix = [[1,-1],[-1,1]]
//输出4
//解释我们可以执行以下操作使和等于 4
//- 将第一行的 2 个元素乘以 -1
//- 将第一列的 2 个元素乘以 -1
//
//
// 示例 2
//
// 输入matrix = [[1,2,3],[-1,-2,-3],[1,2,3]]
//输出16
//解释我们可以执行以下操作使和等于 16
//- 将第二行的最后 2 个元素乘以 -1
//
//
//
//
// 提示
//
//
// n == matrix.length == matrix[i].length
// 2 <= n <= 250
// -10 <= matrix[i][j] <= 10
//
// 👍 0 👎 0
package leetcode.editor.cn;
//1975:最大方阵和
class MaximumMatrixSum {
public static void main(String[] args) {
//测试代码
Solution solution = new MaximumMatrixSum().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public long maxMatrixSum(int[][] matrix) {
int min = Integer.MAX_VALUE;
long sum = 0;
long mul = 1;
int count = 0;
for (int[] ints : matrix) {
for (int j = 0; j < matrix[0].length; j++) {
sum += Math.abs(ints[j]);
if (ints[j] != 0) {
mul = mul * ints[j] > 0 ? 1 : -1;
min = Math.min(min, Math.abs(ints[j]));
} else {
count++;
}
}
}
return mul < 0 && count == 0 ? sum - 2L * min : sum;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,85 @@
//给你 n 个项目编号从 0 n - 1 同时给你一个整数数组 milestones 其中每个 milestones[i] 表示第 i 个项目中的阶
//段任务数量
//
// 你可以按下面两个规则参与项目中的工作
//
//
// 每周你将会完成 某一个 项目中的 恰好一个 阶段任务你每周都 必须 工作
// 连续的 两周中 不能 参与并完成同一个项目中的两个阶段任务
//
//
// 一旦所有项目中的全部阶段任务都完成或者仅剩余一个阶段任务都会导致你违反上面的规则那么你将 停止工作 注意由于这些条件的限制你可能无法完成所有阶段
//任务
//
// 返回在不违反上面规则的情况下你 最多 能工作多少周
//
//
//
// 示例 1
//
//
//输入milestones = [1,2,3]
//输出6
//解释一种可能的情形是
//- 1 你参与并完成项目 0 中的一个阶段任务
//- 2 你参与并完成项目 2 中的一个阶段任务
//- 3 你参与并完成项目 1 中的一个阶段任务
//- 4 你参与并完成项目 2 中的一个阶段任务
//- 5 你参与并完成项目 1 中的一个阶段任务
//- 6 你参与并完成项目 2 中的一个阶段任务
//总周数是 6
//
//
// 示例 2
//
//
//输入milestones = [5,2,1]
//输出7
//解释一种可能的情形是
//- 1 你参与并完成项目 0 中的一个阶段任务
//- 2 你参与并完成项目 1 中的一个阶段任务
//- 3 你参与并完成项目 0 中的一个阶段任务
//- 4 你参与并完成项目 1 中的一个阶段任务
//- 5 你参与并完成项目 0 中的一个阶段任务
//- 6 你参与并完成项目 2 中的一个阶段任务
//- 7 你参与并完成项目 0 中的一个阶段任务
//总周数是 7
//注意你不能在第 8 周参与完成项目 0 中的最后一个阶段任务因为这会违反规则
//因此项目 0 中会有一个阶段任务维持未完成状态
//
//
//
// 提示
//
//
// n == milestones.length
// 1 <= n <= 10
// 1 <= milestones[i] <= 10
//
// Related Topics 贪心 数组 👍 19 👎 0
package leetcode.editor.cn;
//1953:你可以工作的最大周数
class MaximumNumberOfWeeksForWhichYouCanWork {
public static void main(String[] args) {
//测试代码
Solution solution = new MaximumNumberOfWeeksForWhichYouCanWork().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public long numberOfWeeks(int[] milestones) {
long max = 0;
long sum = 0;
for (int milestone : milestones) {
sum += milestone;
max = Math.max(max, milestone);
}
return sum / max >= 2 ? sum : (sum - max) * 2 + 1;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,62 @@
//两个数对 (a, b) (c, d) 之间的 乘积差 定义为 (a * b) - (c * d)
//
//
// 例如(5, 6) (2, 7) 之间的乘积差是 (5 * 6) - (2 * 7) = 16
//
//
// 给你一个整数数组 nums 选出四个 不同的 下标 wxy z 使数对 (nums[w], nums[x]) (nums[y],
//nums[z]) 之间的 乘积差 取到 最大值
//
// 返回以这种方式取得的乘积差中的 最大值
//
//
//
// 示例 1
//
// 输入nums = [5,6,2,7,4]
//输出34
//解释可以选出下标为 1 3 的元素构成第一个数对 (6, 7) 以及下标 2 4 构成第二个数对 (2, 4)
//乘积差是 (6 * 7) - (2 * 4) = 34
//
//
// 示例 2
//
// 输入nums = [4,2,5,9,7,4,8]
//输出64
//解释可以选出下标为 3 6 的元素构成第一个数对 (9, 8) 以及下标 1 5 构成第二个数对 (2, 4)
//乘积差是 (9 * 8) - (2 * 4) = 64
//
//
//
//
// 提示
//
//
// 4 <= nums.length <= 10
// 1 <= nums[i] <= 10
//
// Related Topics 数组 排序 👍 8 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
//1913:两个数对之间的最大乘积差
class MaximumProductDifferenceBetweenTwoPairs {
public static void main(String[] args) {
//测试代码
Solution solution = new MaximumProductDifferenceBetweenTwoPairs().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int maxProductDifference(int[] nums) {
Arrays.sort(nums);
int size = nums.length;
return nums[size - 1] * nums[size - 2] - nums[0] * nums[1];
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,81 @@
//给你一个用无限二维网格表示的花园每一个 整数坐标处都有一棵苹果树整数坐标 (i, j) 处的苹果树有 |i| + |j| 个苹果
//
// 你将会买下正中心坐标是 (0, 0) 的一块 正方形土地 且每条边都与两条坐标轴之一平行
//
// 给你一个整数 neededApples 请你返回土地的 最小周长 使得 至少 neededApples 个苹果在土地 里面或者边缘上
//
// |x| 的值定义为
//
//
// 如果 x >= 0 那么值为 x
// 如果 x < 0 那么值为 -x
//
//
//
//
// 示例 1
//
//
//输入neededApples = 1
//输出8
//解释边长长度为 1 的正方形不包含任何苹果
//但是边长为 2 的正方形包含 12 个苹果如上图所示
//周长为 2 * 4 = 8
//
//
// 示例 2
//
//
//输入neededApples = 13
//输出16
//
//
// 示例 3
//
//
//输入neededApples = 1000000000
//输出5040
//
//
//
//
// 提示
//
//
// 1 <= neededApples <= 10¹
//
// Related Topics 数学 二分查找 👍 11 👎 0
package leetcode.editor.cn;
//1954:收集足够苹果的最小花园周长
class MinimumGardenPerimeterToCollectEnoughApples {
public static void main(String[] args) {
//测试代码
Solution solution = new MinimumGardenPerimeterToCollectEnoughApples().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public long minimumPerimeter(long neededApples) {
if (neededApples == 0) {
return 0;
}
if (neededApples < 12) {
return 8;
}
long n = 2;
long cur = 12;
long sum = 12;
while (sum < neededApples) {
cur = cur + 12 * (n + 1);
sum += cur;
n += 2;
}
return 4 * n;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,97 @@
//有一个特殊打字机它由一个 圆盘 和一个 指针 组成 圆盘上标有小写英文字母 'a' 'z'只有 当指针指向某个字母时它才能被键入指针 初始时
//指向字符 'a'
//
// 每一秒钟你可以执行以下操作之一
//
//
// 将指针 顺时针 或者 逆时针 移动一个字符
// 键入指针 当前 指向的字符
//
//
// 给你一个字符串 word 请你返回键入 word 所表示单词的 最少 秒数
//
//
//
// 示例 1
//
//
//输入word = "abc"
//输出5
//解释
//单词按如下操作键入
//- 1 秒键入字符 'a' in 1 因为指针初始指向 'a' 故不需移动指针
//- 1 秒将指针顺时针移到 'b'
//- 1 秒键入字符 'b'
//- 1 秒将指针顺时针移到 'c'
//- 1 秒键入字符 'c'
//
//
// 示例 2
//
//
//输入word = "bza"
//输出7
//解释
//单词按如下操作键入
//- 1 秒将指针顺时针移到 'b'
//- 1 秒键入字符 'b'
//- 2 秒将指针逆时针移到 'z'
//- 1 秒键入字符 'z'
//- 1 秒将指针顺时针移到 'a'
//- 1 秒键入字符 'a'
//
//
// 示例 3
//
//
//输入word = "zjpc"
//输出34
//解释
//单词按如下操作键入
//- 1 秒将指针逆时针移到 'z'
//- 1 秒键入字符 'z'
//- 10 秒将指针顺时针移到 'j'
//- 1 秒键入字符 'j'
//- 6 秒将指针顺时针移到 'p'
//- 1 秒键入字符 'p'
//- 13 秒将指针逆时针移到 'c'
//- 1 秒键入字符 'c'
//
//
//
//
// 提示
//
//
// 1 <= word.length <= 100
// word 只包含小写英文字母
//
// 👍 2 👎 0
package leetcode.editor.cn;
//1974:使用特殊打字机键入单词的最少时间
class MinimumTimeToTypeWordUsingSpecialTypewriter {
public static void main(String[] args) {
//测试代码
Solution solution = new MinimumTimeToTypeWordUsingSpecialTypewriter().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int minTimeToType(String word) {
int time = 0;
char[] chs = word.toCharArray();
time += chs.length;
time += Math.min(26 - Math.abs(chs[0] - 'a'), Math.abs(chs[0] - 'a'));
for (int i = 1; i < chs.length; i++) {
int abs = Math.abs(chs[i] - chs[i - 1]);
time += Math.min(26 - abs, abs);
}
return time;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,89 @@
//给定一个整数数据流和一个窗口大小根据该滑动窗口的大小计算其所有整数的移动平均值
//
// 实现 MovingAverage
//
//
// MovingAverage(int size) 用窗口大小 size 初始化对象
// double next(int val) 计算并返回数据流中最后 size 个值的移动平均值
//
//
//
//
// 示例
//
//
//输入
//["MovingAverage", "next", "next", "next", "next"]
//[[3], [1], [10], [3], [5]]
//输出
//[null, 1.0, 5.5, 4.66667, 6.0]
//
//解释
//MovingAverage movingAverage = new MovingAverage(3);
//movingAverage.next(1); // 返回 1.0 = 1 / 1
//movingAverage.next(10); // 返回 5.5 = (1 + 10) / 2
//movingAverage.next(3); // 返回 4.66667 = (1 + 10 + 3) / 3
//movingAverage.next(5); // 返回 6.0 = (10 + 3 + 5) / 3
//
//
//
//
// 提示
//
//
// 1 <= size <= 1000
// -10 <= val <= 10
// 最多调用 next 方法 10
//
// Related Topics 设计 队列 数组 数据流 👍 66 👎 0
package leetcode.editor.cn;
import java.util.LinkedList;
import java.util.Queue;
//346:数据流中的移动平均值
class MovingAverageFromDataStream {
public static void main(String[] args) {
//测试代码
// Solution solution = new MovingAverageFromDataStream().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class MovingAverage {
Queue<Integer> queue;
int size;
double sum = 0.0;
/**
* Initialize your data structure here.
*/
public MovingAverage(int size) {
queue = new LinkedList<>();
this.size = size;
}
public double next(int val) {
if (queue.size() >= size) {
sum += 1.0 * (val - queue.poll()) / size;
} else {
if (queue.isEmpty()) {
sum += val;
} else {
sum = (sum * queue.size() + val) / (queue.size() + 1);
}
}
queue.offer(val);
return sum;
}
}
/**
* Your MovingAverage object will be instantiated and called as such:
* MovingAverage obj = new MovingAverage(size);
* double param_1 = obj.next(val);
*/
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,36 @@
//实现 pow(x, n) 即计算 x n 次幂函数x
//
// Related Topics 递归 数学 👍 718 👎 0
package leetcode.editor.cn;
//50:Pow(x, n)
class PowxN {
public static void main(String[] args) {
//测试代码
Solution solution = new PowxN().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public double myPow(double x, int n) {
double ans = 1;
if (n < 0) {
x = 1 / x;
}
long exp = n;
exp = Math.abs(exp);
while (exp > 0) {
if (exp % 2 == 1) {
ans = ans * x;
}
x *= x;
exp /= 2;
}
return ans;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,67 @@
//给你一个字符串数组 words下标 0 开始 计数
//
// 在一步操作中需先选出两个 不同 下标 i j其中 words[i] 是一个非空字符串接着将 words[i] 中的 任一 字符移动到
//words[j] 中的 任一 位置上
//
// 如果执行任意步操作可以使 words 中的每个字符串都相等返回 true 否则返回 false
//
//
//
// 示例 1
//
// 输入words = ["abc","aabc","bc"]
//输出true
//解释 words[1] 中的第一个 'a' 移动到 words[2] 的最前面
//使 words[1] = "abc" words[2] = "abc"
//所有字符串都等于 "abc" 所以返回 true
//
//
// 示例 2
//
// 输入words = ["ab","a"]
//输出false
//解释执行操作无法使所有字符串都相等
//
//
//
//
// 提示
//
//
// 1 <= words.length <= 100
// 1 <= words[i].length <= 100
// words[i] 由小写英文字母组成
//
// Related Topics 哈希表 字符串 计数 👍 4 👎 0
package leetcode.editor.cn;
//1897:重新分配字符使所有字符串都相等
class RedistributeCharactersToMakeAllStringsEqual {
public static void main(String[] args) {
//测试代码
Solution solution = new RedistributeCharactersToMakeAllStringsEqual().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean makeEqual(String[] words) {
int[] count = new int[26];
int size = words.length;
for (String word : words) {
for (char ch : word.toCharArray()) {
count[ch - 'a']++;
}
}
for (int i = 0; i < 26; i++) {
if (count[i] % size > 0) {
return false;
}
}
return true;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,94 @@
//给你一个由小写字母组成的字符串 s 以及一个整数 k
//
// 首先用字母在字母表中的位置替换该字母 s 转化 为一个整数也就是'a' 1 替换'b' 2 替换... 'z' 26 替换
//将整数 转换 为其 各位数字之和 共重复 转换 操作 k
//
// 例如如果 s = "zbax" k = 2 那么执行下述步骤后得到的结果是整数 8
//
//
// 转化"zbax" "(26)(2)(1)(24)" "262124" 262124
// 转换 #1262124 2 + 6 + 2 + 1 + 2 + 4 17
// 转换 #217 1 + 7 8
//
//
// 返回执行上述操作后得到的结果整数
//
//
//
// 示例 1
//
//
//输入s = "iiii", k = 1
//输出36
//解释操作如下
//- 转化"iiii" "(9)(9)(9)(9)" "9999" 9999
//- 转换 #19999 9 + 9 + 9 + 9 36
//因此结果整数为 36
//
//
// 示例 2
//
//
//输入s = "leetcode", k = 2
//输出6
//解释操作如下
//- 转化"leetcode" "(12)(5)(5)(20)(3)(15)(4)(5)" "12552031545" 12552031545
//- 转换 #112552031545 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 33
//- 转换 #233 3 + 3 6
//因此结果整数为 6
//
//
//
//
// 提示
//
//
// 1 <= s.length <= 100
// 1 <= k <= 10
// s 由小写英文字母组成
//
// Related Topics 字符串 模拟 👍 4 👎 0
package leetcode.editor.cn;
//1945:字符串转化后的各位数字之和
class SumOfDigitsOfStringAfterConvert {
public static void main(String[] args) {
//测试代码
Solution solution = new SumOfDigitsOfStringAfterConvert().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int getLucky(String s, int k) {
int sum = 0;
int count = 0;
for (char ch : s.toCharArray()) {
int num = ch - 'a' + 1;
if (num > 9) {
sum += num / 10 + num % 10;
} else {
sum += num;
}
}
if (sum < 10 || k == 1) {
return sum;
}
for (int i = 1; i < k; i++) {
int result = 0;
while (sum > 9) {
result += sum % 10;
sum /= 10;
}
sum += result;
if (sum < 10) {
break;
}
}
return sum;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,103 @@
//给你一个大小为 m * n 的矩阵 mat矩阵由若干军人和平民组成分别用 1 0 表示
//
// 请你返回矩阵中战斗力最弱的 k 行的索引按从最弱到最强排序
//
// 如果第 i 行的军人数量少于第 j 或者两行军人数量相同但 i 小于 j那么我们认为第 i 行的战斗力比第 j 行弱
//
// 军人 总是 排在一行中的靠前位置也就是说 1 总是出现在 0 之前
//
//
//
// 示例 1
//
//
//输入mat =
//[[1,1,0,0,0],
// [1,1,1,1,0],
// [1,0,0,0,0],
// [1,1,0,0,0],
// [1,1,1,1,1]],
//k = 3
//输出[2,0,3]
//解释
//每行中的军人数目
// 0 -> 2
// 1 -> 4
// 2 -> 1
// 3 -> 2
// 4 -> 5
//从最弱到最强对这些行排序后得到 [2,0,3,1,4]
//
//
// 示例 2
//
//
//输入mat =
//[[1,0,0,0],
// [1,1,1,1],
// [1,0,0,0],
// [1,0,0,0]],
//k = 2
//输出[0,2]
//解释
//每行中的军人数目
// 0 -> 1
// 1 -> 4
// 2 -> 1
// 3 -> 1
//从最弱到最强对这些行排序后得到 [0,2,3,1]
//
//
//
//
// 提示
//
//
// m == mat.length
// n == mat[i].length
// 2 <= n, m <= 100
// 1 <= k <= m
// matrix[i][j] 不是 0 就是 1
//
// Related Topics 数组 二分查找 矩阵 排序 优先队列 👍 120 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
//1337:矩阵中战斗力最弱的 K
class TheKWeakestRowsInAMatrix {
public static void main(String[] args) {
//测试代码
Solution solution = new TheKWeakestRowsInAMatrix().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] kWeakestRows(int[][] mat, int k) {
int m = mat.length, n = mat[0].length;
int[][] all = new int[m][2];
for (int i = 0; i < m; i++) {
int cur = 0;
for (int j = 0; j < n; j++) {
cur += mat[i][j];
}
all[i] = new int[]{cur, i};
}
Arrays.sort(all, (a, b) -> {
if (a[0] != b[0]) {
return a[0] - b[0];
}
return a[1] - b[1];
});
int[] ans = new int[k];
for (int i = 0; i < k; i++) {
ans[i] = all[i][1];
}
return ans;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,113 @@
// n 个朋友在举办一个派对这些朋友从 0 n - 1 编号派对里有 无数 张椅子编号为 0 infinity 当一个朋友到达派对时他会占
// 编号最小 且未被占据的椅子
//
//
// 比方说当一个朋友到达时如果椅子 0 1 5 被占据了那么他会占据 2 号椅子
//
//
// 当一个朋友离开派对时他的椅子会立刻变成未占据状态如果同一时刻有另一个朋友到达可以立即占据这张椅子
//
// 给你一个下标从 0 开始的二维整数数组 times 其中 times[i] = [arrivali, leavingi] 表示第 i 个朋友到达和离开的
//时刻同时给你一个整数 targetFriend 所有到达时间 互不相同
//
// 请你返回编号为 targetFriend 的朋友占据的 椅子编号
//
//
//
// 示例 1
//
// 输入times = [[1,4],[2,3],[4,6]], targetFriend = 1
//输出1
//解释
//- 朋友 0 时刻 1 到达占据椅子 0
//- 朋友 1 时刻 2 到达占据椅子 1
//- 朋友 1 时刻 3 离开椅子 1 变成未占据
//- 朋友 0 时刻 4 离开椅子 0 变成未占据
//- 朋友 2 时刻 4 到达占据椅子 0
//朋友 1 占据椅子 1 所以返回 1
//
//
// 示例 2
//
// 输入times = [[3,10],[1,5],[2,6]], targetFriend = 0
//输出2
//解释
//- 朋友 1 时刻 1 到达占据椅子 0
//- 朋友 2 时刻 2 到达占据椅子 1
//- 朋友 0 时刻 3 到达占据椅子 2
//- 朋友 1 时刻 5 离开椅子 0 变成未占据
//- 朋友 2 时刻 6 离开椅子 1 变成未占据
//- 朋友 0 时刻 10 离开椅子 2 变成未占据
//朋友 0 占据椅子 2 所以返回 2
//
//
//
//
// 提示
//
//
// n == times.length
// 2 <= n <= 10
// times[i].length == 2
// 1 <= arrivali < leavingi <= 10
// 0 <= targetFriend <= n - 1
// 每个 arrivali 时刻 互不相同
//
// Related Topics 数组 有序集合 优先队列 👍 9 👎 0
package leetcode.editor.cn;
import java.util.*;
//1942:最小未被占据椅子的编号
class TheNumberOfTheSmallestUnoccupiedChair {
public static void main(String[] args) {
//测试代码
Solution solution = new TheNumberOfTheSmallestUnoccupiedChair().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int smallestChair(int[][] times, int targetFriend) {
int[] target = times[targetFriend];
Arrays.sort(times, (a, b) -> a[0] - b[0]);
int size = 0;
for (int i = 0; i < times.length; i++) {
if (times[i][0] == target[0]) {
size = i;
break;
}
}
int[] arr = new int[size + 1];
List<Integer> use = new ArrayList<>();
int index = 0;
int start = times[0][0];
for (int[] time : times) {
for (int i = 0; i < index; i++) {
if (arr[i] > 0) {
arr[i] -= time[0] - start;
if (arr[i] <= 0) {
use.add(i);
Collections.sort(use);
}
}
}
start = time[0];
if (time[0] == target[0]) {
return use.size() > 0 ? use.get(0) : index;
}
if (use.size() > 0) {
arr[use.get(0)] = time[1] - time[0];
use.remove(0);
} else {
arr[index] = time[1] - time[0];
index++;
}
}
return 0;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,60 @@
//给你一个整数 n 如果 n 恰好有三个正除数 返回 true 否则返回 false
//
// 如果存在整数 k 满足 n = k * m 那么整数 m 就是 n 的一个 除数
//
//
//
// 示例 1
//
// 输入n = 2
//输出false
//解释2 只有两个除数1 2
//
// 示例 2
//
// 输入n = 4
//输出true
//解释4 有三个除数12 4
//
//
//
//
// 提示
//
//
// 1 <= n <= 10
//
// Related Topics 数学 👍 9 👎 0
package leetcode.editor.cn;
//1952:三除数
class ThreeDivisors {
public static void main(String[] args) {
//测试代码
Solution solution = new ThreeDivisors().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean isThree(int n) {
int flag = 0;
if (n <= 2) {
return false;
}
int num = n / 2;
for (int i = 2; i <= num; i++) {
if (n % i == 0) {
if (flag == 1) {
return false;
}
flag = 1;
}
}
return flag == 1;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,43 @@
<p>给你两个 <strong>非空 </strong>链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。</p>
<p>你可以假设除了数字 0 之外,这两个数字都不会以零开头。</p>
<p> </p>
<p><strong>示例1</strong></p>
<p><img alt="" src="https://pic.leetcode-cn.com/1626420025-fZfzMX-image.png" style="width: 302px; " /></p>
<pre>
<strong>输入:</strong>l1 = [7,2,4,3], l2 = [5,6,4]
<strong>输出:</strong>[7,8,0,7]
</pre>
<p><strong>示例2</strong></p>
<pre>
<strong>输入:</strong>l1 = [2,4,3], l2 = [5,6,4]
<strong>输出:</strong>[8,0,7]
</pre>
<p><strong>示例3</strong></p>
<pre>
<strong>输入:</strong>l1 = [0], l2 = [0]
<strong>输出:</strong>[0]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>链表的长度范围为<code> [1, 100]</code></li>
<li><code>0 <= node.val <= 9</code></li>
<li>输入数据保证链表代表的数字无前导 0</li>
</ul>
<p> </p>
<p><strong>进阶:</strong>如果输入链表不能修改该如何处理?换句话说,不能对列表中的节点进行翻转。</p>
<div><div>Related Topics</div><div><li></li><li>链表</li><li>数学</li></div></div><br><div><li>👍 414</li><li>👎 0</li></div>

View File

@ -0,0 +1,61 @@
<p>给你一个有&nbsp;<code>n</code>&nbsp;个节点的 <strong>有向无环图DAG</strong>,请你找出所有从节点 <code>0</code>&nbsp;到节点 <code>n-1</code>&nbsp;的路径并输出(<strong>不要求按特定顺序</strong></p>
<p>二维数组的第 <code>i</code> 个数组中的单元都表示有向图中 <code>i</code> 号节点所能到达的下一些节点,空就是没有下一个结点了。</p>
<p>译者注:有向图是有方向的,即规定了 a→b 你就不能从 b→a 。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/09/28/all_1.jpg" style="height: 242px; width: 242px;" /></p>
<pre>
<strong>输入:</strong>graph = [[1,2],[3],[3],[]]
<strong>输出:</strong>[[0,1,3],[0,2,3]]
<strong>解释:</strong>有两条路径 0 -&gt; 1 -&gt; 3 和 0 -&gt; 2 -&gt; 3
</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/09/28/all_2.jpg" style="height: 301px; width: 423px;" /></p>
<pre>
<strong>输入:</strong>graph = [[4,3,1],[3,2,4],[3],[4],[]]
<strong>输出:</strong>[[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>graph = [[1],[]]
<strong>输出:</strong>[[0,1]]
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>graph = [[1,2,3],[2],[3],[]]
<strong>输出:</strong>[[0,1,2,3],[0,2,3],[0,3]]
</pre>
<p><strong>示例 5</strong></p>
<pre>
<strong>输入:</strong>graph = [[1,3],[2],[3],[]]
<strong>输出:</strong>[[0,1,2,3],[0,3]]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == graph.length</code></li>
<li><code>2 &lt;= n &lt;= 15</code></li>
<li><code>0 &lt;= graph[i][j] &lt; n</code></li>
<li><code>graph[i][j] != i</code>(即,不存在自环)</li>
<li><code>graph[i]</code> 中的所有元素 <strong>互不相同</strong></li>
<li>保证输入为 <strong>有向无环图DAG</strong></li>
</ul>
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>广度优先搜索</li><li></li><li>回溯</li></div></div><br><div><li>👍 143</li><li>👎 0</li></div>

View File

@ -0,0 +1,46 @@
<p>给定一个整数数组 <code>asteroids</code>,表示在同一行的行星。</p>
<p>对于数组中的每一个元素,其绝对值表示行星的大小,正负表示行星的移动方向(正表示向右移动,负表示向左移动)。每一颗行星以相同的速度移动。</p>
<p>找出碰撞后剩下的所有行星。碰撞规则:两个行星相互碰撞,较小的行星会爆炸。如果两颗行星大小相同,则两颗行星都会爆炸。两颗移动方向相同的行星,永远不会发生碰撞。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>asteroids = [5,10,-5]
<strong>输出:</strong>[5,10]
<b>解释:</b>10 和 -5 碰撞后只剩下 10 。 5 和 10 永远不会发生碰撞。</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>asteroids = [8,-8]
<strong>输出:</strong>[]
<b>解释:</b>8 和 -8 碰撞后,两者都发生爆炸。</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>asteroids = [10,2,-5]
<strong>输出:</strong>[10]
<b>解释:</b>2 和 -5 发生碰撞后剩下 -5 。10 和 -5 发生碰撞后剩下 10 。</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>asteroids = [-2,-1,1,2]
<strong>输出:</strong>[-2,-1,1,2]
<b>解释</b><strong></strong>-2 和 -1 向左移动,而 1 和 2 向右移动。 由于移动方向相同的行星不会发生碰撞,所以最终没有行星发生碰撞。 </pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 <= asteroids.length <= 10<sup>4</sup></code></li>
<li><code>-1000 <= asteroids[i] <= 1000</code></li>
<li><code>asteroids[i] != 0</code></li>
</ul>
<div><div>Related Topics</div><div><li></li><li>数组</li></div></div><br><div><li>👍 163</li><li>👎 0</li></div>

View File

@ -0,0 +1,33 @@
<p>给你两个整数 <code>left</code><code>right</code> ,表示区间 <code>[left, right]</code> ,返回此区间内所有数字 <strong>按位与</strong> 的结果(包含 <code>left</code><code>right</code> 端点)。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>left = 5, right = 7
<strong>输出:</strong>4
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>left = 0, right = 0
<strong>输出:</strong>0
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>left = 1, right = 2147483647
<strong>输出:</strong>0
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 <= left <= right <= 2<sup>31</sup> - 1</code></li>
</ul>
<div><div>Related Topics</div><div><li>位运算</li></div></div><br><div><li>👍 309</li><li>👎 0</li></div>

View File

@ -0,0 +1,34 @@
<p>给你一个 <strong>从 0 开始的排列</strong> <code>nums</code><strong>下标也从 0 开始</strong>)。请你构建一个 <strong>同样长度</strong> 的数组 <code>ans</code> ,其中,对于每个 <code>i</code><code>0 &lt;= i &lt; nums.length</code>),都满足 <code>ans[i] = nums[nums[i]]</code> 。返回构建好的数组 <code>ans</code></p>
<p><strong>从 0 开始的排列</strong> <code>nums</code> 是一个由 <code>0</code> 到 <code>nums.length - 1</code><code>0</code><code>nums.length - 1</code> 也包含在内)的不同整数组成的数组。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>nums = [0,2,1,5,3,4]
<strong>输出:</strong>[0,1,2,4,5,3]<strong>
解释:</strong>数组 ans 构建如下:
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
= [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]
= [0,1,2,4,5,3]</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>nums = [5,0,1,2,3,4]
<strong>输出:</strong>[4,5,0,1,2,3]
<strong>解释:</strong>数组 ans 构建如下:
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
= [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]]
= [4,5,0,1,2,3]</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
<li><code>0 &lt;= nums[i] &lt; nums.length</code></li>
<li><code>nums</code> 中的元素 <strong>互不相同</strong></li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>模拟</li></div></div><br><div><li>👍 2</li><li>👎 0</li></div>

View File

@ -0,0 +1,30 @@
<p>给你一个字符串 <code>s</code> ,如果 <code>s</code> 是一个 <strong></strong> 字符串,请你返回 <code>true</code> ,否则请返回 <code>false</code> 。</p>
<p>如果 <code>s</code> 中出现过的 <strong>所有</strong> 字符的出现次数 <strong>相同</strong> ,那么我们称字符串 <code>s</code> 是 <strong></strong> 字符串。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre><b>输入:</b>s = "abacbc"
<b>输出:</b>true
<b>解释:</b>s 中出现过的字符为 'a''b' 和 'c' 。s 中所有字符均出现 2 次。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>s = "aaabb"
<b>输出:</b>false
<b>解释:</b>s 中出现过的字符为 'a' 和 'b' 。
'a' 出现了 3 次,'b' 出现了 2 次,两者出现次数不同。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
<li><code>s</code> 只包含小写英文字母。</li>
</ul>
<div><div>Related Topics</div><div><li>哈希表</li><li>字符串</li><li>计数</li></div></div><br><div><li>👍 0</li><li>👎 0</li></div>

View File

@ -0,0 +1,70 @@
<p>给你无向&nbsp;<strong><a href="https://baike.baidu.com/item/连通图/6460995?fr=aladdin" target="_blank">连通</a>&nbsp;</strong>图中一个节点的引用,请你返回该图的&nbsp;<a href="https://baike.baidu.com/item/深拷贝/22785317?fr=aladdin" target="_blank"><strong>深拷贝</strong></a>(克隆)。</p>
<p>图中的每个节点都包含它的值 <code>val</code><code>int</code> 和其邻居的列表(<code>list[Node]</code>)。</p>
<pre>class Node {
public int val;
public List&lt;Node&gt; neighbors;
}</pre>
<p>&nbsp;</p>
<p><strong>测试用例格式:</strong></p>
<p>简单起见,每个节点的值都和它的索引相同。例如,第一个节点值为 1<code>val = 1</code>),第二个节点值为 2<code>val = 2</code>),以此类推。该图在测试用例中使用邻接列表表示。</p>
<p><strong>邻接列表</strong> 是用于表示有限图的无序列表的集合。每个列表都描述了图中节点的邻居集。</p>
<p>给定节点将始终是图中的第一个节点(值为 1。你必须将&nbsp;<strong>给定节点的拷贝&nbsp;</strong>作为对克隆图的引用返回。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/01/133_clone_graph_question.png" style="height: 500px; width: 500px;"></p>
<pre><strong>输入:</strong>adjList = [[2,4],[1,3],[2,4],[1,3]]
<strong>输出:</strong>[[2,4],[1,3],[2,4],[1,3]]
<strong>解释:
</strong>图中有 4 个节点。
节点 1 的值是 1它有两个邻居节点 2 和 4 。
节点 2 的值是 2它有两个邻居节点 1 和 3 。
节点 3 的值是 3它有两个邻居节点 2 和 4 。
节点 4 的值是 4它有两个邻居节点 1 和 3 。
</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/01/graph.png" style="height: 148px; width: 163px;"></p>
<pre><strong>输入:</strong>adjList = [[]]
<strong>输出:</strong>[[]]
<strong>解释:</strong>输入包含一个空列表。该图仅仅只有一个值为 1 的节点,它没有任何邻居。
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>adjList = []
<strong>输出:</strong>[]
<strong>解释:</strong>这个图是空的,它不含任何节点。
</pre>
<p><strong>示例 4</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/01/graph-1.png" style="height: 133px; width: 272px;"></p>
<pre><strong>输入:</strong>adjList = [[2],[1]]
<strong>输出:</strong>[[2],[1]]</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ol>
<li>节点数不超过 100 。</li>
<li>每个节点值&nbsp;<code>Node.val</code> 都是唯一的,<code>1 &lt;= Node.val &lt;= 100</code></li>
<li>无向图是一个<a href="https://baike.baidu.com/item/简单图/1680528?fr=aladdin" target="_blank">简单图</a>,这意味着图中没有重复的边,也没有自环。</li>
<li>由于图是无向的,如果节点 <em>p</em> 是节点 <em>q</em> 的邻居,那么节点 <em>q</em> 也必须是节点 <em>p</em>&nbsp;的邻居。</li>
<li>图是连通图,你可以从给定节点访问到所有节点。</li>
</ol>
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>广度优先搜索</li><li></li><li>哈希表</li></div></div><br><div><li>👍 393</li><li>👎 0</li></div>

View File

@ -0,0 +1,40 @@
<p>给定一个数组 <code>candidates</code> 和一个目标数 <code>target</code> ,找出 <code>candidates</code> 中所有可以使数字和为 <code>target</code> 的组合。</p>
<p><code>candidates</code> 中的每个数字在每个组合中只能使用一次。</p>
<p><strong>注意:</strong>解集不能包含重复的组合。 </p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> candidates = <code>[10,1,2,7,6,1,5]</code>, target = <code>8</code>,
<strong>输出:</strong>
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> candidates = [2,5,2,1,2], target = 5,
<strong>输出:</strong>
[
[1,2,2],
[5]
]</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= candidates.length <= 100</code></li>
<li><code>1 <= candidates[i] <= 50</code></li>
<li><code>1 <= target <= 30</code></li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>回溯</li></div></div><br><div><li>👍 668</li><li>👎 0</li></div>

View File

@ -0,0 +1,15 @@
<p>输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>head = [1,3,2]
<strong>输出:</strong>[2,3,1]</pre>
<p>&nbsp;</p>
<p><strong>限制:</strong></p>
<p><code>0 &lt;= 链表长度 &lt;= 10000</code></p>
<div><div>Related Topics</div><div><li></li><li>递归</li><li>链表</li><li>双指针</li></div></div><br><div><li>👍 173</li><li>👎 0</li></div>

View File

@ -0,0 +1,28 @@
<p>一个 <strong>平方和三元组</strong> <code>(a,b,c)</code> 指的是满足 <code>a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup></code> 的 <strong>整数 </strong>三元组 <code>a</code><code>b</code> 和 <code>c</code> 。</p>
<p>给你一个整数 <code>n</code> ,请你返回满足<em> </em><code>1 &lt;= a, b, c &lt;= n</code> 的 <strong>平方和三元组</strong> 的数目。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre><b>输入:</b>n = 5
<b>输出:</b>2
<b>解释:</b>平方和三元组为 (3,4,5) 和 (4,3,5) 。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>n = 10
<b>输出:</b>4
<b>解释:</b>平方和三元组为 (3,4,5)(4,3,5)(6,8,10) 和 (8,6,10) 。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 250</code></li>
</ul>
<div><div>Related Topics</div><div><li>数学</li><li>枚举</li></div></div><br><div><li>👍 5</li><li>👎 0</li></div>

Some files were not shown because too many files have changed in this diff Show More