Created
July 24, 2018 09:13
-
-
Save luojiyin1987/eec87a45fcb099d53152e9a94d2ccebd to your computer and use it in GitHub Desktop.
Count Binary Substrings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func countBinarySubstrings(s string) int { | |
| count, countZero, countOne := 0, 0, 0 | |
| prev := rune(s[0]) | |
| for _, r := range s { | |
| if prev == r { | |
| if r == '0' { | |
| countZero++ | |
| } else { | |
| countOne++ | |
| } | |
| } else { | |
| // 较少的数字决定了符合题意的子字符串个数 | |
| // 例如 "00011" 符合题意的子字符串为 "0011", "01",其中第一个 "0" 是无用的 | |
| count += min(countZero, countOne) | |
| if r == '0' { | |
| countZero = 1 | |
| } else { | |
| countOne = 1 | |
| } | |
| } | |
| prev = r | |
| } | |
| return count + min(countZero, countOne) | |
| } | |
| func min(a, b int) int { | |
| if a < b { | |
| return a | |
| } | |
| return b | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment