New Problem Solution - "Check if Binary String Has at Most One Segment of Ones"

This commit is contained in:
Hao Chen 2021-03-07 12:35:35 +08:00
parent d358ec02e8
commit d2dd2048b4
2 changed files with 38 additions and 0 deletions

View File

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

View File

@ -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<s.size() && s[i]=='1') i++;
while(i<s.size() && s[i]=='0') i++;
if(i<s.size() && s[i]=='1') return false;
return true;
}
};