696 python
时间: 2023-10-17 09:31:04 浏览: 92
引用:class Solution(object): def countBinarySubstrings(self, s): groups = for i in xrange(1, len(s)): if s[i-1] != s[i]: groups.append(1) else: groups[-1] = 1 ans = 0 for i in xrange(1, len(groups)): ans = min(groups[i-1], groups[i]) return ans
引用:class Solution(object): def countBinarySubstrings(self, s): """ :type s: str :rtype: int """ self.flag=0 self.count=1 self.res=0 for i in range(len(s)): self.flag = 0 self.count = 1 j=i while(j 1<len(s) and s[j]==s[j+1]): j=j+1 self.count=1 self.count k=0 if(j-self.count>=0): self.flag=1 while(k<self.count and j-1<len(s)): if(s[i]==s[j-1]) : self.flag=1 break j=j-1 k=k+1 if (self.flag==0): self.res=self.res+1 return self.res s=Solution() print(s.countBinarySubstrings("00000000000000000"))
根据引用和引用,这是两个使用Python解决LeetCode题目696的解法。引用是一个类Solution的方法countBinarySubstrings,使用了贪心算法来计算二进制子字符串的数量。引用是另一个类Solution的方法countBinarySubstrings,使用了暴力解法来计算二进制子字符串的数量。这两个解法都可以在本地运行并得到正确的结果,但是在LeetCode上可能会超时。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [用 Python 解决旅行商问题的 模拟退火算法_python_代码_下载](https://download.csdn.net/download/qq_38334677/85720879)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [python--leetcode696. Count Binary Substrings](https://blog.csdn.net/wenqiwenqi123/article/details/78462141)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文