原文链接: https://leetcode-cn.com/problems/longer-contiguous-segments-of-ones-than-zeros
英文原文
Given a binary string s, return true if the longest contiguous segment of 1's is strictly longer than the longest contiguous segment of 0's in s, or return false otherwise.
- For example, in
s = "110100010"the longest continuous segment of1s has length2, and the longest continuous segment of0s has length3.
Note that if there are no 0's, then the longest continuous segment of 0's is considered to have a length 0. The same applies if there is no 1's.
Example 1:
Input: s = "1101" Output: true Explanation: The longest contiguous segment of 1s has length 2: "1101" The longest contiguous segment of 0s has length 1: "1101" The segment of 1s is longer, so return true.
Example 2:
Input: s = "111000" Output: false Explanation: The longest contiguous segment of 1s has length 3: "111000" The longest contiguous segment of 0s has length 3: "111000" The segment of 1s is not longer, so return false.
Example 3:
Input: s = "110100010" Output: false Explanation: The longest contiguous segment of 1s has length 2: "110100010" The longest contiguous segment of 0s has length 3: "110100010" The segment of 1s is not longer, so return false.
Constraints:
1 <= s.length <= 100s[i]is either'0'or'1'.
中文题目
给你一个二进制字符串 s 。如果字符串中由 1 组成的 最长 连续子字符串 严格长于 由 0 组成的 最长 连续子字符串,返回 true ;否则,返回 false 。
- 例如,
s = "110100010"中,由1组成的最长连续子字符串的长度是2,由0组成的最长连续子字符串的长度是3。
注意,如果字符串中不存在 0 ,此时认为由 0 组成的最长连续子字符串的长度是 0 。字符串中不存在 1 的情况也适用此规则。
示例 1:
输入:s = "1101" 输出:true 解释: 由1组成的最长连续子字符串的长度是 2:"1101" 由0组成的最长连续子字符串的长度是 1:"1101" 由 1 组成的子字符串更长,故返回 true 。
示例 2:
输入:s = "111000" 输出:false 解释: 由1组成的最长连续子字符串的长度是 3:"111000" 由0组成的最长连续子字符串的长度是 3:"111000" 由 1 组成的子字符串不比由 0 组成的子字符串长,故返回 false 。
示例 3:
输入:s = "110100010" 输出:false 解释: 由1组成的最长连续子字符串的长度是 2:"110100010" 由0组成的最长连续子字符串的长度是 3:"110100010" 由 1 组成的子字符串不比由 0 组成的子字符串长,故返回 false 。
提示:
1 <= s.length <= 100s[i]不是'0'就是'1'
通过代码
高赞题解
解题思路
此处撰写解题思路
代码
class Solution {
public boolean checkZeroOnes(String s) {
int l=s.length();
int m=0,resm=0;
int n=0,resn=0;
for(int i=0;i<l;i++){
if(s.charAt(i) == '1'){
m++;
resm=Math.max(m,resm);
n=0;
}else{
n++;
m=0;
resn=Math.max(n,resn);
}
}
return resm>resn? true:false;
}
}
统计信息
| 通过次数 | 提交次数 | AC比率 |
|---|---|---|
| 13215 | 17018 | 77.7% |
提交历史
| 提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
|---|