随笔分类 -  leetCode

摘要:1752. 检查数组是否经排序和轮转得到 class Solution { public boolean check(int[] nums) { int n = nums.length; int x = 0; for(int i = 0 ; i < n - 1 ; i ++ ){ if(nums[i 阅读全文
posted @ 2022-11-27 16:04 Eiffelzero 阅读(14) 评论(0) 推荐(0) 编辑
摘要:882. 细分图中的可到达节点 class Solution { int N = 3010, M = 20010, INF = 0x3f3f3f3f; int[] h = new int[N]; int[] e = new int[M]; int[] w = new int[M]; int[] ne 阅读全文
posted @ 2022-11-26 15:57 Eiffelzero 阅读(19) 评论(0) 推荐(0) 编辑
摘要:809. 情感丰富的文字 class Solution { public int expressiveWords(String s, String[] words) { int res = 0; // 空字符串 if (s.isEmpty()) { // 找words中有多少个空字符串 for (S 阅读全文
posted @ 2022-11-25 22:34 Eiffelzero 阅读(13) 评论(0) 推荐(0) 编辑
摘要:795. 区间子数组个数 题解: 实现一个函数cal 返回小于等于k的子数组数量 函数cal,可以用双指针实现 class Solution { public int numSubarrayBoundedMax(int[] nums, int left, int right) { return (i 阅读全文
posted @ 2022-11-24 19:06 Eiffelzero 阅读(15) 评论(0) 推荐(0) 编辑
摘要:1742. 盒子中小球的最大数量 class Solution { public int countBalls(int l, int r) { int[] box = new int[50]; int max = 0; for (int i = l; i <= r; i++) { int num = 阅读全文
posted @ 2022-11-23 19:30 Eiffelzero 阅读(22) 评论(0) 推荐(0) 编辑
摘要:878. 第 N 个神奇数字 题解:二分 + gcd 1~n中,能被a整除的个数 n/a , 能被b整除的个数 n/b, 能被a 且 b 整除的个数 n/ gcd(a,b) 则 1~n中能被a或b整除的个数为 n/a + n/b - n/gcd(a,b) 从1~n中二分,搜这个数k 1~k中 k/a 阅读全文
posted @ 2022-11-22 18:59 Eiffelzero 阅读(19) 评论(0) 推荐(0) 编辑
摘要:808. 分汤 题解: 先对n进行优化,除以25 上取整 分析A和B汤 | A | B | 概率 | | | | | | <=0 | <=0 | 0.5 | | >0 | <=0 | 0 | | <=0 | >0 | 1 | | >0 | >0 | f[i][j] = (f[i-4][j] + f[ 阅读全文
posted @ 2022-11-21 23:00 Eiffelzero 阅读(29) 评论(0) 推荐(0) 编辑
摘要:1732. 找到最高海拔 class Solution { public int largestAltitude(int[] gain) { int res = 0; int high = 0; for(int i = 0; i < gain.length; i ++) { high += gain 阅读全文
posted @ 2022-11-19 16:49 Eiffelzero 阅读(20) 评论(0) 推荐(0) 编辑
摘要:891. 子序列宽度之和 题解: 对于每个数a而言,其对res的贡献在于 a * a作为最大值的次数 - a * a作为最小值的次数 先将数组排序 a 作为最大值的次数: a的下标为i, 比a小的数有 0 ~ i-1 总共i个数,则 为 $$2^i$$ 个 a 作为最小值的次数: a的下标为i, 比 阅读全文
posted @ 2022-11-18 18:46 Eiffelzero 阅读(11) 评论(0) 推荐(0) 编辑
摘要:792. 匹配子序列的单词数 // 时间复杂度 n + m * k public int numMatchingSubseq(String s, String[] words) { List<List<Pair>> list = new ArrayList<>(); for (int i = 0; 阅读全文
posted @ 2022-11-17 23:39 Eiffelzero 阅读(20) 评论(0) 推荐(0) 编辑
摘要:775. 全局倒置与局部倒置 题解: 用归并排序求全局倒置(逆序对) 可以用树状数组求逆序对 class Solution { int num2 = 0; public boolean isIdealPermutation(int[] nums) { int n = nums.length; if 阅读全文
posted @ 2022-11-16 17:44 Eiffelzero 阅读(9) 评论(0) 推荐(0) 编辑
摘要:1710. 卡车上的最大单元数 class Solution { public int maximumUnits(int[][] boxTypes, int truckSize) { int n = boxTypes.length; Arrays.sort(boxTypes, Comparator. 阅读全文
posted @ 2022-11-15 19:32 Eiffelzero 阅读(15) 评论(0) 推荐(0) 编辑
摘要:791. 自定义字符串排序 class Solution { int[] w = new int[30]; public String customSortString(String order, String s) { for (int i =0 ; i < 26;i ++) { w[i] = 3 阅读全文
posted @ 2022-11-13 17:55 Eiffelzero 阅读(21) 评论(0) 推荐(0) 编辑
摘要:790. 多米诺和托米诺平铺 题解: dp num数组表示的是:i-1列的瓷砖都被铺满了,第i列的状态枚举 第i列的状态枚举有4种: 11 表示 上下两行都被填充, 10 表示上面那行被填充, 01 表示下面那行被填充, 00 表示两行都没有被填充 转移方程: num[i+1][k] = num[i 阅读全文
posted @ 2022-11-12 14:14 Eiffelzero 阅读(32) 评论(0) 推荐(0) 编辑
摘要:34. 在排序数组中查找元素的第一个和最后一个位置 class Solution { public int[] searchRange(int[] nums, int target) { if (nums.length == 0) return new int[]{-1, -1}; // 搜左边界 阅读全文
posted @ 2022-11-11 14:21 Eiffelzero 阅读(14) 评论(0) 推荐(0) 编辑
摘要:35. 搜索插入位置 class Solution { public int searchInsert(int[] nums, int target) { int l = 0, r = nums.length - 1; while (l < r) { int mid = l + ((r - l) > 阅读全文
posted @ 2022-11-11 13:03 Eiffelzero 阅读(12) 评论(0) 推荐(0) 编辑
摘要:1704. 判断字符串的两半是否相似 class Solution { public boolean halvesAreAlike(String s) { Set<Character> set = new HashSet<>(); set.add('a'); set.add('e'); set.ad 阅读全文
posted @ 2022-11-11 11:04 Eiffelzero 阅读(13) 评论(0) 推荐(0) 编辑
摘要:864. 获取所有钥匙的最短路径 题解: bfs 总共不超过6把钥匙,通过位运算保存钥匙 class Solution { public int shortestPathAllKeys(String[] grid) { int[][][] dist = new int[31][31][64]; in 阅读全文
posted @ 2022-11-10 16:33 Eiffelzero 阅读(15) 评论(0) 推荐(0) 编辑
摘要:764. 最大加号标志 题解: 枚举二维数组每个位置,向上、下、左、右四个方向能延伸的最长长度 取这四个方向的最小值,即为答案 可以用f[i] = f[i - 1] + 1 计算四个方向的最大值,然后参考滚动数组思想,用一个变量记录即可 class Solution { public int ord 阅读全文
posted @ 2022-11-10 00:00 Eiffelzero 阅读(20) 评论(0) 推荐(0) 编辑
摘要:1684. 统计一致字符串的数目 class Solution { public int countConsistentStrings(String allowed, String[] words) { int[] flag = new int[30]; char[] all = allowed.t 阅读全文
posted @ 2022-11-08 22:25 Eiffelzero 阅读(12) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示