博客记录-day144-力扣

一、力扣

1、统计好子数组的数目

2537. 统计好子数组的数目

java 复制代码
class Solution {
    public long countGood(int[] nums, int k) {
        long res=0;
        int count=0;
        int left=0;
        Map<Integer,Integer> map=new HashMap<>();
        for(var e:nums){
            count+=map.getOrDefault(e,0);
            map.merge(e,1,Integer::sum);
            while(count>=k){
                count=count-map.get(nums[left])+1;
                map.merge(nums[left],-1,Integer::sum);
                left++;
            }
            res+=left;
        }
        return res;
    }
}

2、指定日期的产品价格

1164. 指定日期的产品价格

sql 复制代码
select p1.product_id, ifnull(p2.new_price, 10) as price
from (
    select distinct product_id
    from products
) as p1 -- 所有的产品
left join (
    select product_id, new_price 
    from products
    where (product_id, change_date) in (
        select product_id, max(change_date)
        from products
        where change_date <= '2019-08-16'
        group by product_id
    )
) as p2 -- 在 2019-08-16 之前有过修改的产品和最新的价格
on p1.product_id = p2.product_id

3、连续出现的数字

180. 连续出现的数字

sql 复制代码
select distinct a.num ConsecutiveNums
from Logs a,Logs b,Logs c
where a.id=b.id-1 
    and b.id=c.id-1
    and a.num=b.num
    and b.num=c.num

4、二叉树中的最大路径和

124. 二叉树中的最大路径和

java 复制代码
class Solution {
    int res=0;
    public int maxPathSum(TreeNode root) {
        res=root.val;
        dfs(root);
        return res;
    }
    public int dfs(TreeNode root){
        if(root==null) return 0;
        int left=Math.max(dfs(root.left),0);
        int right=Math.max(dfs(root.right),0);
        res=Math.max(res,left+right+root.val);
        return Math.max(left,right)+root.val;
    }
}

5、字符串转换整数

8. 字符串转换整数 (atoi)

java 复制代码
public class Solution {
    public int myAtoi(String str) {
        char[] chars = str.toCharArray();
        int n = chars.length;
        int idx = 0;
        while (idx < n && chars[idx] == ' ') {
            // 去掉前导空格
            idx++;
        }
        if (idx == n) {
            //去掉前导空格以后到了末尾了
            return 0;
        }
        boolean negative = false;
        if (chars[idx] == '-') {
            //遇到负号
            negative = true;
            idx++;
        } else if (chars[idx] == '+') {
            // 遇到正号
            idx++;
        } else if (!Character.isDigit(chars[idx])) {
            // 其他符号
            return 0;
        }
        int ans = 0;
        while (idx < n && Character.isDigit(chars[idx])) {
            int digit = chars[idx] - '0';
            if (ans > (Integer.MAX_VALUE - digit) / 10) {
                // 本来应该是 ans * 10 + digit > Integer.MAX_VALUE
                // 但是 *10 和 + digit 都有可能越界,所有都移动到右边去就可以了。
                return negative? Integer.MIN_VALUE : Integer.MAX_VALUE;
            }
            ans = ans * 10 + digit;
            idx++;
        }
        return negative? -ans : ans;
    }
}

6、 最长有效括号

32. 最长有效括号

java 复制代码
public int longestValidParentheses(String s) {
    int maxans = 0;
    Stack<Integer> stack = new Stack<>();
    stack.push(-1);
    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == '(') {
            stack.push(i);
        } else {
            stack.pop();
            if (stack.empty()) {
                stack.push(i);
            } else {
                maxans = Math.max(maxans, i - stack.peek());
            }
        }
    }
    return maxans;
}

7、零钱兑换

322. 零钱兑换

java 复制代码
class Solution {
    public int coinChange(int[] coins, int amount) {
        int n=coins.length;
        int[][] dp=new int[n+1][amount+1];
        Arrays.fill(dp[0],Integer.MAX_VALUE/2);
        dp[0][0]=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<=amount;j++){
                if(coins[i]>j){
                    dp[i+1][j]=dp[i][j];                
                }else{
                    dp[i+1][j]=Math.min(dp[i][j],dp[i+1][j-coins[i]]+1);
                }
            }
        }
        return dp[n][amount]==Integer.MAX_VALUE/2?-1:dp[n][amount];
    }
}
相关推荐
XD7429716364 小时前
科技早报晚报|2026年5月7日:电子签署、团队知识库与可嵌入表格引擎,今天更值得动手的 3 个开源机会
科技·开源·github·开源项目·开发者工具
Rkgua9 小时前
初学者对与.gitignore应该有的了解
github
darkb1rd11 小时前
deepclaude:低成本指南,17 倍省调用费
开源·github·好物分享
摇滚侠11 小时前
如何打开 GitHub,GitHub 是基于 Git 版本控制系统的在线代码托管平台
git·github
DogDaoDao12 小时前
【GitHub】System Informer:Windows 平台最强开源系统监控与调试利器
windows·程序员·开源·github·开发工具·system informer
MY_TEUCK12 小时前
【Git 实习生小白专用】:最安全、永不翻车、公司最爱 的标准版本控制工作流程
git·安全·github
逛逛GitHub13 小时前
这款 DeepSeek V4 终端编程神器,在 GitHub 上火了。
github
冴羽yayujs14 小时前
GitHub 前端热榜项目 - 日榜(2026-05-07)
前端·github
java1234_小锋15 小时前
能让你的 AI 编程 Token 降低 60% 以上的开源神器:目前 GitHub 狂揽约 4.2 万星标
人工智能·github·ai编程
三维搬砖者16 小时前
挑战AI辅助从零构建3D模型编辑器:01基于Vue3 + Three.js的现代化架构设计
前端·vue.js·github