This commit is contained in:
whai 2024-03-27 21:52:49 +08:00
parent 9d0ddcf668
commit bdd083a912
5 changed files with 313 additions and 12 deletions

View File

@ -2,15 +2,47 @@
<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
- [Algorithm](#algorithm) - [Algorithm](./src/main/java/cn/whaifree)
- [Array](#array) - [LeetCode](./src/main/java/cn/whaifree/leetCode)
- [String](#string) - [Array 链表](./src/main/java/cn/whaifree/leetCode/Array)
- [Math](#math) - [BackTracking 回溯法](./src/main/java/cn/whaifree/leetCode/Array/BackTracking)
- [Tree](#tree) - [Dynamic 动态规划](./src/main/java/cn/whaifree/leetCode/Array/Dynamic)
- [Backtracking](#backtracking) - [Greedy 贪心算法](./src/main/java/cn/whaifree/leetCode/Array/Greedy)
- [Bit Manipulation](#bit-manipulation) - [Hash 哈希](./src/main/java/cn/whaifree/leetCode/Array/Hash)
- [Union Find](#union-find) - [Other 其他 ](./src/main/java/cn/whaifree/leetCode/Array/LeetCode)
- [Heap](#heap) - [LinkedList 链表](./src/main/java/cn/whaifree/leetCode/Array/LinkedList)
- [Greedy](#greedy) - [model 模型类](./src/main/java/cn/whaifree/leetCode/Array/model)
- [Sort](#sort) - [Stack 栈](./src/main/java/cn/whaifree/leetCode/Array/Stack)
- [Divide and Conquer]() - [String 字符串](./src/main/java/cn/whaifree/leetCode/Array/String)
- [Tree 树](./src/main/java/cn/whaifree/leetCode/Array/Tree)
- [utils 工具](./src/main/java/cn/whaifree/leetCode/Array/utils)
- [Offer](./src/main/java/cn/whaifree/leetCode)
- [redo](./src/main/java/cn/whaifree/redo)
# 自己瞎写的公众号与博客
- 👋 Hi, Im @whaibetter
- 👀 I am currently studying the postgraduate class of *Computer Technology* in Zhejiang university of technology(ZJUT) of China
- I studied the undergraduate class of ***Data science and Big data technology\* in Zhejiang university of science and technology(ZUST)** of China
- 🌴 Im currently learning java and big data
- 💕
- 🐦 How to reach me:
- wechat:***whaihalo***
- ✉️ email: ***whaifree@163.com***
------
- 👋嗨,我是@whaibetter
- 👁 我目前就为在 浙江工业大学 就读*计算机技术* 专业硕士研究生
- 我本科毕业于**浙江科技学院\*数据科学与大数据技术专业\***
- 🌴 目前正在学习java和大数据
- 💕 联系我?
- 微信:***whaihalo***
- ✉️:邮箱:***whaifree@163.com***
<a href= "http://www.whaifree.top">whai的个人博客 whaifree.top 欢迎留言!</a>
<a href="whaifree.top"><img src="http://42.192.130.83:9000/picgo/imgs/image-20230830165044149.png" style="zoom:67%;" /></a>

View File

@ -0,0 +1,56 @@
package cn.whaifree.leetCode.Dynamic;
import org.junit.Test;
import java.util.Scanner;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/27 12:45
* @注释
*/
public class KaMa47 {
@Test
public void test()
{
int i = new Solution().plt(3, 2);
System.out.println(i);
}
/* https://kamacoder.com/problempage.php?pid=1067
*/
class Solution{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int capacity = scanner.nextInt();
int m = scanner.nextInt();
System.out.println(plt(capacity, m));
}
/**
* @param capacity 需要n阶
* @param m 每一步可以走几个
* @return
*/
public static int plt(int capacity, int m) {
// 排列
int[] dp = new int[capacity + 1];
dp[0] = 1;
for (int j = 0; j <= capacity; j++) {
for (int i = 1; i <= m; i++) {
if (j >= i) {
dp[j] += dp[j - i];
}
}
}
return dp[capacity];
}
}
}

View File

@ -0,0 +1,47 @@
package cn.whaifree.leetCode.Dynamic;
import org.junit.Test;
import java.util.Arrays;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/27 12:58
* @注释
*/
public class LeetCode322 {
@Test
public void test() {
int[] coins = {1,2,5};
int amount = 11;
int i = new Solution().coinChange(coins, amount);
System.out.println(i);
}
class Solution {
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0; // 总和为0的方案为0
for (int i = 0; i < coins.length; i++) {
for (int j = coins[i]; j <= amount ; j++) {
//只有dp[j-coins[i]]不是初始最大值时该位才有选择的必要
if (dp[j - coins[i]] != Integer.MAX_VALUE) {
//选择硬币数目最小的情况
// 不放 加上本次方案1
dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1);
}
}
}
/**
* 输入coins = [2], amount = 3
* 输出-1
*/
return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
}
}
}

View File

@ -0,0 +1,101 @@
package cn.whaifree.leetCode.Dynamic;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/27 11:46
* @注释
*/
public class LeetCode377 {
@Test
public void test() {
int[] nums = {1, 2, 3};
int target = 4;
int res = new Solution1().combinationSum4(nums, target);
System.out.println(res);
}
/**
* 超时
*/
class Solution {
int sum = 0;
int res = 0;
public int combinationSum4(int[] nums, int target) {
backTracking(nums, target);
return res;
}
public void backTracking(int[] nums, int target) {
if (sum == target) {
res++;
return;
}
if (sum > target) {
return;
}
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
backTracking(nums, target);
sum -= nums[i];
}
}
}
/**
* dp 完全背包
*/
class Solution1 {
/**
* 1 2 3随意取使得总包围target
* @param nums
* @param target
* @return
*/
public int combinationSum4(int[] nums, int target) {
// dp[j] 表示 从0-i-1任取使得满足包容量为j的可能性
/**
* 0 1 2 3 4
* [1, 1, 0, 0, 0]
* [1, 1, 2, 0, 0]
* [1, 1, 2, 4, 0]
* [1, 1, 2, 4, 7]
*/
int[] dp = new int[target + 1];
dp[0] = 1; // 都不放入 情况有一种
for (int j = 1; j < target + 1; j++) {
for (int i = 0; i < nums.length; i++) {
if (j >= nums[i]) {
dp[j] = dp[j] + dp[j - nums[i]];
// dp[j] 不放
// dp[j - nums[i]] 放也有多种情况
}
}
/**
* 如果把遍历nums物品放在外循环
* 遍历target的作为内循环的话举一个例子
* 计算dp[4]的时候结果集只有 {1,3} 这样的集合忽略了{3,1}
*/
}
/**
* 如果求 组合数 就是外层for循环遍历物品内层for遍历背包
* 如果求 排列数 就是外层for遍历背包内层for循环遍历物品
*/
return dp[target];
}
}
}

View File

@ -0,0 +1,65 @@
//package cn.whaifree.test;
//
//import java.io.File;
//import java.io.FileWriter;
//import java.io.IOException;
//import java.util.Arrays;
//import java.util.List;
//import java.util.stream.Collectors;
//
///**
// * @version 1.0
// * @Author whai文海
// * @Date 2024/3/26 16:52
// * @注释
// */
//public class DirectoryToMarkdown {
// public static void main(String[] args) {
// // 指定你要读取的文件夹路径
// String directoryPath = "D:\\project\\LeetCode\\src";
//
// // 生成Markdown文件的内容
// String markdownContent = generateMarkdownContent(directoryPath);
//
//
// System.out.println(markdownContent);
// // 将Markdown内容写入文件
// File markdownFile = new File("D:\\project\\LeetCode\\目录.md");
// try (FileWriter writer = new FileWriter(markdownFile)) {
// writer.write(markdownContent);
// System.out.println("Markdown文件已生成: " + markdownFile.getAbsolutePath());
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
//
// private static String generateMarkdownContent(String directoryPath) {
// File directory = new File(directoryPath);
// if (!directory.exists() || !directory.isDirectory()) {
// return "指定的路径不存在或不是一个文件夹";
// }
//
// // 获取文件夹中的所有文件和子文件夹
// File[] files = directory.listFiles();
// if (files == null) {
// return "文件夹为空";
// }
//
// // 使用流来构建Markdown列表
// List<String> links = Arrays.stream(files)
// .map(file -> {
// String fileName = file.getName();
// String filePath = directoryPath + "/" + fileName;
// // 转换为Markdown格式的链接
// return "- [" + fileName + "](#" + fileName.replace(" ", "-").toLowerCase() + ")\n";
// })
// .collect(Collectors.toList());
//
// // 构建完整的Markdown内容
// StringBuilder markdownBuilder = new StringBuilder();
// markdownBuilder.append("# 目录\n\n");
// links.forEach(markdownBuilder::append);
//
// return markdownBuilder.toString();
// }
//}