Merge branch 'master' of https://gitee.com/huangge1199_admin/leet-code
Conflicts: src/main/java/leetcode/editor/cn/all.json
This commit is contained in:
commit
8a70c75786
103
src/main/java/leetcode/editor/cn/CoinChange.java
Normal file
103
src/main/java/leetcode/editor/cn/CoinChange.java
Normal file
@ -0,0 +1,103 @@
|
||||
//给你一个整数数组 coins ,表示不同面额的硬币;以及一个整数 amount ,表示总金额。
|
||||
//
|
||||
// 计算并返回可以凑成总金额所需的 最少的硬币个数 。如果没有任何一种硬币组合能组成总金额,返回 -1 。
|
||||
//
|
||||
// 你可以认为每种硬币的数量是无限的。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:coins = [1, 2, 5], amount = 11
|
||||
//输出:3
|
||||
//解释:11 = 5 + 5 + 1
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:coins = [2], amount = 3
|
||||
//输出:-1
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:coins = [1], amount = 0
|
||||
//输出:0
|
||||
//
|
||||
//
|
||||
// 示例 4:
|
||||
//
|
||||
//
|
||||
//输入:coins = [1], amount = 1
|
||||
//输出:1
|
||||
//
|
||||
//
|
||||
// 示例 5:
|
||||
//
|
||||
//
|
||||
//输入:coins = [1], amount = 2
|
||||
//输出:2
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= coins.length <= 12
|
||||
// 1 <= coins[i] <= 231 - 1
|
||||
// 0 <= amount <= 104
|
||||
//
|
||||
// Related Topics 广度优先搜索 数组 动态规划
|
||||
// 👍 1399 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
//322:零钱兑换
|
||||
public class CoinChange {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CoinChange().new Solution();
|
||||
System.out.println(solution.coinChange(new int[]{1, 2, 5}, 11));
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int coinChange(int[] coins, int amount) {
|
||||
if (amount < 1) {
|
||||
return 0;
|
||||
}
|
||||
return coinChange(coins, amount, new HashMap());
|
||||
}
|
||||
|
||||
private int coinChange(int[] coins, int rem, Map<Integer, Integer> map) {
|
||||
if (rem < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (rem == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (map.containsKey(rem)) {
|
||||
return map.get(rem);
|
||||
}
|
||||
int min = Integer.MAX_VALUE;
|
||||
for (int coin : coins) {
|
||||
int count = coinChange(coins, rem - coin, map);
|
||||
if (count >= 0 && count < min) {
|
||||
min = 1 + count;
|
||||
}
|
||||
}
|
||||
min = (min == Integer.MAX_VALUE) ? -1 : min;
|
||||
map.put(rem, min);
|
||||
return min;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
52
src/main/java/leetcode/editor/cn/CoinChange.md
Normal file
52
src/main/java/leetcode/editor/cn/CoinChange.md
Normal file
@ -0,0 +1,52 @@
|
||||
<p>给你一个整数数组 <code>coins</code> ,表示不同面额的硬币;以及一个整数 <code>amount</code> ,表示总金额。</p>
|
||||
|
||||
<p>计算并返回可以凑成总金额所需的 <strong>最少的硬币个数</strong> 。如果没有任何一种硬币组合能组成总金额,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p>你可以认为每种硬币的数量是无限的。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>coins = <code>[1, 2, 5]</code>, amount = <code>11</code>
|
||||
<strong>输出:</strong><code>3</code>
|
||||
<strong>解释:</strong>11 = 5 + 5 + 1</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>coins = <code>[2]</code>, amount = <code>3</code>
|
||||
<strong>输出:</strong>-1</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>coins = [1], amount = 0
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>coins = [1], amount = 1
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>coins = [1], amount = 2
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= coins.length <= 12</code></li>
|
||||
<li><code>1 <= coins[i] <= 2<sup>31</sup> - 1</code></li>
|
||||
<li><code>0 <= amount <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>广度优先搜索</li><li>数组</li><li>动态规划</li></div></div>\n<div><li>👍 1399</li><li>👎 0</li></div>
|
@ -0,0 +1,88 @@
|
||||
//给你一个整数数组 nums ,你需要找出一个 连续子数组 ,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序。
|
||||
//
|
||||
// 请你找出符合题意的 最短 子数组,并输出它的长度。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:nums = [2,6,4,8,10,9,15]
|
||||
//输出:5
|
||||
//解释:你只需要对 [6, 4, 8, 10, 9] 进行升序排序,那么整个表都会变为升序排序。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:nums = [1,2,3,4]
|
||||
//输出:0
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:nums = [1]
|
||||
//输出:0
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= nums.length <= 104
|
||||
// -105 <= nums[i] <= 105
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 进阶:你可以设计一个时间复杂度为 O(n) 的解决方案吗?
|
||||
//
|
||||
//
|
||||
// Related Topics 栈 贪心 数组 双指针 排序 单调栈
|
||||
// 👍 596 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author huangge1199
|
||||
*/
|
||||
//581:最短无序连续子数组
|
||||
public class ShortestUnsortedContinuousSubarray {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new ShortestUnsortedContinuousSubarray().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int findUnsortedSubarray(int[] nums) {
|
||||
int[] sort = Arrays.copyOf(nums, nums.length);
|
||||
Arrays.sort(sort);
|
||||
boolean bl = false;
|
||||
int start = 0;
|
||||
int end = nums.length - 1;
|
||||
while (start < end) {
|
||||
if (nums[start] != sort[start]) {
|
||||
break;
|
||||
}
|
||||
start++;
|
||||
}
|
||||
while (start < end) {
|
||||
if (nums[end] != sort[end]) {
|
||||
break;
|
||||
}
|
||||
end--;
|
||||
}
|
||||
return start == end ? 0 : end - start + 1;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
<p>给你一个整数数组 <code>nums</code> ,你需要找出一个 <strong>连续子数组</strong> ,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序。</p>
|
||||
|
||||
<p>请你找出符合题意的 <strong>最短</strong> 子数组,并输出它的长度。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<div class="original__bRMd">
|
||||
<div>
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,6,4,8,10,9,15]
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>你只需要对 [6, 4, 8, 10, 9] 进行升序排序,那么整个表都会变为升序排序。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3,4]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>你可以设计一个时间复杂度为 <code>O(n)</code> 的解决方案吗?</p>
|
||||
</div>
|
||||
</div>
|
||||
<div><div>Related Topics</div><div><li>栈</li><li>贪心</li><li>数组</li><li>双指针</li><li>排序</li><li>单调栈</li></div></div>\n<div><li>👍 596</li><li>👎 0</li></div>
|
73
src/main/java/leetcode/editor/cn/UniquePaths.java
Normal file
73
src/main/java/leetcode/editor/cn/UniquePaths.java
Normal file
@ -0,0 +1,73 @@
|
||||
//一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。
|
||||
//
|
||||
// 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。
|
||||
//
|
||||
// 问总共有多少条不同的路径?
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:m = 3, n = 7
|
||||
//输出:28
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:m = 3, n = 2
|
||||
//输出:3
|
||||
//解释:
|
||||
//从左上角开始,总共有 3 条路径可以到达右下角。
|
||||
//1. 向右 -> 向下 -> 向下
|
||||
//2. 向下 -> 向下 -> 向右
|
||||
//3. 向下 -> 向右 -> 向下
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:m = 7, n = 3
|
||||
//输出:28
|
||||
//
|
||||
//
|
||||
// 示例 4:
|
||||
//
|
||||
//
|
||||
//输入:m = 3, n = 3
|
||||
//输出:6
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= m, n <= 100
|
||||
// 题目数据保证答案小于等于 2 * 109
|
||||
//
|
||||
// Related Topics 数学 动态规划 组合数学
|
||||
// 👍 1065 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
/**
|
||||
* @author huangge1199
|
||||
*/
|
||||
//62:不同路径
|
||||
public class UniquePaths {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new UniquePaths().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int uniquePaths(int m, int n) {
|
||||
int max = Math.max(m, n);
|
||||
return max * (max + 1) / 2;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
48
src/main/java/leetcode/editor/cn/UniquePaths.md
Normal file
48
src/main/java/leetcode/editor/cn/UniquePaths.md
Normal file
@ -0,0 +1,48 @@
|
||||
<p>一个机器人位于一个 <code>m x n</code><em> </em>网格的左上角 (起始点在下图中标记为 “Start” )。</p>
|
||||
|
||||
<p>机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。</p>
|
||||
|
||||
<p>问总共有多少条不同的路径?</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png" />
|
||||
<pre>
|
||||
<strong>输入:</strong>m = 3, n = 7
|
||||
<strong>输出:</strong>28</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>m = 3, n = 2
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>
|
||||
从左上角开始,总共有 3 条路径可以到达右下角。
|
||||
1. 向右 -> 向下 -> 向下
|
||||
2. 向下 -> 向下 -> 向右
|
||||
3. 向下 -> 向右 -> 向下
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>m = 7, n = 3
|
||||
<strong>输出:</strong>28
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>m = 3, n = 3
|
||||
<strong>输出:</strong>6</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= m, n <= 100</code></li>
|
||||
<li>题目数据保证答案小于等于 <code>2 * 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>数学</li><li>动态规划</li><li>组合数学</li></div></div>\n<div><li>👍 1065</li><li>👎 0</li></div>
|
119
src/main/java/leetcode/editor/cn/WordSearch.java
Normal file
119
src/main/java/leetcode/editor/cn/WordSearch.java
Normal file
@ -0,0 +1,119 @@
|
||||
//给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。
|
||||
//
|
||||
// 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "AB
|
||||
//CCED"
|
||||
//输出:true
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SE
|
||||
//E"
|
||||
//输出:true
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "AB
|
||||
//CB"
|
||||
//输出:false
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// m == board.length
|
||||
// n = board[i].length
|
||||
// 1 <= m, n <= 6
|
||||
// 1 <= word.length <= 15
|
||||
// board 和 word 仅由大小写英文字母组成
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 进阶:你可以使用搜索剪枝的技术来优化解决方案,使其在 board 更大的情况下可以更快解决问题?
|
||||
// Related Topics 数组 回溯 矩阵
|
||||
// 👍 973 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
//79:单词搜索
|
||||
public class WordSearch{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new WordSearch().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public boolean exist(char[][] board, String word) {
|
||||
|
||||
String str = Arrays.deepToString(board);
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
if (str.indexOf(word.charAt(i)) == -1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < board.length; i++) {
|
||||
for (int j = 0; j < board[0].length; j++) {
|
||||
if (board[i][j] == word.charAt(0)) {
|
||||
if (dfs(board, word, i, j, 0, new int[board.length][board[0].length])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 深度优先搜索
|
||||
* @param board 传入的二维字符网格
|
||||
* @param word 目标字符串
|
||||
* @param i 起点 i 坐标
|
||||
* @param j 起点 j 坐标
|
||||
* @param index 遍历到word的哪一个位置了
|
||||
* @param visit 访问数组,记录哪些地方已经走过
|
||||
* @return
|
||||
*/
|
||||
private boolean dfs(char[][] board, String word, int i, int j, int index, int[][] visit) {
|
||||
|
||||
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length
|
||||
|| board[i][j] != word.charAt(index) || visit[i][j] == 1) {
|
||||
return false;
|
||||
}
|
||||
if (index == word.length() - 1) {
|
||||
return true;
|
||||
}
|
||||
visit[i][j] = 1;
|
||||
int[][] direction = new int[][]{{i - 1, j}, {i + 1, j}, {i, j - 1}, {i, j + 1}};
|
||||
for (int[] d : direction) {
|
||||
int[][] copy = new int[visit.length][visit[0].length];
|
||||
for (int k = 0; k < visit.length; k++) {
|
||||
copy[k] = Arrays.copyOf(visit[k], visit[0].length);
|
||||
}
|
||||
if (dfs(board, word, d[0], d[1], index + 1, copy)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
43
src/main/java/leetcode/editor/cn/WordSearch.md
Normal file
43
src/main/java/leetcode/editor/cn/WordSearch.md
Normal file
@ -0,0 +1,43 @@
|
||||
<p>给定一个 <code>m x n</code> 二维字符网格 <code>board</code> 和一个字符串单词 <code>word</code> 。如果 <code>word</code> 存在于网格中,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p>单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/04/word2.jpg" style="width: 322px; height: 242px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/04/word-1.jpg" style="width: 322px; height: 242px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/15/word3.jpg" style="width: 322px; height: 242px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == board.length</code></li>
|
||||
<li><code>n = board[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 6</code></li>
|
||||
<li><code>1 <= word.length <= 15</code></li>
|
||||
<li><code>board</code> 和 <code>word</code> 仅由大小写英文字母组成</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>你可以使用搜索剪枝的技术来优化解决方案,使其在 <code>board</code> 更大的情况下可以更快解决问题?</p>
|
||||
<div><div>Related Topics</div><div><li>数组</li><li>回溯</li><li>矩阵</li></div></div>\n<div><li>👍 973</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user