目录

1.leetcode301删除无效的括号

2.leetcode45跳跃游戏 II

3.leetcode517超级洗衣机

4.leetcode407接雨水 II

5.面试题 08.14. 布尔运算

6.剑指 Offer 62. 圆圈中最后剩下的数字

下一阶段

7.leetcode7整数反转

8.leetcode9回文数

9.leetcode14最长公共前缀

10.leetcode16最接近的三数之和


1.leetcode301删除无效的括号

class Solution {// 来自leetcode投票第一的答案,实现非常好,我们来赏析一下public static List<String> removeInvalidParentheses(String s) {List<String> ans = new ArrayList<>();remove(s, ans, 0, 0, new char[] { '(', ')' });return ans;}// modifyIndex <= checkIndex// 只查s[checkIndex....]的部分,因为之前的一定已经调整对了// 但是之前的部分是怎么调整对的,调整到了哪?就是modifyIndex// 比如:// ( ( ) ( ) ) ) ...// 0 1 2 3 4 5 6// 一开始当然checkIndex = 0,modifyIndex = 0// 当查到6的时候,发现不对了,// 然后可以去掉2位置、4位置的 ),都可以// 如果去掉2位置的 ), 那么下一步就是// ( ( ( ) ) ) ...// 0 1 2 3 4 5 6// checkIndex = 6 ,modifyIndex = 2// 如果去掉4位置的 ), 那么下一步就是// ( ( ) ( ) ) ...// 0 1 2 3 4 5 6// checkIndex = 6 ,modifyIndex = 4// 也就是说,// checkIndex和modifyIndex,分别表示查的开始 和 调的开始,之前的都不用管了  par  (  )public static void remove(String s, List<String> ans, int checkIndex, int deleteIndex, char[] par) {for (int count = 0, i = checkIndex; i < s.length(); i++) {if (s.charAt(i) == par[0]) {count++;}if (s.charAt(i) == par[1]) {count--;}// i check计数<0的第一个位置if (count < 0) {for (int j = deleteIndex; j <= i; ++j) {// 比如if (s.charAt(j) == par[1] && (j == deleteIndex || s.charAt(j - 1) != par[1])) {remove(s.substring(0, j) + s.substring(j + 1, s.length()),ans, i, j, par);}}return;  //删除第一个不合法的。 直接return}}String reversed = new StringBuilder(s).reverse().toString();if (par[0] == '(') {remove(reversed, ans, 0, 0, new char[] { ')', '(' });} else {ans.add(reversed);}}
}

2.leetcode45跳跃游戏 II

/**O(N)O(1)*/
class Solution {public static int jump(int[] arr) {if (arr == null || arr.length == 0) {return 0;}int step = 0;int cur = 0;int next = 0;for (int i = 0; i < arr.length; i++) {if (cur < i) {step++;cur = next;}next = Math.max(next, i + arr[i]);}return step;}
}

3.leetcode517超级洗衣机

class Solution {public  int findMinMoves(int[] arr) {if (arr == null || arr.length == 0) {return 0;}int size = arr.length;int sum = 0;for (int i = 0; i < size; i++) {sum += arr[i];}if (sum % size != 0) {return -1;}int avg = sum / size;int leftSum = 0;int ans = 0;for (int i = 0; i < arr.length; i++) {int leftRest = leftSum - i * avg;int rightRest = (sum - leftSum - arr[i]) - (size - i - 1) * avg;if (leftRest < 0 && rightRest < 0) {ans = Math.max(ans, Math.abs(leftRest) + Math.abs(rightRest));} else {ans = Math.max(ans, Math.max(Math.abs(leftRest), Math.abs(rightRest)));}leftSum += arr[i];}return ans;}
}

4.leetcode407接雨水 II

