Registry Mirror
搜索
Skip to content
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Files
Expand file tree
master
Breadcrumbs
leetcode
/
algorithms
/
cpp
/
checkIfBinaryStringHasAtMostOneSegmentOfOnes
/
CheckIfBinaryStringHasAtMostOneSegmentOfOnes.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
37 lines (35 loc) · 1 KB
master
Breadcrumbs
leetcode
/
algorithms
/
cpp
/
checkIfBinaryStringHasAtMostOneSegmentOfOnes
/
CheckIfBinaryStringHasAtMostOneSegmentOfOnes.cpp
Copy path
Top
File metadata and controls
Code
Blame
37 lines (35 loc) · 1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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;
}
};
You can’t perform that action at this time.