Skip to content

Commit 6c39f47

Browse files
author
luzhicheng
committed
更新md
更新md
1 parent fa473c3 commit 6c39f47

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

leetcode_stack/main.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010
8. [删除最外层的括号](删除最外层的括号.md)
1111
9. [删除字符串中的所有相邻重复项](删除字符串中的所有相邻重复项.md)
1212
10. [用栈操作构建数组](用栈操作构建数组.md)
13+
11. [整理字符串](整理字符串.md)
14+
12. [文件夹操作日志搜集器](文件夹操作日志搜集器.md)

leetcode_stack/整理字符串.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## 整理字符串
2+
给你一个由大小写英文字母组成的字符串 s 。
3+
4+
一个整理好的字符串中,两个相邻字符 s[i] 和 s[i+1],其中 0<= i <= s.length-2 ,要满足如下条件:
5+
* 若 s[i] 是小写字符,则 s[i+1] 不可以是相同的大写字符。
6+
* 若 s[i] 是大写字符,则 s[i+1] 不可以是相同的小写字符。
7+
8+
请你将字符串整理好,每次你都可以从字符串中选出满足上述条件的 两个相邻 字符并删除,直到字符串整理好为止。
9+
10+
请返回整理好的 字符串 。题目保证在给出的约束条件下,测试样例对应的答案是唯一的。
11+
12+
注意:空字符串也属于整理好的字符串,尽管其中没有任何字符。
13+
14+
#### 示例:
15+
```python
16+
输入:s = "leEeetcode"
17+
输出:"leetcode"
18+
解释:无论你第一次选的是 i = 1 还是 i = 2,都会使 "leEeetcode" 缩减为 "leetcode"
19+
20+
输入:s = "abBAcC"
21+
输出:""
22+
```
23+
24+
#### 代码:
25+
```python
26+
def makeGood(s):
27+
stack = []
28+
for item in s:
29+
if not stack:
30+
stack.append(item)
31+
continue
32+
33+
while stack:
34+
ele = stack.pop()
35+
if ord(ele) - ord(item) == 32 or ord(item) - ord(ele) == 32:
36+
break
37+
else:
38+
stack.append(ele)
39+
stack.append(item)
40+
break
41+
42+
return ''.join(stack)
43+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## 文件夹操作日志搜集器
2+
每当用户执行变更文件夹操作时,LeetCode 文件系统都会保存一条日志记录。
3+
下面给出对变更操作的说明:
4+
* "../" :移动到当前文件夹的父文件夹。如果已经在主文件夹下,则 继续停留在当前文件夹 。
5+
* "./" :继续停留在当前文件夹。
6+
* "x/" :移动到名为 x 的子文件夹中。题目数据 保证总是存在文件夹 x 。
7+
给你一个字符串列表 logs ,其中 logs[i] 是用户在 ith 步执行的操作。
8+
9+
文件系统启动时位于主文件夹,然后执行 logs 中的操作。
10+
11+
执行完所有变更文件夹操作后,请你找出 返回主文件夹所需的最小步数 。
12+
13+
示例:
14+
```python
15+
输入:logs = ["d1/","../","../","../"]
16+
输出:0
17+
```
18+
19+
#### 代码
20+
```python
21+
def minOperations(logs):
22+
stack = []
23+
for item in logs:
24+
if item == '../':
25+
if stack:
26+
stack.pop()
27+
elif item == './':
28+
pass
29+
else:
30+
stack.append(item)
31+
return len(stack)
32+
```

0 commit comments

Comments
 (0)