class Solution {public static class Node {public int value;public int row;public int col;public Node(int v, int r, int c) {value = v;row = r;col = c;}}public static int trapRainWater(int[][] heightMap) {if (heightMap == null || heightMap.length == 0 || heightMap[0] == null || heightMap[0].length == 0) {return 0;}int N = heightMap.length;int M = heightMap[0].length;boolean[][] isEnter = new boolean[N][M];PriorityQueue<Node> heap = new PriorityQueue<>((a, b) -> a.value - b.value);for (int col = 0; col < M - 1; col++) {isEnter[0][col] = true;heap.add(new Node(heightMap[0][col], 0, col));}for (int row = 0; row < N - 1; row++) {isEnter[row][M - 1] = true;heap.add(new Node(heightMap[row][M - 1], row, M - 1));}for (int col = M - 1; col > 0; col--) {isEnter[N - 1][col] = true;heap.add(new Node(heightMap[N - 1][col], N - 1, col));}for (int row = N - 1; row > 0; row--) {isEnter[row][0] = true;heap.add(new Node(heightMap[row][0], row, 0));}int water = 0;int max = 0;while (!heap.isEmpty()) {Node cur = heap.poll();max = Math.max(max, cur.value);int r = cur.row;int c = cur.col;//   上if (r > 0 && !isEnter[r - 1][c]) {water += Math.max(0, max - heightMap[r - 1][c]);isEnter[r - 1][c] = true;heap.add(new Node(heightMap[r - 1][c], r - 1, c));}//  下if (r < N - 1 && !isEnter[r + 1][c]) {water += Math.max(0, max - heightMap[r + 1][c]);isEnter[r + 1][c] = true;heap.add(new Node(heightMap[r + 1][c], r + 1, c));}// 左if (c > 0 && !isEnter[r][c - 1]) {water += Math.max(0, max - heightMap[r][c - 1]);isEnter[r][c - 1] = true;heap.add(new Node(heightMap[r][c - 1], r, c - 1));}// 右if (c < M - 1 && !isEnter[r][c + 1]) {water += Math.max(0, max - heightMap[r][c + 1]);isEnter[r][c + 1] = true;heap.add(new Node(heightMap[r][c + 1], r, c + 1));}}return water;}
}

5.面试题 08.14. 布尔运算


class Solution {public  int countEval(String express, int desired) {if (express == null || express.equals("")) {return 0;}char[] exp = express.toCharArray();int N = exp.length;Info[][] dp = new Info[N][N];Info allInfo = func(exp, 0, exp.length - 1, dp);return desired == 1 ? allInfo.t : allInfo.f;}public  class Info {public int t;   //为true的方法数public int f;   //为false的方法数public Info(int tr, int fa) {t = tr;f = fa;}}// 限制:// L...R上,一定有奇数个字符// L位置的字符和R位置的字符,非0即1,不能是逻辑符号!// 返回str[L...R]这一段,为true的方法数,和false的方法数public  Info func(char[] str, int L, int R, Info[][] dp) {if (dp[L][R] != null) {return dp[L][R];}int t = 0;int f = 0;if (L == R) {t = str[L] == '1' ? 1 : 0;f = str[L] == '0' ? 1 : 0;} else { // L..R >=3// 每一个种逻辑符号,split枚举的东西// 都去试试最后结合for (int split = L + 1; split < R; split += 2) {Info leftInfo = func(str, L, split - 1, dp);Info rightInfo = func(str, split + 1, R, dp);int a = leftInfo.t;int b = leftInfo.f;int c = rightInfo.t;int d = rightInfo.f;switch (str[split]) {case '&':t += a * c;f += b * c + b * d + a * d;break;case '|':t += a * c + a * d + b * c;f += b * d;break;case '^':t += a * d + b * c;f += a * c + b * d;break;}}}dp[L][R] = new Info(t, f);return dp[L][R];}
}

6.剑指 Offer 62. 圆圈中最后剩下的数字

/**公式:  前 = (后 + m - 1) % i + 1
*/// 提交直接通过// 给定的编号是0~n-1的情况下,数到m就杀// 返回谁会活?public int lastRemaining1(int n, int m) {return getLive(n, m) - 1;}// 课上题目的设定是,给定的编号是1~n的情况下,数到m就杀// 返回谁会活?public static int getLive(int n, int m) {if (n == 1) {return 1;}//公式:前 = (后 + m - 1) % i + 1 ;return (getLive(n - 1, m) + m - 1) % n + 1;}class Solution {public int lastRemaining(int n, int m) {int ans = 1;int r = 1;while (r <= n) {ans = (ans + m - 1) % (r++) + 1;}return ans - 1;}
}

下一阶段

7.leetcode7整数反转

/**时间复杂度O(log|x|)空间复杂度O(1)//为什么要除以10  // 2147483647     2的31次方-1// 2147483650   此时溢出// 那么我们可以直接从 最大数的1/10处,开始判断*/
class Solution {public int reverse(int x) {int rev = 0;while (x != 0) {if (rev < Integer.MIN_VALUE / 10 || rev > Integer.MAX_VALUE / 10) {return 0;}int digit = x % 10;x /= 10;rev = rev * 10 + digit;}return rev;}
}

8.leetcode9回文数

/**时间复杂度O(log n)空间复杂度O(1)*/
class Solution {public boolean isPalindrome(int x) {// 特殊情况:// 如上所述,当 x < 0 时,x 不是回文数。// 同样地,如果数字的最后一位是 0,为了使该数字为回文,// 则其第一位数字也应该是 0// 只有 0 满足这一属性if (x < 0 || (x % 10 == 0 && x != 0)) {return false;}int revertedNumber = 0;while (x > revertedNumber) {revertedNumber = revertedNumber * 10 + x % 10;x /= 10;}// 当数字长度为奇数时,我们可以通过 revertedNumber/10 去除处于中位的数字。// 例如,当输入为 12321 时,在 while 循环的末尾我们可以得到 x = 12,revertedNumber = 123,// 由于处于中位的数字不影响回文(它总是与自己相等),所以我们可以简单地将其去除。return x == revertedNumber || x == revertedNumber / 10;}
}

9.leetcode14最长公共前缀

/**纵向扫描时间复杂度O(m n)空间复杂度O( 1 )*/
class Solution {public String longestCommonPrefix(String[] strs) {if (strs == null || strs.length == 0) {return "";}int length = strs[0].length(); //第一个字符串的长度int count = strs.length; //字符串的个数for (int i = 0; i < length; i++) {  //第一个字符串的长度char c = strs[0].charAt(i);   //第一个字符for (int j = 1; j < count; j++) {  // 开始后面字符串的比对if (i == strs[j].length() || strs[j].charAt(i) != c) {return strs[0].substring(0, i);}}}return strs[0];}
}

10.leetcode16最接近的三数之和

/**时间复杂度O( N的平方 )空间复杂度O( log N )*/
class Solution {public int threeSumClosest(int[] nums, int target) {Arrays.sort(nums);int n = nums.length;int best = 10000000;// 枚举 afor (int i = 0; i < n; ++i) {// 保证和上一次枚举的元素不相等if (i > 0 && nums[i] == nums[i - 1]) {continue;}// 使用双指针枚举 b 和 cint j = i + 1, k = n - 1;while (j < k) {int sum = nums[i] + nums[j] + nums[k];// 如果和为 target 直接返回答案if (sum == target) {return target;}// 根据差值的绝对值来更新答案if (Math.abs(sum - target) < Math.abs(best - target)) {best = sum;}if (sum > target) {// 如果和大于 target,移动 c 对应的指针int k0 = k - 1;// 移动到下一个不相等的元素while (j < k0 && nums[k0] == nums[k]) {--k0;}k = k0;} else {// 如果和小于 target,移动 b 对应的指针int j0 = j + 1;// 移动到下一个不相等的元素while (j0 < k && nums[j0] == nums[j]) {++j0;}j = j0;}}}return best;}
}

leetcode hot 100(刷题篇9)(301/45/517/407/offer62/MST08.14/7/9/14/16)相关推荐

