2019-11-26 11:05:10

  • 1096. Brace Expansion II

问题描述:

问题求解

经典的字符串扩展问题。

一般来说这种问题有两种解法,一个是采用stack,一个是采用recursive。事实证明,这种题目采用recursive在时间效率和程序易读性上要远远高于stack,请尽量采用recursive。

本题有个很好的讲解视频:https://www.youtube.com/watch?v=blXuT7DOMwU

    public List<String> braceExpansionII(String expression) {
List<String> res = new ArrayList<>();
if (expression.length() <= 1) {
res.add(expression);
return res;
}
if (expression.charAt(0) == '{') {
int cnt = 0;
int idx = 0;
for (; idx < expression.length(); idx++) {
if (expression.charAt(idx) == '{') cnt += 1;
if (expression.charAt(idx) == '}') cnt -= 1;
if (cnt == 0) break;
}
List<String> strs = helper(expression.substring(1, idx));
HashSet<String> set = new HashSet<>();
for (String str : strs) {
List<String> tmp = braceExpansionII(str);
set.addAll(tmp);
}
List<String> rest = braceExpansionII(expression.substring(idx + 1));
for (String str1 : set) {
for (String str2 : rest) {
res.add(str1 + str2);
}
}
}
else {
String prev = expression.charAt(0) + "";
int idx = 0;
List<String> rest = braceExpansionII(expression.substring(1));
for (String s : rest) res.add(prev + s);
}
Collections.sort(res);
return res;
} public List<String> helper(String s) {
List<String> res = new ArrayList<>();
int cnt = 0;
int i = 0;
for (int j = 0; j < s.length(); j++) {
if (s.charAt(j) == ',') {
if (cnt == 0) {
res.add(s.substring(i, j));
i = j + 1;
}
}
else if (s.charAt(j) == '{') cnt += 1;
else if (s.charAt(j) == '}') cnt -= 1;
}
res.add(s.substring(i));
return res;
}

 

同类题:

  • 1106. Parsing A Boolean Expression

问题描述

问题求解

没过几天又一次在hard区看到类似的题目,这次的题目我认为就是上面的题目换了一个说法,而且相对来说更加简单了。

果然,直接使用recursive就ac了。

    public boolean parseBoolExpr(String expression) {
if (expression.length() == 1) {
return expression.charAt(0) == 't' ? true : false;
}
char first = expression.charAt(0);
if (first == '!') return !parseBoolExpr(expression.substring(2, expression.length() - 1));
List<String> strs = helper(expression.substring(2, expression.length() - 1));
boolean res = false;
if (first == '|') {
for (String str : strs) {
if (parseBoolExpr(str)) res = true;
}
}
else {
res = true;
for (String str : strs) {
if (!parseBoolExpr(str)) res = false;
}
}
return res;
} private List<String> helper(String s) {
List<String> res = new ArrayList<>();
int cnt = 0;
int i = 0;
for (int j = 0; j < s.length(); j++) {
if (s.charAt(j) == '(') cnt += 1;
if (s.charAt(j) == ')') cnt -= 1;
if (s.charAt(j) == ',' && cnt == 0) {
res.add(s.substring(i, j));
i = j + 1;
}
}
res.add(s.substring(i));
return res;
}

  

 

