696. Count Binary Substrings
1.æ´åæŗ, æžåēææ substring, then count 1's count equal 0's count, but 0,1æ¯čĻéŖįēåēįžį
å ļå¯Ļį 0 or 1 åēįžįéŖįēčĄįē
ex: 10101 â substring, 1010 â ä¸å°åīŧ 0 æ 1 čĻ group éŖįēåēįž
2. run length... å ļå¯Ļé常ä¸åĨŊæŗ
Run Length
time: O(n)
space: O(1)
æčˇ¯
s(i) = s(i-1) â å¤æˇæ¯åĻéŖįēä¸æ¨Ŗ
ä¸ä¸æ¨Ŗįæå â 1. æ´æ° pre, 2 æ´æ° cur
ex : 0 0 1 1 â å°1 æ, ä¸ä¸æ¨Ŗäē, éæå cur éæ¯ 00,
do pre = cur
cur = 1
â pre: 00, cur = 1
ex. 0011
hit the first '1'
when you hit the first '1', curRun = 1, preRun = 2,
means 0s number is larger than 1s number, so we could form "01" at this time, count++ .
hit the second '1',
When you hit the second '1', curRun = 2, preRun = 2,
means 0s' number equals to 1s' number, so we could form "0011" at this time, that is why count++)
Last updated