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. 码海拾遗:strcpy()、strncpy()和strcpy_s()区别

    1.strcpy() 原型:char *strcpy(char *dst,const char *src) 功能:将以src为首地址的字符串复制到以dst为首地址的字符串,包括'\0'结束符,返回ds ...

  2. 11. 无数人难办事? - 迪米特法则(LoD)

    11.1 第一天上班 时间: 4月2日19点   地点: 小菜大鸟住所的客厅   任务: 小菜, 大鸟      "回来啦! 怎么样? 第一天上班感受多吧." 大鸟关关心的问道.  ...

  3. RocketMQ 零拷贝

    一.零拷贝原理:Consumer 消费消息过程,使用了零拷贝,零拷贝包含以下两种方式: 1.使用 mmap + write 方式  (RocketMQ选择的方式:因为有小块数据传输的需求,效果会比 s ...

  4. vs2017 tfs服务器迁移更换服务器IP地址方法

    今天公司服务器换了IP地址,然后发现tfs的服务器删除不了,也添加不了.最后参考了其他vs版本提供的方法,找到了解决的方法. 一共需要修改两个地方: 1.找到项目的sln文件,使用其他文本编辑器打开, ...

  5. python Could not find a version that satisfies the requirement pymysql (from versions: none) ERROR: No matching distribution found for pymysql

    使用pip安装pymysql出错;Could not find a version that satisfies the requirement cryptography (from pymysql) ...

  6. 前端每日实战:147# 视频演示如何用纯 CSS 创作透视按钮的悬停特效

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/qJEdKb 可交互视频 此视频是可 ...

  7. 汇编语言-[bx]和loop指令和多个段

    5.1 [BX]和内存单元的描述 要完成描述一个内存单元,需要两种信息: 内存单元的地址: 可以用 [0] 表示一个内存单元, 0 表示单元的偏移地址,段地址默认在 ds 中: 同样也可以用 [bx] ...

  8. JavaScript的数组(一)

    在JavaScript中,对象,数组,函数是最最常用的东东了,写完了对象和函数,最后来说说数组吧,提到数组,就只能想到,map,forEach啊,pop,push啊,当真是没有一点点的积累了?这么多年 ...

  9. 逐浪web无障碍与国际化以及全民族语言支持白皮书

    北京时间2019年5月10日,领先的门户网站与WEB内核服务厂商--上海Zoomla!逐浪CMS团队发布其年度重榜产品:逐浪CMS全民族语言与国际版,体验站点:http://demo2.z01.com ...

  10. 轻量级MVC框架(自行开发)

    源码及demo: https://github.com/killallspree/myFrame/