Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【专题】 反向思考 #572

Open
azl397985856 opened this issue Nov 14, 2022 · 3 comments
Open

【专题】 反向思考 #572

azl397985856 opened this issue Nov 14, 2022 · 3 comments

Comments

@azl397985856
Copy link
Owner

记录反向思考的题目。比如: 991. 坏了的计算器

class Solution:
    def brokenCalc(self, startValue: int, target: int) -> int:
        ans = 0
        while startValue != target:
            if target < startValue: return ans + startValue - target
            if target & 1:
                target += 1
            else:
                target = target // 2
            ans += 1
        return ans
@azl397985856
Copy link
Owner Author

#423

@azl397985856
Copy link
Owner Author

azl397985856 commented Nov 15, 2022

@azl397985856
Copy link
Owner Author

1358. 包含所有三种字符的子字符串数目

不要思考往前舍去 多少,而是思考往后加上多少

class Solution:
    def numberOfSubstrings(self, s: str) -> int:
        i = ans = 0
        w = collections.Counter()
        for j in range(len(s)):
            w[s[j]] += 1
            while i < j and len(w) == 3:
                ans += len(s) - j
                w[s[i]] -= 1
                if w[s[i]] == 0: del w[s[i]]
                i += 1
        return ans

Repository owner deleted a comment from cherie-wuyajun Nov 15, 2022
Repository owner deleted a comment from lording Nov 15, 2022
Repository owner deleted a comment from xiaosisong Nov 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant