Re: [闲聊] 每日leetcode

楼主: sustainer123 (caster)   2024-07-10 09:55:38
※ 引述《DJYOMIYAHINA (通通打死)》之铭言:
: 今天本肥肥也是个称职的ez守门员
: 准备出门当帕鲁
: def minOperations(self, logs: List[str]) -> int:
: base = 0
: for log in logs:
: if log == "../":
: base = max(base-1, 0)
: elif log != "./":
: base += 1
: return base
https://leetcode.com/problems/crawler-log-folder
1598. Crawler Log Folder
有一log纪录使用者更改资料夹的操作
操作有三:
1 ../:回到父资料夹 假如已在主资料夹 则不变
2 ./:不变
3 x/:前往子资料夹x
请回传完成log的资料夹操作后 回到主资料夹的最小操作
Example 1:
Input: logs = ["d1/","d2/","../","d21/","./"]
Output: 2
Explanation: Use this change folder operation "../" 2 times and go back to
the main folder.
Example 2:
Input: logs = ["d1/","d2/","./","d3/","../","d31/"]
Output: 3
Example 3:
Input: logs = ["d1/","../","../","../"]
Output: 0
Constraints:
1 <= logs.length <= 103
2 <= logs[i].length <= 10
logs[i] contains lowercase English letters, digits, '.', and '/'.
logs[i] follows the format described in the statement.
Folder names consist of lowercase English letters and digits.
思路:
照题目需求模拟
Python Code:
class Solution:
def minOperations(self, logs: List[str]) -> int:
result = 0
for log in logs:
if log == "../":
if result > 0:
result -= 1
else:
continue
elif log == "./":
continue
else:
result += 1
return result
作者: SecondRun (雨夜琴声)   2024-07-10 09:57:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com