Leetcode Biweekly Contest 81
Graph 題我直接當場去世。
2315. Count Asterisks
Difficulty: Easy, Open in Leetcode
You are given a string s, where every two consecutive vertical bars '|' are grouped into a pair. In other words, the 1st and 2nd '|' make a pair, the 3rd and 4th '|' make a pair, and so forth.
Return the number of '*' in s, excluding the '*' between each pair of '|'.
Note that each '|' will belong to exactly one pair.
題目解釋
以 | 為分隔下去 split,算出奇數 index 的 * 有幾個
Solution
不知道算不算暴力。先用 split() 依照 | 分開字串,接著用 count() 計算奇數 index 中的 *,
加總後就是答案了。
1 | class Solution: |
來看另一種解法,跑過一次的同時紀錄 | 狀況,最後也是加總就答案。
1 | def countAsterisks(self, s: str) -> int: |