Recursive-Brace Expansion II的更多相关文章

  1. LeetCode 1087. Brace Expansion

    原题链接在这里:https://leetcode.com/problems/brace-expansion/ 题目: A string S represents a list of words. Ea ...

  2. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

  3. vim_action

    读取文件,显示行号 nl -a.txt brace expansion 花括号扩展 echo a{A{1,2},B{3,4}}b mkdir {2009...2011}-0{1...9} {2009. ...

  4. SH Script Grammar

    http://linux.about.com/library/cmd/blcmdl1_sh.htm http://pubs.opengroup.org/onlinepubs/9699919799/ut ...

  5. oracle已知会导致错误结果的bug列表(Bug Issues Known to cause Wrong Results)

    LAST UPDATE:     1 Dec 15, 2016 APPLIES TO:     1 2 3 4 Oracle Database - Enterprise Edition - Versi ...

  6. CentOS 7.4 初次手记:第三章 CentOS基础了解

    第三章 CentOS基础了解... 36 第一节 语言编码.终端... 36 I 查看语言编码... 36 II Tty?.pts/?. 36 第二节 bash/sh command. 38 I 查找 ...

  7. man bash

    BASH(1) General Commands Manual BASH(1) NAME bash - GNU Bourne-Again SHell SYNOPSIS bash [options] [ ...

  8. bash5.0参考手册

    Bash Reference Manual a.summary-letter { text-decoration: none } blockquote.indentedblock { margin-r ...

  9. Here String 中不该进行分词

    我们知道,在 Shell 中,一个变量在被展开后,如果它没有被双引号包围起来,那么它展开后的值还会进行一次分词(word splitting,或者叫拆词,分词这个术语已经被搜索引擎相关技术占用了)操作 ...

随机推荐

  1. 一劳永逸的解决AFNetworking3.0网络请求问题

    AFNetworking在iOS网络请求第三方库中占据着半壁江山,前段时间将AFNetworking进行了3.0版本的迁移,运用面向对象的设计将代码进行封装整合,这篇文章主要为还在寻找AFNetwor ...

  2. Linux USB 鼠标驱动程序详解(转)

    Linux USB 鼠标驱动程序详解 USB 总线引出两个重要的链表!一个 USB 总线引出两个重要的链表,一个为 USB 设备链表,一个为 USB 驱动链表.设备链表包含各种系统中的 USB 设备以 ...

  3. AAAI |如何保证人工智能系统的准确性?

    ​ |如何保证人工智能系统的准确性?" title="AAAI |如何保证人工智能系统的准确性?"> ​ 注:本文译自AI is getting smarter; ...

  4. Pandorabox固件路由器上申请Let's Encrypt证书,为内网里的多个web服务提供SSL支持

    对于家中宽带有公网IP的用户,有时我们需要将路由器内部网络的某些web服务通过端口转发暴露到外网(例如NAS远程访问),但HTTP是明文传输,有被监听的风险:如果在NAS上使用自签名证书,再端口转发, ...

  5. C++ 迷宫寻路问题

    迷宫寻路应该是栈结构的一个非常经典的应用了, 最近看数据结构算法应用时看到了这个问题, 想起来在校求学时参加算法竞赛有遇到过相关问题, 感觉十分亲切, 在此求解并分享过程, 如有疏漏, 欢迎指正 问题 ...

  6. Python基础--动态传参

    形参的顺序: 位置 *arg     默认值  **args  ps:可以随便搭配,但是*和**以及默认值的位置顺序不能变 *,** 形参:聚合 位置参数* >>元祖 关键字** > ...

  7. Python学习之布尔和数字

    布尔有True和Flase两种值 数字0.None,以及元素为空的容器类对象都可视为False,反之为Ture.

  8. openwrt sdk 添加软件包 Makefile 写法

    参考 https://openwrt.org/start?id=docs/guide-developer/packages ,英文稍好点的自己看吧,我写出来也就是方便,英文不好的人看. 软件包的来源, ...

  9. ASP.NET Core 中jwt授权认证的流程原理

    目录 1,快速实现授权验证 1.1 添加 JWT 服务配置 1.2 颁发 Token 1.3 添加 API访问 2,探究授权认证中间件 2.1 实现 Token 解析 2.2 实现校验认证 1,快速实 ...

  10. JDk下载和环境变量Path的配置

    JDK下载与安装 下载地址 打开该网址会显示如下图,点击DOWMLOAD即可: 出现该页面时,点击接受: 选择对应的安装包下载即可(本人用的是Windows64位): 注:如果您无法确定您的windo ...