Java算法 leetcode简单刷题记录10

23 篇文章 0 订阅

Java算法 leetcode简单刷题记录10

庆祝一下:大概花费了9天,我把所有leetcode Java 的简单题都刷完了,接下来开始冲刺中等和复杂;

简单题里用到的比较多的是字符串的处理,转换,拆分,替换,PriorityQueue 依次输出最大最小值;走楼梯等动态规划;

  • charAt
  • startsWith
  • toLowerCase()
  • replaceAll
  • split
  • indexOf
  • 进制转换: Integer.valueOf(“0xff”,16); 十六进制转十进制
  • Integer.valueOf(“0101”,2); 二进制转十进制
  • 十进制转二进制 Integer.toBinaryString(10)
  • split 特殊字符拆分需要加 “\”+“.”
  • char转int int a = (int)(‘9’-‘0’); 或者 int a = (int)(str.charAt(0)-‘0’);

PriorityQueue每次弹出队列里剩下的值中的最大值,最小值;

class Solution {
    public long pickGifts(int[] gifts, int k) {
        // 保证每次队列弹出最大值
        PriorityQueue<Integer> queue = new PriorityQueue<Integer>((obj1, obj2) -> {
            return obj2 - obj1;
        });

        // 每次弹出最小值
        /** PriorityQueue<Integer> queue = new PriorityQueue<Integer>((obj1, obj2) -> {
            return obj1 - obj2;
        });**/
        for (int gift : gifts) {
            queue.add(gift);
        }
        int i = 0;
        while (i < k && queue.size() > 0) {
            int q = queue.poll();
            queue.add((int) Math.sqrt(q));
            i++;
        }
        long sum = 0;
        while (queue.size() > 0) {
            sum += queue.poll();
        }
        return sum;
    }
}
  1. 一周中的第几天: https://leetcode.cn/problems/day-of-the-week/
    这是第2遍写这道题,稍微有些不一样,之前是告诉1970.1.1 是星期四,问 days后是哪个日期及星期几?
    这道题是已知1970.1.1 是星期四,问某个日期假如2023.11.20是星期几?
    俩道题知识点一致,需要计算1970.1.1到该日期经过了多少年,月,日,总天数,然后求星期。
class Solution {
    public String dayOfTheWeek(int day, int month, int year) {
        int[] months = new int[] { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        String[] weeks = new String[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
        int year0 = 1970;
        int week0 = 3;
        int month0 = 1;
        int sum = 0;
        while (year0 < year) {
            int days = 365;
            if (isRunYear(year0)) {
                days = 366;
            }
            System.out.println(year0 + " " + " " + days + " " + sum);
            sum += days;
            year0++;
        }

        for (int i = 1; i < month; i++) {
            int days = months[i];
            if (isRunYear(year0) && i == 2) {
                days = 29;
            }
            // System.out.println(year0 + " " + i + " " + days + " " + sum);
            sum += days;
        }
        // System.out.println(" days: " + day + " " + sum);
        sum += day - 1; // 当前天占一天,也不算进去;
        if (year > 1970) {
            sum -= 1; // 1970一整年中365天,起始日元旦算1天,所以要减掉;
        }
        // 假设法,假设距离只有3天,则为周四周五周六,否则mod 7求余;
        int n = sum > 3 ? (sum - 3) % 7 : sum + week0;
        // System.out.println(n);
        return weeks[n];
    }

    public boolean isRunYear(int year) {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return true;
        }
        return false;
    }
}
  1. 一年中的第几天: https://leetcode.cn/problems/day-of-the-year/
    判断闰月即可:整除4不被100整除,或者整除400

  2. 赎金信: https://leetcode.cn/problems/ransom-note/
    字符串处理,
    charAt
    replaceAll

  3. 按分隔符拆分字符串: https://leetcode.cn/problems/split-strings-by-separator/
    特殊字符,直接split会不起作用,需要用 \+"."等

class Solution {
    public List<String> splitWordsBySeparator(List<String> words, char separator) {
        List<String> res = new ArrayList<>();
        for (String str : words) {
            res.addAll(Arrays.asList(str.split("\\" + String.valueOf(separator))).stream().filter(x -> {
                return x.length() != 0;
            }).collect(Collectors.toList()));
        }
        return res;
    }
}
  1. 最长交替子数组: https://leetcode.cn/problems/longest-alternating-subarray/
    看着比较简单,还是需要把情况都考虑仔细,改改改才能通过所有的case…
class Solution {
    public int alternatingSubarray(int[] nums) {
        if (nums.length < 1) {
            return -1;
        }
        int res = -1;
        for (int i = 0; i < nums.length - 1; i++) {
            int cnt = -1;
            for (int j = i + 1; j < nums.length; j++) {
                if (cnt == -1 && nums[j - 1] + 1 == nums[j]) {
                    cnt = 2;
                    continue;
                }
                System.out.println(j + " " + nums[j - 1] + " " + nums[j]);
                if (nums[j - 1] + 1 == nums[j] || nums[j - 1] - 1 == nums[j]) {
                    if (j >= 2) {
                        if (nums[j] == nums[j - 2]) {
                            cnt++;
                        } else {
                            break;
                        }
                    }
                } else {
                    break;
                }
            }
            // System.out.println("----else " + j + " " + cnt);
            res = Math.max(res, cnt);
            cnt = -1;
        }
        return res;
    }
}
  1. 计算 K 置位下标对应元素的和: https://leetcode.cn/problems/sum-of-values-at-indices-with-k-set-bits/
    replaceAll & int转2进制值:Integer.toBinaryString();
  • 23
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序媛一枚~

您的鼓励是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值