1. 给一组括号,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. * @param args
  44. */
  45. public static void main(String[] args) {
  46. // TODO Auto-generated method stub
  47. String res = fix(")((())(");
  48. System.out.println(res);
  49. }
  50.  
  51. }

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

  1. [LeetCode] 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  2. LeetCode 921. 使括号有效的最少添加(Minimum Add to Make Parentheses Valid) 48

    921. 使括号有效的最少添加 921. Minimum Add to Make Parentheses Valid 题目描述 给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的 ...

  3. 72. Generate Parentheses && Valid Parentheses

    Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  4. [Swift]LeetCode921.使括号有效的最少添加 | Minimum Add to Make Parentheses Valid

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

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

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  6. 921. Minimum Add to Make Parentheses Valid

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  7. 【LeetCode】921. Minimum Add to Make Parentheses Valid 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...

  8. FB面经 Prepare: All Palindromic Substrings

    Given a string, calculate how many substring is palindrome. Ignore non-char characters. Ignore case; ...

  9. FB面经 Prepare: Task Schedule

    tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output ...

随机推荐

  1. ionic2自定义radio样式

    刚开始以为用的是字体图标,结果翻了代码一看竟然是通过纯css实现的,图标模式用的是ios,代码如下: .radio-ios .radio-checked { margin:; border-radiu ...

  2. VM下新建虚拟机并装linux系统

    一.新建虚拟机 1.选择典型----> 2.选择稍后安装操作系统---> 3.选择操作系统和版本----> 4.选择虚拟机存放位置---> 5.配置虚拟机---> 二.l ...

  3. 使用Python下载文件

    python -c "with open('/tmp/file.sh', 'wb') as f: import urllib2; f.write(urllib2.urlopen('http: ...

  4. KVM嵌套虚拟化nested之CPU透传

    嵌套式虚拟nested是一个可通过内核参数来启用的功能.它能够使一台虚拟机具有物理机CPU特性,支持vmx或者svm(AMD)硬件虚拟化.该特性需要内核升级到Linux 3.X版本 ,所以在cento ...

  5. Auth模块、Forms组件

    Auth模块 auth模块是Django自带的用户认证模块: 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这 ...

  6. js获取手机信息

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. jQuery (02) 重点知识点总结

    jQuery 如果用户未登录,当加入购物车,会将商品相关信息存入 cookie 或者 session,这两个都是可以标识用户信息的东西 是一个 JavaScript 库,封装了常用的开发功能,和一些需 ...

  8. [EOJ Monthly 2018.10][C. 痛苦的 01 矩阵]

    题目链接:C. 痛苦的 01 矩阵 题目大意:原题说的很清楚了,不需要简化_(:з」∠)_ 题解:设\(r_i\)为第\(i\)行中0的个数,\(c_j\)为第\(j\)列中0的个数,\(f_{i,j ...

  9. Jumpserver之设置开机自启动

    vi /usr/lib/systemd/system/jms.service [Unit] Description=jms After=network.target mariadb.service r ...

  10. wpf1

    emCombobox.Items[2].IsEnabled = false; 隐藏下拉框里面的一个item wpf 单例模式. [DllImport("user32", CharS ...