LeetCode 每日刷题!!

Remove Invalid Parentheses

这道题在做的时候用到了BFS因为它要求的是最优解

层次依次为删除0个元素,1个元素,2个元素。。。

如果在一层找到符合要求的就返回

在做的时候考虑效率问题,要对BFS进行剪枝,不然会timelimit, 使用的方法一:出现过的就加入visited里面不再加入叶子节点,二:visited用hash,提高查找效率

#include <iostream>
#include <stdio.h>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <unordered_set>
#include <queue>
#include <stack>
#include <assert.h>
#include <set>
using namespace std;class Solution {
public:vector<string> removeInvalidParentheses(string s) {set<string> visited;string *current = &s;vector<string> ans;vector<string> layerSet;vector<string> nextlayer;if (s == ""){ans.push_back("");return ans;}layerSet.push_back(s);while (layerSet.size() != 0){//each layer of a BFT, the optimize have the least delete number//and each loop have the same delete num.for (int i = 0; i < layerSet.size(); i++){*current = layerSet[i];if (isValid(*current)){ans.push_back(*current);}}if (ans.size() != 0){return ans;}for (int i = 0; i < layerSet.size(); i++){*current = layerSet[i];for (int i = 0; i < current->size(); i++){if ((*current)[i] != '(' && (*current)[i] != ')'){continue;}string tmp = current->substr(0, i) + current->substr(i + 1);if (tmp == ""){ans.push_back("");return ans;}if (visited.find(tmp) == visited.end()){nextlayer.push_back(tmp);visited.insert(tmp);}}}layerSet.swap(nextlayer);}return ans;}bool isValid(string s){int num = 0;if (s.size() == 0){return false;}for (int i = 0; i < s.size(); i++){if (s[i] == '(') {num++;}if (s[i] == ')') {num--;}if (num < 0)return false;}return num == 0;}
};int main()
{string test1 = ")()m)(((()((()((((";vector<string> ans;Solution *solu = new Solution();ans = solu->removeInvalidParentheses(test1);for (int i = 0; i < ans.size(); i++){cout << ans[i] << endl;}system("pause");return 0;}

LeetCode Remove Invalid Parenthese相关推荐

  1. LeetCode:Remove Duplicates from Sorted List I II

    LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...

  2. 301. Remove Invalid Parentheses

    题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Return ...

  3. [LeetCode] Remove Element 分析

    Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接:https://leetcode.com/problems/remove-el ...

  4. LeetCode Remove K Digits

    原题链接在这里:https://leetcode.com/problems/remove-k-digits/description/ 题目: Given a non-negative integer  ...

  5. 301 Remove Invalid Parentheses 删除无效的括号

    删除最小数目的无效括号,使输入的字符串有效,返回所有可能的结果. 注意: 输入可能包含了除 ( 和 ) 以外的元素. 示例 : "()())()" -> ["()( ...

  6. LeetCode:Remove Nth Node From End of List

    题目链接 Given a linked list, remove the nth node from the end of list and return its head. For example, ...

  7. [LeetCode] Remove Element

    代码: 1 1 class Solution { 2 2 public: 3 3 int removeElement(int A[], int n, int elem) { 4 4 int index ...

  8. LeetCode Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  9. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

    Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...

最新文章

  1. 一、Axis2 WebService开发准备工作
  2. 太阳光是平行光吗_科普文系列活动 || 奇妙的光现象
  3. Microbit-高温报警系统
  4. 嗅觉计算机应用,重磅!美国科技巨头宣布!计算机终于有了“嗅觉”了!
  5. 斯坦福大学机器学习第六课“神经网络的表示(Neural Networks: Representation)”
  6. 倒计时 3 天!1024 程序员节全日程曝光,105 场深度演讲点燃数字经济新时代
  7. 核心对象+持久对象全析(1)
  8. 机械制图与计算机辅助绘图题,机械制图与计算机绘图作业题解ppt课件.ppt
  9. Java实习日记(2-1)
  10. Android RTSP IPC的使用
  11. Nginx专题:nginx+tomcat实现动静分离
  12. 周鸿祎亲自站台,360年度新旗舰究竟有何特别?
  13. ImportError: sys.meta_path is None, Python is likely shutting down 解决方案
  14. JavaScript闭包的个人理解
  15. Alibaba Arthas 3.1.2版本发布:增加logger/heapdump/vmoption命令,支持tunnel server
  16. linux三维动画软件,Blender2.9免费版下载
  17. 程序员团队名称和口号_这50个团建活动的口号,你曾经叫过几次呢?
  18. Excel累加上一行的数值
  19. g4600支持服务器内存吗,Intel奔腾G4560和G4600哪个好?秒懂G4560和G4600区别 (全文)
  20. 【iOS开发】---- 网站或技术论坛

热门文章

  1. 基于ssm的零食商城的设计与实现
  2. 基于MT6752/32平台 Android L版本驱动移植步骤 根据MK官网所述,在Android L 版本上Turnkey ABS 架构将会phase out,而Medi
  3. Etcd特性学习1——KV操作
  4. 微信小程序 等待几秒、_微信小程序—setTimeOut定时器的问题及解决
  5. 国内市场:朝阳赛道,万类春光竞自由
  6. 适合Python 的5大练手项目,你练了么?
  7. 【Android】按照遗忘规律自动制定背单词计划功能的实现
  8. 一个UI设计师在工作中所需要具备的综合能力!
  9. 美国光伏市场或将关闭 “201”保护有何玄机?
  10. 在家远程访问公司内网办公系统