leecode,rt_phm更新更新

This commit is contained in:
kevinding1125 2023-06-13 13:46:11 +08:00
parent 9de573cb99
commit d3b671b006
2 changed files with 122 additions and 5 deletions

View File

@ -134,4 +134,49 @@ public class T79_301_RemoveInvalidParentheses {
} }
//判断左括号右括号哪边多哪边多就把哪边的删了
public List<String> removeInvalidParentheses2(String s) {
int left = 0;
int right = 0;
for (int i = 0; i < s.length(); i++) {
char cur = s.charAt(i);
if (cur == '(') {
left++;
} else if (cur == ')') {
if (left > 0) {
left--;
} else {
right++;
}
}
}
List<String> result = new ArrayList<>();
remove(left, right, s, result, 0);
return result;
}
public void remove(int left, int right, String s, List<String> result, int start) {
if (left == 0 && right == 0) {
if (isValid(s)) {
result.add(new String(s));
}
return;
}
for (int i = start; i < s.length(); i++) {
char cur = s.charAt(i);
if (i > start && cur == s.charAt(i - 1)) {
continue;
}
if (left > 0 && cur == '(') {
remove(left - 1, right, s.substring(0, i) + s.substring(i + 1), result, i);
}
if (right > 0 && cur == ')') {
remove(left, right - 1, s.substring(0, i) + s.substring(i + 1), result, i);
}
}
}
} }

View File

@ -0,0 +1,72 @@
package com.markilue.leecode.interview.huawei.T0412;
import java.util.*;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.interview.huawei.T0412
*@Author: markilue
*@CreateTime: 2023-06-13 11:24
*@Description:
* TODO 获取最多的食物:
* 主办方设计了一个获取食物的游戏
* 游戏的地图由N个方格组成每个方格上至多2个传送门通过传送门可将参与者传送至指定的其它方格
* 同时每个方格上标注了三个数字:
* (1) 第一个数字id:代表方格的编号从0到N-1每个方格各不相同
* (2)第二个数字parent-id:代表从编号为parent-id的方格可以通过传送门传送到当前方格(-1则表示没有任何方格可以通过传送门传送到此方格这样的方格在地图中有且仅有一个)
* (3)第三个数字value: 取值在[100100]的整数值正整数代表参与者得到相队取值单位的食物负整数代表失去相应数值单位的食物(参与者可能存在临时持有食物为负数的情况)0则代表无变化
*@Version: 1.0
*/
public class Question2 {
static int max = Integer.MIN_VALUE;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
List<List<Node>> edges = new ArrayList<>();
HashMap<Integer, Integer> map = new HashMap<>();//<id,value>
for (int i = 0; i < num; i++) {
edges.add(new ArrayList<>());
}
for (int i = 0; i < num; i++) {
int id = sc.nextInt();
int parentId = sc.nextInt();
int value = sc.nextInt();
map.put(id, value);
if (parentId != -1) {
edges.get(parentId).add(new Node(id, value));
}
}
for (int i = 0; i < num; i++) {
solve(edges, map, 0, i);
}
System.out.println(max);
}
public static void solve(List<List<Node>> edges, Map<Integer, Integer> map, int curValue, int curIndex) {
curValue += map.get(curIndex);
max = Math.max(max, curValue);
List<Node> children = edges.get(curIndex);
for (Node child : children) {
solve(edges, map, curValue, child.id);
}
}
static class Node {
int value;
int id;
public Node() {
}
public Node(int id, int value) {
this.id = id;
this.value = value;
}
}
}