1.4
This commit is contained in:
1 parent
c5025637ee
commit
ac5b43af95
4 files changed
+128
No files matched your search
@@ -0,0 +1,61 @@
|
|||||||
|
package com.hong.www;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
class A30_2 {
|
||||||
|
public List<Integer> findSubstring(String s, String[] words) {
|
||||||
|
List<Integer> res = new ArrayList<>();
|
||||||
|
if (s == null || s.length() == 0 || words == null || words.length == 0) return res;
|
||||||
|
|
||||||
|
int w = words[0].length();
|
||||||
|
int k = words.length;
|
||||||
|
int totalLen = w * k;
|
||||||
|
int n = s.length();
|
||||||
|
|
||||||
|
// 1. 统计目标单词
|
||||||
|
Map<String, Integer> need = new HashMap<>();
|
||||||
|
for (String word : words) {
|
||||||
|
need.put(word, need.getOrDefault(word, 0) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 核心:外层按偏移量分组 (0 到 w-1)
|
||||||
|
for (int start = 0; start < w; start++) {
|
||||||
|
int left = start;
|
||||||
|
int right = start;
|
||||||
|
int count = 0; // 满足的单词种类数
|
||||||
|
Map<String, Integer> window = new HashMap<>();
|
||||||
|
|
||||||
|
// 3. 窗口内每次移动 w 长度
|
||||||
|
while (right + w <= n) {
|
||||||
|
// 右边进一个单词
|
||||||
|
String word = s.substring(right, right + w);
|
||||||
|
right += w;
|
||||||
|
|
||||||
|
if (need.containsKey(word)) {
|
||||||
|
window.put(word, window.getOrDefault(word, 0) + 1);
|
||||||
|
if (window.get(word).equals(need.get(word))) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果窗口长度超过总长度,左边出一个单词
|
||||||
|
while (right - left > totalLen) {
|
||||||
|
String leftWord = s.substring(left, left + w);
|
||||||
|
left += w;
|
||||||
|
if (need.containsKey(leftWord)) {
|
||||||
|
if (window.get(leftWord).equals(need.get(leftWord))) {
|
||||||
|
count--;
|
||||||
|
}
|
||||||
|
window.put(leftWord, window.get(leftWord) - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 判断是否刚好凑齐
|
||||||
|
if (right - left == totalLen && count == need.size()) {
|
||||||
|
res.add(left);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.hong.www;
|
||||||
|
|
||||||
|
public class A3693 {
|
||||||
|
public int climbStairs(int n, int[] costs) {
|
||||||
|
if(n==0){
|
||||||
|
return 0;
|
||||||
|
}else if(n==1){
|
||||||
|
return costs[0]+1;
|
||||||
|
}else if(n==2){
|
||||||
|
return Math.min(4+costs[1], costs[1]+1+1+costs[0]);
|
||||||
|
}
|
||||||
|
int[] dp=new int[n+1];
|
||||||
|
dp[0]=0;
|
||||||
|
dp[1]=costs[0]+1;
|
||||||
|
dp[2]=Math.min(dp[0]+4+costs[1], dp[1]+costs[1]+1);
|
||||||
|
for(int i=3;i<=n;i++){
|
||||||
|
dp[i]=Math.min(dp[i-2]+4+costs[i-1], Math.min(dp[i-1]+costs[i-1]+1, dp[i-3]+9+costs[i-1]));
|
||||||
|
}
|
||||||
|
return dp[n];
|
||||||
|
}
|
||||||
|
public static void main(String[] args) {
|
||||||
|
A3693 a3693 = new A3693();
|
||||||
|
int n = 2;
|
||||||
|
int[] costs = {4, 6};
|
||||||
|
int result = a3693.climbStairs(n, costs);
|
||||||
|
System.out.println("The minimum cost to climb " + n + " stairs is: " + result);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.hong.www;
|
||||||
|
|
||||||
|
public class A70 {
|
||||||
|
public int climbStairs(int n) {
|
||||||
|
if (n == 1) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
int[] dp = new int[n + 1];
|
||||||
|
dp[0] = 1;
|
||||||
|
dp[1] = 1;
|
||||||
|
dp[2] = 2;
|
||||||
|
if (n >= 3) {
|
||||||
|
for (int i = 3; i <= n; i++) {
|
||||||
|
dp[i] = dp[i - 2] + dp[i - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[n];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.hong.www;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class LCR102 {
|
||||||
|
public int coinChange(int[] coins, int amount) {
|
||||||
|
int[] dp=new int[amount+1];
|
||||||
|
Arrays.fill(dp, Integer.MAX_VALUE-100);
|
||||||
|
dp[0]=0;
|
||||||
|
for(int x:coins){
|
||||||
|
for(int i=x;i<=amount;i++){
|
||||||
|
dp[i]=Math.min(dp[i], dp[i-x]+1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(dp[amount]>=Integer.MAX_VALUE-100){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return dp[amount];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user