From d2dd2048b459b159bcfabb9bca487c246a3c2a6b Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Sun, 7 Mar 2021 12:35:35 +0800 Subject: [PATCH] New Problem Solution - "Check if Binary String Has at Most One Segment of Ones" --- README.md | 1 + ...fBinaryStringHasAtMostOneSegmentOfOnes.cpp | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 algorithms/cpp/checkIfBinaryStringHasAtMostOneSegmentOfOnes/CheckIfBinaryStringHasAtMostOneSegmentOfOnes.cpp diff --git a/README.md b/README.md index 30a6c3a..8f9b7ae 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ LeetCode | # | Title | Solution | Difficulty | |---| ----- | -------- | ---------- | +|1784|[Check if Binary String Has at Most One Segment of Ones](https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/) | [C++](./algorithms/cpp/checkIfBinaryStringHasAtMostOneSegmentOfOnes/CheckIfBinaryStringHasAtMostOneSegmentOfOnes.cpp)|Easy| |1761|[Minimum Degree of a Connected Trio in a Graph](https://leetcode.com/problems/minimum-degree-of-a-connected-trio-in-a-graph/) | [C++](./algorithms/cpp/minimumDegreeOfAConnectedTrioInAGraph/MinimumDegreeOfAConnectedTrioInAGraph.cpp)|Hard| |1760|[Minimum Limit of Balls in a Bag](https://leetcode.com/problems/minimum-limit-of-balls-in-a-bag/) | [C++](./algorithms/cpp/minimumLimitOfBallsInABag/MinimumLimitOfBallsInABag.cpp)|Medium| |1759|[Count Number of Homogenous Substrings](https://leetcode.com/problems/count-number-of-homogenous-substrings/) | [C++](./algorithms/cpp/countNumberOfHomogenousSubstrings/CountNumberOfHomogenousSubstrings.cpp)|Medium| diff --git a/algorithms/cpp/checkIfBinaryStringHasAtMostOneSegmentOfOnes/CheckIfBinaryStringHasAtMostOneSegmentOfOnes.cpp b/algorithms/cpp/checkIfBinaryStringHasAtMostOneSegmentOfOnes/CheckIfBinaryStringHasAtMostOneSegmentOfOnes.cpp new file mode 100644 index 0000000..407b4e7 --- /dev/null +++ b/algorithms/cpp/checkIfBinaryStringHasAtMostOneSegmentOfOnes/CheckIfBinaryStringHasAtMostOneSegmentOfOnes.cpp @@ -0,0 +1,37 @@ +// Source : https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/ +// Author : Hao Chen +// Date : 2021-03-07 + +/***************************************************************************************************** + * + * Given a binary string s without leading zeros, return true if s contains at most one + * contiguous segment of ones. Otherwise, return false. + * + * Example 1: + * + * Input: s = "1001" + * Output: false + * Explanation: The ones do not form a contiguous segment. + * + * Example 2: + * + * Input: s = "110" + * Output: true + * + * Constraints: + * + * 1 <= s.length <= 100 + * s[i] is either '0' or '1'. + * s[0] is '1'. + ******************************************************************************************************/ + +class Solution { +public: + bool checkOnesSegment(string s) { + int i=0; + while(i