1869:哪种连续子字符串更长
This commit is contained in:
parent
a4f80ae13f
commit
66aa44dbaa
@ -0,0 +1,94 @@
|
|||||||
|
//给你一个二进制字符串 s 。如果字符串中由 1 组成的 最长 连续子字符串 严格长于 由 0 组成的 最长 连续子字符串,返回 true ;否则,返回 fa
|
||||||
|
//lse 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 例如,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 <= 100
|
||||||
|
// s[i] 不是 '0' 就是 '1'
|
||||||
|
//
|
||||||
|
// Related Topics 数组 双指针
|
||||||
|
// 👍 7 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
//1869:哪种连续子字符串更长
|
||||||
|
public class LongerContiguousSegmentsOfOnesThanZeros{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new LongerContiguousSegmentsOfOnesThanZeros().new Solution();
|
||||||
|
}
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public boolean checkZeroOnes(String s) {
|
||||||
|
if (s.length() == 0 || !s.contains("1")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!s.contains("0")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
int[] lengths = new int[2];
|
||||||
|
int start0 = s.indexOf("0");
|
||||||
|
int start1 = s.indexOf("1");
|
||||||
|
int[] maxs = new int[2];
|
||||||
|
char ch = '0';
|
||||||
|
for (int i = 0; i < s.length(); i++) {
|
||||||
|
if (s.charAt(i) == ch) {
|
||||||
|
lengths[ch - '0']++;
|
||||||
|
} else {
|
||||||
|
maxs[ch - '0'] = Math.max(maxs[ch - '0'], lengths[ch - '0']);
|
||||||
|
lengths[ch - '0'] = 0;
|
||||||
|
ch = s.charAt(i);
|
||||||
|
lengths[ch - '0']++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
maxs[ch - '0'] = Math.max(maxs[ch - '0'], lengths[ch - '0']);
|
||||||
|
return maxs[1]>maxs[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
<p>给你一个二进制字符串 <code>s</code> 。如果字符串中由 <code>1</code> 组成的 <strong>最长</strong> 连续子字符串 <strong>严格长于</strong> 由 <code>0</code> 组成的 <strong>最长</strong> 连续子字符串,返回 <code>true</code> ;否则,返回 <code>false</code><em> </em>。</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>例如,<code>s = "<strong>11</strong>01<strong>000</strong>10"</code> 中,由 <code>1</code> 组成的最长连续子字符串的长度是 <code>2</code> ,由 <code>0</code> 组成的最长连续子字符串的长度是 <code>3</code> 。</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p>注意,如果字符串中不存在 <code>0</code> ,此时认为由 <code>0</code> 组成的最长连续子字符串的长度是 <code>0</code> 。字符串中不存在 <code>1</code> 的情况也适用此规则。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>s = "1101"
|
||||||
|
<strong>输出:</strong>true
|
||||||
|
<strong>解释:</strong>
|
||||||
|
由 <code>1</code> 组成的最长连续子字符串的长度是 2:"<strong>11</strong>01"
|
||||||
|
由 <code>0</code> 组成的最长连续子字符串的长度是 1:"11<strong>0</strong>1"
|
||||||
|
由 1 组成的子字符串更长,故返回 true 。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>s = "111000"
|
||||||
|
<strong>输出:</strong>false
|
||||||
|
<strong>解释:</strong>
|
||||||
|
由 <code>1</code> 组成的最长连续子字符串的长度是 3:"<strong>111</strong>000"
|
||||||
|
由<code> 0</code> 组成的最长连续子字符串的长度是 3:"111<strong>000</strong>"
|
||||||
|
由 1 组成的子字符串不比由 0 组成的子字符串长,故返回 false 。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>s = "110100010"
|
||||||
|
<strong>输出:</strong>false
|
||||||
|
<strong>解释:</strong>
|
||||||
|
由 <code>1</code> 组成的最长连续子字符串的长度是 2:"<strong>11</strong>0100010"
|
||||||
|
由 <code>0</code> 组成的最长连续子字符串的长度是 3:"1101<strong>000</strong>10"
|
||||||
|
由 1 组成的子字符串不比由 0 组成的子字符串长,故返回 false 。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= s.length <= 100</code></li>
|
||||||
|
<li><code>s[i]</code> 不是 <code>'0'</code> 就是 <code>'1'</code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>数组</li><li>双指针</li></div></div>\n<div><li>👍 7</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user