  1. 【嵌入式入门篇】嵌入式0基础沉浸式刷题篇1

    嵌入式-入门沉浸式刷题篇 前言 Q1:嵌入式牛牛宏大小 Q2:嵌入式宏空间坐标数量 Q3:嵌入式牛牛疑惑的变量 Q4:嵌入式关键字const Q5:嵌入式机器的大小端 Q6:嵌入式不用排序找数字 Q7 ...

  2. 【Verilog刷题篇】硬件工程师从0到入门2|组合逻辑

    Verilog从0到入门2-组合逻辑 前言 Q1:4位数值比较器电路 Q2:4bit超前进位加法器电路 Q3:优先编码器电路① Q4:用优先编码器①实现键盘编码电路 Q5:优先编码器Ⅰ Q6:使用8线 ...

  3. 力扣(LeetCode)怎么刷题,以排序算法为例

    掌握 LeetCode 刷题方法再开始刷题,属于磨刀不误砍柴工.掌握正确方法是非常重要的. 如果你在刷题的时候发现怎么也写不出来,别担心,这是正常的.如果你还发现,之前明明刷过的题,过段时间再做的时候 ...

  4. 2022-12-16 leetcode与蓝桥刷题情况

    一.leetcode题目 1.构成特定和需要添加的最少元素 题目描述 给你一个整数数组 nums ,和两个整数 limit 与 goal .数组 nums 有一条重要属性:abs(nums[i]) & ...

