# Code language: Python classSolution: defcheckOnesSegment(self, s: str) -> bool: tag = False for i in s: if i == '0': tag = True if tag and i == '1': returnFalse returnTrue
1 2 3 4 5 6 7 8 9 10 11 12 13
// Code language: Java classSolution { publicbooleancheckOnesSegment(String s) { booleanzero=false; for (inti=0, n = s.length(); i < n; ++i) { if (s.charAt(i) == '1') { if (zero) returnfalse; } else zero = true; } returntrue; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Code language: C++ classSolution { public: boolcheckOnesSegment(string s){ bool zero = false; for (int i = 0, n = s.size(); i < n; ++i) { if (s[i] == '1') { if (zero) returnfalse; } else zero = true; } returntrue; } };