1869:哪种连续子字符串更长

This commit is contained in:
huangge1199 2021-06-07 13:11:53 +08:00
parent a4f80ae13f
commit 66aa44dbaa
2 changed files with 146 additions and 0 deletions

View File

@ -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)
}

View File

@ -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>