  5. 【Python刷题篇】Python从0到入门3|循环、条件复习、元组入门、字典入门

    Python从0到入门3目录 前言 Q1:团队分组 Q2:禁止重复注册 Q3:元组-牛客运动会 Q4:字典-遍历字典 Q5:字典-毕业生就业调查 Q6:姓名与学号 总结 前言 - 本期是Python从 ...

  6. 【Python刷题篇】Python从0到入门4|字典复习、函数入门、综合实践

    Python从0到入门3目录 前言 Q1:字典复习-首都 Q2:函数入门-求差 Q3:函数入门-牛牛的朋友 Q4:综合案例:自动售卖饮料机 Q5:综合案例-被8整除的数字 Q6:综合案例-披萨店的订单 ...

  7. 【Go语言刷题篇】Go完结篇|函数、结构体、接口、错误入门学习

    Go从0到入门6-Go完结篇 前言 Q1:函数-数字的阶乘 Q2:函数-绝对值 Q3:函数-加减乘除 Q4:结构体-学生信息1 Q5:结构体-学生信息2 Q6:接口-动物和老虎 Q7:错误-网络延迟 ...

  8. LeetCode按知识点刷题,额外附带题解

    刷题顺序转自:ACM金牌选手整理的[LeetCode刷题顺序]_清澈不在远方-CSDN博客 题解为本人自刷 数据结构 数组&双指针 LeetCode 1. 两数之和 /*** 给定一个整数数组 ...

  9. 力扣(LeetCode)打卡刷题交流计划(长期维护)

    前言 忙忙活活暑期过去了一半,在即将升学的日子里,打算干点什么东西,由于实力以及经验的欠缺没有创群和大家讨论,但我更喜欢以更实在的方式--能作点什么--和大家一起从0打开力扣LeetCode, 对,没 ...

最新文章

  1. ASP.NET Web Pages – 文件夹简介
  2. Vue2 利用 v-model 实现组件props双向绑定的优美解决方案
  3. les物流执行系统_物流规划工作如何开展?
  4. MySQL 数据库的备份和恢复
  5. VC编写的程序不能在其他机器上运行的解决方案
  6. java 区间树_线段树(区间树)之区间染色和4n推导过程
  7. android-support安装不,导入android.support无法解析
  8. Servlet和JSP中的文件上传示例
  9. JavaScript设计模式 Item 2 -- 接口的实现
  10. Python 数据结构与算法——从某个列表中找出两个彼此最接近但不相等的数
  11. react 点击使父元素消失_React 基础:Refs 和 DOM 引用之间的关系
  12. 【王道计组笔记】输入/输出设备磁盘
  13. Java知多少(87)选择框和单选按钮
  14. 集成Android SlidingMenu(SlideMenu)
  15. soui 设置边框_第四篇:SOUI资源文件组织
  16. 计算机专业毕业了,还要不要参加培训班
  17. NYOJ 17 (最长单调递增子序列) O (n*n) + O(n*lgn)
  18. arctanx麦克劳林公式推导过程_【数学】「专题」初识泰勒级数(Taylor Series)与泰勒公式(Taylor#x27;s Formula)...
  19. altium Designer使用方法大总结
  20. 泰坦尼克号幸存者的预测

热门文章

  1. 段晓君NPDP产品经理备考心得:给自己持续的鼓励,保持良好心态
  2. 35款锁屏勒索程序曝光,多为抢红包、游戏外挂类软件...
  3. 「小技巧」如何让IGV更快的加载GTF和GFF注释文件
  4. AQR资本20年精选20篇之:风格因子工匠精神Alpha
  5. 作息时间表 (2005.3 ~ 2005.7)
  6. RL:prat1:key_concepts_in_RL强化学习
  7. android安装在sd卡上,android_安装软件到SD卡上
  8. 计算机在职博士好毕业么,计算机在职博士毕业答辩流程
  9. vue中使用 百度地图 轨迹动画
  10. 推荐算法之潜在因子(Latent Factor)算法