classSolution { public: intcountBinarySubstrings(string s){ vector<int> counts; int ptr = 0, n = s.size(); while (ptr < n) { char c = s[ptr]; int count = 0; while (ptr < n && s[ptr] == c) { ++ptr; ++count; } counts.push_back(count); } int ans = 0; for (int i = 1; i < counts.size(); ++i) { ans += min(counts[i], counts[i - 1]); } return ans; } };
classSolution { publicintcountBinarySubstrings(String s) { List<Integer> counts = newArrayList<Integer>(); intptr=0, n = s.length(); while (ptr < n) { charc= s.charAt(ptr); intcount=0; while (ptr < n && s.charAt(ptr) == c) { ++ptr; ++count; } counts.add(count); } intans=0; for (inti=1; i < counts.size(); ++i) { ans += Math.min(counts.get(i), counts.get(i - 1)); } return ans; } }
[sol0-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var countBinarySubstrings = function(s) { const counts = []; let ptr = 0, n = s.length; while (ptr < n) { const c = s.charAt(ptr); let count = 0; while (ptr < n && s.charAt(ptr) === c) { ++ptr; ++count; } counts.push(count); } let ans = 0; for (let i = 1; i < counts.length; ++i) { ans += Math.min(counts[i], counts[i - 1]); } return ans; };
funccountBinarySubstrings(s string)int { counts := []int{} ptr, n := 0, len(s) for ptr < n { c := s[ptr] count := 0 for ptr < n && s[ptr] == c { ptr++ count++ } counts = append(counts, count) } ans := 0 for i := 1; i < len(counts); i++ { ans += min(counts[i], counts[i-1]) } return ans }
funcmin(x, y int)int { if x < y { return x } return y }
intcountBinarySubstrings(char* s) { int n = strlen(s); int counts[n], counts_len = 0; memset(counts, 0, sizeof(counts)); int ptr = 0; while (ptr < n) { char c = s[ptr]; int count = 0; while (ptr < n && s[ptr] == c) { ++ptr; ++count; } counts[counts_len++] = count; } int ans = 0; for (int i = 1; i < counts_len; ++i) { ans += fmin(counts[i], counts[i - 1]); } return ans; }
这个实现的时间复杂度和空间复杂度都是 O(n)。
对于某一个位置 i,其实我们只关心 i - 1 位置的 counts 值是多少,所以可以用一个 last 变量来维护当前位置的前一个位置,这样可以省去一个 counts 数组的空间。
代码
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public: intcountBinarySubstrings(string s){ int ptr = 0, n = s.size(), last = 0, ans = 0; while (ptr < n) { char c = s[ptr]; int count = 0; while (ptr < n && s[ptr] == c) { ++ptr; ++count; } ans += min(count, last); last = count; } return ans; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { publicintcountBinarySubstrings(String s) { intptr=0, n = s.length(), last = 0, ans = 0; while (ptr < n) { charc= s.charAt(ptr); intcount=0; while (ptr < n && s.charAt(ptr) == c) { ++ptr; ++count; } ans += Math.min(count, last); last = count; } return ans; } }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
var countBinarySubstrings = function(s) { let ptr = 0, n = s.length, last = 0, ans = 0; while (ptr < n) { const c = s.charAt(ptr); let count = 0; while (ptr < n && s.charAt(ptr) === c) { ++ptr; ++count; } ans += Math.min(count, last); last = count; } return ans; };
funccountBinarySubstrings(s string)int { var ptr, last, ans int n := len(s) for ptr < n { c := s[ptr] count := 0 for ptr < n && s[ptr] == c { ptr++ count++ } ans += min(count, last) last = count }
return ans }
funcmin(x, y int)int { if x < y { return x } return y }
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
intcountBinarySubstrings(char* s) { int ptr = 0, n = strlen(s), last = 0, ans = 0; while (ptr < n) { char c = s[ptr]; int count = 0; while (ptr < n && s[ptr] == c) { ++ptr; ++count; } ans += fmin(count, last); last = count; } return ans; }