Skip to content

Latest commit

 

History

History
192 lines (162 loc) · 4.41 KB

File metadata and controls

192 lines (162 loc) · 4.41 KB

中文文档

Description

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 of 1s has length 2, and the longest continuous segment of 0s has length 3.

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 <= 100
  • s[i] is either '0' or '1'.

Solutions

Python3

class Solution:
    def checkZeroOnes(self, s: str) -> bool:
        n0 = n1 = 0
        t0 = t1 = 0
        for c in s:
            if c == '0':
                t0 += 1
                t1 = 0
            else:
                t0 = 0
                t1 += 1
            n0 = max(n0, t0)
            n1 = max(n1, t1)
        return n1 > n0

Java

class Solution {
    public boolean checkZeroOnes(String s) {
        int n0 = 0, n1 = 0;
        int t0 = 0, t1 = 0;
        for (int i = 0; i < s.length(); ++i) {
            if (s.charAt(i) == '0') {
                ++t0;
                t1 = 0;
            } else {
                ++t1;
                t0 = 0;
            }
            n0 = Math.max(n0, t0);
            n1 = Math.max(n1, t1);
        }
        return n1 > n0;
    }
}

JavaScript

/**
 * @param {string} s
 * @return {boolean}
 */
var checkZeroOnes = function (s) {
    let max0 = 0,
        max1 = 0;
    let t0 = 0,
        t1 = 0;
    for (let char of s) {
        if (char == '0') {
            t0++;
            t1 = 0;
        } else {
            t1++;
            t0 = 0;
        }
        max0 = Math.max(max0, t0);
        max1 = Math.max(max1, t1);
    }
    return max1 > max0;
};

C++

class Solution {
public:
    bool checkZeroOnes(string s) {
        int n0 = 0, n1 = 0;
        int t0 = 0, t1 = 0;
        for (auto c : s)
        {
            if (c == '0')
            {
                ++t0;
                t1 = 0;
            }
            else
            {
                ++t1;
                t0 = 0;
            }
            n0 = max(n0, t0);
            n1 = max(n1, t1);
        }
        return n1 > n0;
    }
};

Go

func checkZeroOnes(s string) bool {
	n0, n1 := 0, 0
	t0, t1 := 0, 0
	for _, c := range s {
		if c == '0' {
			t0++
			t1 = 0
		} else {
			t1++
			t0 = 0
		}
		n0 = max(n0, t0)
		n1 = max(n1, t1)
	}
	return n1 > n0
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

...