[LeetCode] 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
- 난이도: Easy
- 태그: 문자열
- 결과:
Time: 0 ms (100%), Space: 8.3 MB (10.47%)
풀이
class Solution {
public:
bool checkOnesSegment(string s) {
bool escape = false;
for (int i = 0; i < s.size(); ++i)
{
if (s[i] == '0')
{
escape = true;
}
else if (escape)
{
return false;
}
}
return true;
}
};
연속된 1이 끊겼다가 다시 나오는 순간 false를 반환하면 된다.
배운 점과 후기
at most는 ‘최대로’, ‘많아야’ 정도로 번역되는 표현이다. 그러므로 문제에서 at most one contiguous segment of ones 는 연속된 1 구간이 많아야 1번 나온다는 의미이다.
댓글남기기