diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T79_301_RemoveInvalidParentheses.java b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T79_301_RemoveInvalidParentheses.java index 00f3860..1e13f6c 100644 --- a/Leecode/src/main/java/com/markilue/leecode/hot100/second/T79_301_RemoveInvalidParentheses.java +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/second/T79_301_RemoveInvalidParentheses.java @@ -103,11 +103,11 @@ public class T79_301_RemoveInvalidParentheses { } //判断完之后就需要进行删除;同时,不可能存在left,right都大于0的情况 List result = new ArrayList<>(); - remove(s, left, right, result,0); + remove(s, left, right, result, 0); return result; } - public void remove(String s, int left, int right, List res,int start) { + public void remove(String s, int left, int right, List res, int start) { if (left == 0 && right == 0) { if (isValid(s)) { res.add(s); @@ -118,17 +118,62 @@ public class T79_301_RemoveInvalidParentheses { //必须在删除的后面继续删才可以 for (int i = start; i < s.length(); i++) { //去重 - if(i>start&&s.charAt(i)==s.charAt(i-1))continue; + if (i > start && s.charAt(i) == s.charAt(i - 1)) continue; //不够了 if (left + right > s.length() - i) return; if (left > 0 && s.charAt(i) == '(') { //可以移除左边 - remove(s.substring(0, i) + s.substring(i + 1), left - 1, right, res,i); + remove(s.substring(0, i) + s.substring(i + 1), left - 1, right, res, i); } if (right > 0 && s.charAt(i) == ')') { //可以移除左边 - remove(s.substring(0, i) + s.substring(i + 1), left, right - 1, res,i); + remove(s.substring(0, i) + s.substring(i + 1), left, right - 1, res, i); + } + } + } + + + //判断左括号右括号哪边多,哪边多就把哪边的删了 + public List 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 result = new ArrayList<>(); + remove(left, right, s, result, 0); + return result; + } + + public void remove(int left, int right, String s, List 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); } } } diff --git a/Leecode/src/main/java/com/markilue/leecode/interview/huawei/T0412/Question2.java b/Leecode/src/main/java/com/markilue/leecode/interview/huawei/T0412/Question2.java new file mode 100644 index 0000000..bbbce6e --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/interview/huawei/T0412/Question2.java @@ -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: 取值在[100,100]的整数值,正整数代表参与者得到相队取值单位的食物,负整数代表失去相应数值单位的食物(参与者可能存在临时持有食物为负数的情况),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> edges = new ArrayList<>(); + HashMap map = new HashMap<>();// + 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> edges, Map map, int curValue, int curIndex) { + + curValue += map.get(curIndex); + max = Math.max(max, curValue); + List 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; + } + } +}