11 2022 档案
摘要:895. 最大频率栈 class FreqStack { Map<Integer, Stack<Integer>> stk; // k: 出现次数, v: 栈 Map<Integer, Integer> cnt; // k:数, v:该数出现的个数 int n; // 当前 个数的最大值 publi
阅读全文
摘要:1758. 生成交替二进制字符串的最少操作数 class Solution { public int minOperations(String s) { char[] c = s.toCharArray(); int n = c.length; int res1 = 0; int res2 = 0;
阅读全文
摘要:813. 最大平均值和的分组 题解: DP f[i][j] 表示:将[1,i]的序列分成 j 段的最大值 状态转移方程: f[i][j] = max(f[i][j], f[i][k] + (cnt[i]-cnt[k] / (i-k)) (cnt[i]-cnt[k] / (i-k)) 表示的是 最后一
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要:809. 情感丰富的文字 class Solution { public int expressiveWords(String s, String[] words) { int res = 0; // 空字符串 if (s.isEmpty()) { // 找words中有多少个空字符串 for (S
阅读全文
摘要:795. 区间子数组个数 题解: 实现一个函数cal 返回小于等于k的子数组数量 函数cal,可以用双指针实现 class Solution { public int numSubarrayBoundedMax(int[] nums, int left, int right) { return (i
阅读全文
摘要: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 =
阅读全文
摘要: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
阅读全文
摘要: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[
阅读全文
摘要:1732. 找到最高海拔 class Solution { public int largestAltitude(int[] gain) { int res = 0; int high = 0; for(int i = 0; i < gain.length; i ++) { high += gain
阅读全文
摘要:891. 子序列宽度之和 题解: 对于每个数a而言,其对res的贡献在于 a * a作为最大值的次数 - a * a作为最小值的次数 先将数组排序 a 作为最大值的次数: a的下标为i, 比a小的数有 0 ~ i-1 总共i个数,则 为 $$2^i$$ 个 a 作为最小值的次数: a的下标为i, 比
阅读全文
摘要:792. 匹配子序列的单词数 // 时间复杂度 n + m * k public int numMatchingSubseq(String s, String[] words) { List<List<Pair>> list = new ArrayList<>(); for (int i = 0;
阅读全文
摘要:775. 全局倒置与局部倒置 题解: 用归并排序求全局倒置(逆序对) 可以用树状数组求逆序对 class Solution { int num2 = 0; public boolean isIdealPermutation(int[] nums) { int n = nums.length; if
阅读全文
摘要:1710. 卡车上的最大单元数 class Solution { public int maximumUnits(int[][] boxTypes, int truckSize) { int n = boxTypes.length; Arrays.sort(boxTypes, Comparator.
阅读全文
摘要: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
阅读全文
摘要:790. 多米诺和托米诺平铺 题解: dp num数组表示的是:i-1列的瓷砖都被铺满了,第i列的状态枚举 第i列的状态枚举有4种: 11 表示 上下两行都被填充, 10 表示上面那行被填充, 01 表示下面那行被填充, 00 表示两行都没有被填充 转移方程: num[i+1][k] = num[i
阅读全文
摘要:34. 在排序数组中查找元素的第一个和最后一个位置 class Solution { public int[] searchRange(int[] nums, int target) { if (nums.length == 0) return new int[]{-1, -1}; // 搜左边界
阅读全文
摘要: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) >
阅读全文
摘要:1704. 判断字符串的两半是否相似 class Solution { public boolean halvesAreAlike(String s) { Set<Character> set = new HashSet<>(); set.add('a'); set.add('e'); set.ad
阅读全文
摘要:官方文档 spark sql 函数 表结构相关 显示该表的建表语句 SHOW CREATE TABLE `database`.`tableName` 显示该表的列名、列的类型、列的注释 DESC `database`.`tableName` 显示该表的列名 SHOW COLUMNS FROM `da
阅读全文
摘要:864. 获取所有钥匙的最短路径 题解: bfs 总共不超过6把钥匙,通过位运算保存钥匙 class Solution { public int shortestPathAllKeys(String[] grid) { int[][][] dist = new int[31][31][64]; in
阅读全文
摘要:764. 最大加号标志 题解: 枚举二维数组每个位置,向上、下、左、右四个方向能延伸的最长长度 取这四个方向的最小值,即为答案 可以用f[i] = f[i - 1] + 1 计算四个方向的最大值,然后参考滚动数组思想,用一个变量记录即可 class Solution { public int ord
阅读全文
摘要:1684. 统计一致字符串的数目 class Solution { public int countConsistentStrings(String allowed, String[] words) { int[] flag = new int[30]; char[] all = allowed.t
阅读全文
摘要:816. 模糊坐标 题解: 暴力枚举 两个点 判断小数点前 是否 有前导零, 判断小数点后 是否 有后置0 class Solution { public List<String> ambiguousCoordinates(String s) { s = s.substring(1, s.lengt
阅读全文
摘要:33. 搜索旋转排序数组 题解: 二分找出翻转的位置 然后判断在target在前后哪个区间,二分找到target的位置 class Solution { public int search(int[] nums, int target) { int n = nums.length; if (n ==
阅读全文
摘要:32. 最长有效括号 题解: 合法子串中num('(') > num(')') 左右括号的数量相等 合法序列的前缀 num('(') >= num(')') 左括号的序列要大于右括号的序列 如果出现 num('(') < num(')') 则说明这个右括号开始是不合法的,可以从该右括号为起点,重新遍
阅读全文
摘要:31. 下一个排列 题解: 从后往前找第一个降序的点的下标 a 从该点x出发 往后找 第一个比该点大的数,下标为 b 两个数交换,然后将 a 后面的数 翻转过来 public void nextPermutation(int[] nums) { int k = nums.length - 1; wh
阅读全文
摘要:1678. 设计 Goal 解析器 class Solution { public String interpret(String command) { char[] ch = command.toCharArray(); int n = ch.length; StringBuilder sb =
阅读全文
摘要:30. 串联所有单词的子串 题解: 题目可以转换为:把字符串s按word的长度划分为一堆集合后,然后这一堆集合中,找出完全由words集合组成的字符串。 滑动窗口,每次滑动按一个word的长度滑,当窗口内的元素完全等于words的元素时,此时的下标即为答案。 class Solution { pub
阅读全文
摘要:29. 两数相除 题解: a / b = y 等价于 b > a - (b * y) > 0 , 只要求出减了多少次b就好了。 一个一个地减b,效率太低了,最坏情况下, a = 2^31 - 1 , b = 1 ,超时; 应该先预处理出 b * 2^k (最多不超过31个), 然后从大到小被 a减,
阅读全文
摘要:1106. 解析布尔表达式 题解:每个小括号都可以优先递归计算,dfs class Solution { int index; char[] ch; public boolean parseBoolExpr(String expression) { ch = expression.toCharArr
阅读全文
摘要:754. 到达终点数字 class Solution { public int reachNumber(int target) { target = Math.abs(target); int n = 0, sum = 0; while(sum < target || (sum - target)
阅读全文
摘要:1668. 最大重复子字符串 方法一:暴力枚举 class Solution { public int maxRepeating(String sequence, String word) { char[] ch1 = sequence.toCharArray(); int res = 0; Str
阅读全文
摘要:1620. 网络信号最好的坐标 题解:数据范围小,直接暴力枚举所有点,然后计算贡献,取最大值即可 class Solution { public int[] bestCoordinate(int[][] towers, int radius) { int n = towers.length; int
阅读全文
摘要:检查两个字符串数组是否相等 class Solution { public boolean arrayStringsAreEqual(String[] word1, String[] word2) { StringBuilder w1 = new StringBuilder(); for (Stri
阅读全文