| comments | true | |
|---|---|---|
| difficulty | 简单 | |
| edit_url | https://github.com/doocs/leetcode/edit/main/solution/1700-1799/1784.Check%20if%20Binary%20String%20Has%20at%20Most%20One%20Segment%20of%20Ones/README.md | |
| rating | 1206 | |
| source | 第 231 场周赛 Q1 | |
| tags |
|
给你一个二进制字符串 s ,该字符串 不含前导零 。
如果 s 包含 零个或一个由连续的 '1' 组成的字段 ,返回 true 。否则,返回 false 。
示例 1:
输入:s = "1001"
输出:false
解释:由连续若干个 '1' 组成的字段数量为 2,返回 false
示例 2:
输入:s = "110" 输出:true
提示:
1 <= s.length <= 100s[i] 为'0'或'1's[0]为'1'
注意到字符串
若字符串 false。
若字符串 true。
因此,只需要判断字符串
时间复杂度
class Solution:
def checkOnesSegment(self, s: str) -> bool:
return '01' not in sclass Solution {
public boolean checkOnesSegment(String s) {
return !s.contains("01");
}
}class Solution {
public:
bool checkOnesSegment(string s) {
return s.find("01") == -1;
}
};func checkOnesSegment(s string) bool {
return !strings.Contains(s, "01")
}function checkOnesSegment(s: string): boolean {
let pre = s[0];
for (const c of s) {
if (pre !== c && c === '1') {
return false;
}
pre = c;
}
return true;
}impl Solution {
pub fn check_ones_segment(s: String) -> bool {
!s.contains("01")
}
}function checkOnesSegment(s: string): boolean {
return !s.includes('01');
}