FB面经 Prepare: Make Parentheses valid

给一组括号,remove最少的括号使得它valid

从左从右各scan一次

 1 package fb;
 2
 3 public class removeParen {
 4
 5     public static String fix(String str) {
 6         StringBuffer res = new StringBuffer(str);
 7         int l = 0, r = 0;
 8         int i = 0;
 9         while (i < res.length()) {
10             if (res.charAt(i) == ‘(‘) l++;
11             else {
12                 if (l <= r) {
13                     res.deleteCharAt(i);
14                     i--;
15                 }
16                 else {
17                     r++;
18                 }
19             }
20             i++;
21         }
22
23         l = 0;
24         r = 0;
25         i = res.length()-1;
26         while (i >= 0) {
27             if (res.charAt(i) == ‘)‘) r++;
28             else {
29                 if (l >= r) {
30                     res.deleteCharAt(i);
31                 }
32                 else {
33                     l++;
34                 }
35             }
36             i--;
37         }
38
39         return res.toString();
40     }
41
42
43     /**
44      * @param args
45      */
46     public static void main(String[] args) {
47         // TODO Auto-generated method stub
48         String res = fix(")((())(");
49         System.out.println(res);
50     }
51
52 }
时间: 2024-10-10 17:52:16

FB面经 Prepare: Make Parentheses valid的相关文章

72. Generate Parentheses &amp;&amp; Valid Parentheses

Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]"

[leetcode-921-Minimum Add to Make Parentheses Valid]

Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', and in any positions ) so that the resulting parentheses string is valid. Formally, a parentheses string is valid if and only if: It is the empty stri

LeetCode-921 Minimum Add to Make Parentheses Valid Solution (with Java)

1. Description: Notes:  2. Examples: 3.Solutions: 1 /** 2 * Created by sheepcore on 2019-05-07 3 */ 4 class Solution { 5 public int minAddToMakeValid(String s) { 6 Stack<Character> stack = new Stack<Character>(); 7 int addnum = 0; 8 for(int i

FB面经 Prepare: K closest point to the origin

Give n points on 2-D plane, find the K closest points to origin 1 package fbPractise; 2 3 import java.util.*; 4 5 class Coordinate { 6 int x; 7 int y; 8 public Coordinate(int x, int y) { 9 this.x = x; 10 this.y = y; 11 } 12 } 13 14 public class Kclos

FB面经prepare: Task Schedule

每种task都有冷却时间,比如task1执行后,要经过interval时间后才能再次执行,求总共所需时间. 用HashMap保存每一个task的下一次可以开始执行的最早时间 1 package TaskSchedule; 2 import java.util.*; 3 4 public class Solution { 5 public int schedule(int[] str, int recover) { 6 if (str==null || str.length==0) return

FB面经Prepare: Merge K sorted Array

跟Merge K sorted lists不同在于,从PQ里poll出来以后不知道下一个需要被加入PQ的是哪一个 所以需要写一个wrapper class import java.util.*; public class MergeKLists { static class Pair { int listIndex; int idInList; int value; public Pair(int l, int id, int val) { this.listIndex = l; this.id

2019年3月27日 921. Minimum Add to Make Parentheses Valid

不是很难的模拟题,想好前后状态的变化就会发现,其实")"括号才可以抵消之前的"("括号,反之不行. class Solution(object): def minAddToMakeValid(self, S): """ :type S: str :rtype: int """ l, r = 0, 0 for i in S: if i == '(': r += 1 elif i == ')': if r &g

921. Minimum Add to Make Parentheses Valid

给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的括号( '(' 或是 ')',可以在任何位置),以使得到的括号字符串有效. 从形式上讲,只有满足下面几点之一,括号字符串才是有效的: 它是一个空字符串,或者 它可以被写成 AB (A 与 B 连接), 其中 A 和 B 都是有效字符串,或者 它可以被写作 (A),其中 A 是有效字符串. 给定一个括号字符串,返回为使结果字符串有效而必须添加的最少括号数. # left表示需要的左括号数,right表示需要的右括号数. # 遍

[LeetCode] Remove Invalid Parentheses 移除非法括号

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Examples: "()())()" -> ["()()()",