当前位置:网站首页>leetcode hot 100(刷题篇9)(301/45/517/407/offer62/MST08.14/)
leetcode hot 100(刷题篇9)(301/45/517/407/offer62/MST08.14/)
2022-07-29 08:19:00 【武田】
目录
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;
}
}边栏推荐
- UE4 principle and difference between skylight and reflecting sphere
- Alibaba political commissar system - Chapter 1: political commissars are built on companies
- Compatible with cc1101/cmt2300-dp4301 sub-1g wireless transceiver chip
- NFC two-way communication 13.56MHz contactless reader chip -- si512 replaces pn512
- [beauty of software engineering - column notes] 25 | what methods can improve development efficiency?
- torch.Tensor和torch.tensor的区别
- Deep learning (1): prediction of bank customer loss
- A problem encountered in SQL interview
- ML.NET相关资源整理
- Lora opens a new era of Internet of things -asr6500s, asr6501/6502, asr6505, asr6601
猜你喜欢

Proteus simulation based on msp430f2491 (realize water lamp)

Random lottery turntable wechat applet project source code

Deep learning (2): image and character recognition
![[beauty of software engineering - column notes] 27 | what is the core competitiveness of software engineers? (top)](/img/23/288f6c946a44e36ab58eb0555f3650.png)
[beauty of software engineering - column notes] 27 | what is the core competitiveness of software engineers? (top)

Cyberpunk special effect shader

Reading papers on false news detection (4): a novel self-learning semi supervised deep learning network to detect fake news on

数字人民币时代隐私更安全

Tb6600+stm32f407 test
![[beauty of software engineering - column notes] 26 | continuous delivery: how to release new versions to the production environment at any time?](/img/65/79f876b62fa3db421e5038a2445b83.png)
[beauty of software engineering - column notes] 26 | continuous delivery: how to release new versions to the production environment at any time?

Ga-rpn: recommended area network for guiding anchors
随机推荐
[beauty of software engineering - column notes] 22 | how to do a good job in technology selection for the project?
Preparation of SQL judgment statement
Tb6600+stm32f407 test
华为无线设备配置利用WDS技术部署WLAN业务
110 MySQL interview questions and answers (continuously updated)
Cs5340 domestic alternative dp5340 multi bit audio a/d converter
torch.Tensor和torch.tensor的区别
Unity - default rendering pipeline - sculpt shader
sql判断语句的编写
Gan: generate adversarial networks
Stm32ff030 replaces domestic MCU dp32g030
Data warehouse layered design and data synchronization,, 220728,,,,
torch.nn.functional.one_hot()
Implementation of support vector machine with ml11 sklearn
数字人民币时代隐私更安全
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
Cv520 domestic replacement of ci521 13.56MHz contactless reader chip
Hal learning notes - Advanced timer of 7 timer
Proteus simulation based on msp430f2491 (realize water lamp)
Cyberpunk special effect shader