leetcode - 括号字符串是否有效
括号字符串是否有效
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
```
/**
* 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
* 有效字符串需满足:
* 左括号必须用相同类型的右括号闭合。
* 左括号必须以正确的顺序闭合。
* 注意空字符串可被认为是有效字符串。
* <p>关键点在于<b>括号对应</b>, 所以需要提前保存括号, 这里使用键值对比较合适</p>
* @param s
* @return
*/
public static boolean checkValidString(String s) {
// 执行用时 :4 ms, 在所有 java 提交中击败了71.86%的用户
//内存消耗 :34.2 MB, 在所有 java 提交中击败了85.82%的用户
if(s == null || s .equals("")) {
return true;
}
// 保存括号对应关系
Map<Character, Character> map = new HashMap<>(3);
map.put('(',')');
map.put('{','}');
map.put('[',']');
// 左括号集合
Set<Character> keys = map.keySet();
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char temp = s.charAt(i);
if(i == 0 && !keys.contains(temp)) {
return false;
}
if(keys.contains(temp)) {
stack.push(temp);
} else {
if(temp != (stack.isEmpty() ? '#' : map.get(stack.pop()))) {
return false;
}
}
}
return stack.isEmpty();
// 没用过java的栈结构, 所以使用了LinkedList, 但是执行时间和空间都比较大
// 执行用时 :15 ms, 在所有 java 提交中击败了9.23%的用户
//内存消耗 :36.1 MB, 在所有 java 提交中击败了40.88%的用户
/* String[] strings = s.split("");
LinkedList<String> strList = new LinkedList<>();
for (int i = 0; i < strings.length; i++) {
if(keys.contains(strings[i])) {
strList.add(strings[i]);
} else {
if(i == 0 || (i != 0 && strList.size() == 0)) {
return false;
} else {
// 判断括号是否匹配
if(strings[i].equals(map.get(strList.getLast()))) {
strList.removeLast();
continue;
} else {
return false;
}
}
}
}
if(strList.size() == 0) {
return true;
}
return false;*/
}
/**
* 执行用时 :3 ms, 在所有 java 提交中击败了86.43%的用户
* 内存消耗 : 34.4 MB, 在所有 java 提交中击败了 84.17%的用户
* @param s
* @return
*/
public static boolean checkValidString1(String s) {
if(s == null || s .equals("")) {
return true;
}
// 保存括号对应关系
Map<Character, Character> map = new HashMap<>(3);
map.put(')','(');
map.put('}','{');
map.put(']','[');
// 左括号集合
Set<Character> keys = map.keySet();
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
Character temp = s.charAt(i);
if(keys.contains(temp)) {
Character element = stack.isEmpty() ? '#' : stack.pop();
if(!element.equals(map.get(temp))) {
return false;
}
} else {
stack.push(temp);
}
}
return stack.isEmpty();
}
public static void main(String[] args) {
/* String s1 = "{}[]()";
String s2 = "([{}])";
String s3 = "][(){}";
String s4 = "({(())}";*/
String s5 = "[])";
// true
/* System.out.println(checkValidString(s1));
// true
System.out.println(checkValidString(s2));
// false
System.out.println(checkValidString(s3));
// false
System.out.println(checkValidString(s4));*/
System.out.println(checkValidString(s5));
}
```
leetcode - 括号字符串是否有效的更多相关文章
- [LeetCode] Valid Parenthesis String 验证括号字符串
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- [leetcode]678. Valid Parenthesis String验证有效括号字符串
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- [LeetCode]678. 有效的括号字符串、20. 有效的括号(栈)
题目 678. 有效的括号字符串 给定一个只包含三种字符的字符串:( ,) 和 *,写一个函数来检验这个字符串是否为有效字符串.有效字符串具有如下规则: 任何左括号 ( 必须有相应的右括号 ). 任何 ...
- Leetcode 678.有效的括号字符串
有效的括号字符串 给定一个只包含三种字符的字符串:( ,) 和 *,写一个函数来检验这个字符串是否为有效字符串.有效字符串具有如下规则: 任何左括号 ( 必须有相应的右括号 ). 任何右括号 ) 必须 ...
- Java实现 LeetCode 678 有效的括号字符串(暴力+思路转换)
678. 有效的括号字符串 给定一个只包含三种字符的字符串:( ,) 和 *,写一个函数来检验这个字符串是否为有效字符串.有效字符串具有如下规则: 任何左括号 ( 必须有相应的右括号 ). 任何右括号 ...
- leetcode 1541. 平衡括号字符串的最少插入次数
问题描述 给你一个括号字符串 s ,它只包含字符 '(' 和 ')' .一个括号字符串被称为平衡的当它满足: 任何左括号 '(' 必须对应两个连续的右括号 '))' . 左括号 '(' 必须在对应的连 ...
- 判断括号字符串是否为合法+求n对括号的所有组合
n对括号的有效组合数 参考:https://zh.wikipedia.org/wiki/%E5%8D%A1%E5%A1%94%E5%85%B0%E6%95%B0 import java.util.Ar ...
- Leetcode中字符串总结
本文是个人对LeetCode中字符串类型题目的总结,纯属个人感悟,若有不妥的地方,欢迎指出. 一.有关数字 1.数转换 题Interger to roman和Roman to integer这两题是罗 ...
- LeetCode:字符串的排列【567】
LeetCode:字符串的排列[567] 题目描述 给定两个字符串 s1 和 s2,写一个函数来判断 s2 是否包含 s1 的排列. 换句话说,第一个字符串的排列之一是第二个字符串的子串. 示例1: ...
随机推荐
- 数据库报ORA-12514
Listener refused the connection with the following error: ORA-12514, TNS:listener does not currently ...
- Redis—负载状态
服务端启动与客户端连接 # 服务端启动# 客户端连接:host:远程redis服务器IP.port:远程redis服务端口.password:远程redis服务密码(无密码就不需要-a参数了) [ro ...
- Linux—添加开机启动(服务/脚本)
系统启动时需要加载的配置文件 /etc/profile./root/.bash_profile/etc/bashrc./root/.bashrc/etc/profile.d/*.sh./etc/pro ...
- [PHP] 运维新增服务器导致的附件上传失败问题
现象:客服反馈webmail发信上传附件时,报错提示上传失败,发信时提示发送失败前因:运维同事新增加了三台服务器 1.服务器上有一个挂载的公共目录,该目录是存储的上传后的附件文件.该路径是通过一个软链 ...
- C++:map用法及元素的默认值
C++:map用法 一.map基本用法 键值对 第一个参数为键的类型,第二个参数为值的类型. 源代码 #include <iostream> #include <string> ...
- CUDA -- 规约求矩阵的行和
求矩阵每行的和? 可以把每行放入一个不同线程块,这样行与行之间进行粗粒度的并行.而对于每行,其对应的线程块中分配n个线程(对应行宽),使用共享存储器,让每个线程从显存中读取一个数至shared mem ...
- 01-CSS3-justify-content: space-around; justify-content: space-between;
/* justify-content: space-around; 运用在父级元素上 第一个子元素距离左边的距离==最后一个子元素距离右边的距离 除第一个子元素和最后一个子元素外,第2个,第3个... ...
- 谈谈你对OOM的理解?
(1)整体架构 (1)ByteBuffer使用native方法,直接在堆外分配内存. 当堆外内存(也即本地物理内存)不够时,就会抛出这个异常 ----GC Direct buffer memo ...
- 初学JavaScript正则表达式(十一)
JavaScript的对象属性 整理自慕课网教学 点此进入
- python 给多人发送邮件,且将结果添加为附件
import unittest,HTMLTestRunnerimport osdef runa(): path=os.getcwd() print(path) a=unittest.defaultTe ...