Skip to content

Commit 6078971

Browse files
committed
feat: update valid parentheses
1 parent 11455a1 commit 6078971

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

leetcode/string/valid_parentheses.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Chacha
33
* @Date: 2022-03-31 21:48:11
44
* @Last Modified by: Chacha
5-
* @Last Modified time: 2022-04-01 22:05:50
5+
* @Last Modified time: 2022-04-01 22:39:48
66
*/
77

88
/**
@@ -51,8 +51,18 @@ class Solution
5151

5252
/**
5353
* 判断括号的有效性可以使用「栈」这一数据结构来解决。
54-
* 栈先入后出特点恰好与本题括号排序特点一致,即若遇到左括号入栈,
55-
* 遇到右括号时将对应栈顶左括号出栈,则遍历完所有括号后 stack 仍然为空
54+
* 1. 栈先入后出特点恰好与本题括号排序特点一致,即若遇到左括号入栈,
55+
* 遇到右括号时将对应栈顶左括号出栈,则遍历完所有括号后 stack 仍然为空。
56+
* 2. 建立哈希表 pairs 构建左右括号对应关系:key左括号,value右括号;这样查询 2 个括号是否对应,
57+
* 只需 O(1) 时间复杂度;建立栈 stack,遍历字符串 s 并按照算法流程一一判断
58+
*
59+
* 算法流程:
60+
* 1. 如果 ch 是 左括号,则入栈 push
61+
* 2. 否则通过哈希表判断括号对应关系,若 stack 栈顶出栈括号 stack.top() 与当前遍历括号 ch 不对应,则提前返回 false
62+
*
63+
* 复杂度分析:
64+
* 时间复杂度:正确的括号组合需要遍历 1 遍 s
65+
* 空间复杂度:哈希表和栈使用线性的空间大小
5666
*
5767
*/
5868
bool Solution::isValid(string s)
0 Bytes
Binary file not shown.

leetcode/two_pointers/container_with_most_water.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Chacha
33
* @Date: 2022-04-01 21:20:42
44
* @Last Modified by: Chacha
5-
* @Last Modified time: 2022-04-01 22:04:58
5+
* @Last Modified time: 2022-04-01 22:10:08
66
*/
77

88
/**
@@ -61,8 +61,7 @@ class Solution
6161
*/
6262
int Solution::maxArea(vector<int> &height)
6363
{
64-
int i = 0, j = height.size() - 1;
65-
int ans = 0;
64+
int i = 0, j = height.size() - 1, ans = 0;
6665

6766
while (i < j)
6867
{
@@ -77,9 +76,12 @@ int main(int argc, char const *argv[])
7776
Solution s;
7877

7978
vector<int> height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
79+
vector<int> height1 = {1, 1};
8080

8181
cout
8282
<< "输入:[1,8,6,2,5,4,8,3,7] 输出:" << s.maxArea(height) << endl;
83+
cout
84+
<< "输入:[1, 1] 输出:" << s.maxArea(height1) << endl;
8385

8486
return 0;
8587
}

0 commit comments

Comments
 (0)