class Solution {
public boolean checkOnesSegment(String s) {
String[] a = s.split("0");
return a.length==1;
}
}
第二种解法,以01进行判断
class Solution {
public boolean checkOnesSegment(String s) {
int n = s.length(), cnt = 0;//指针位置
for(int i = 0; i < n; ++i){
if(cnt >= 1 && s.charAt(i) == '1' && s.charAt(i-1) == '0') return false; //有1的情况下,判断再次出现1的时候是否和前面是连续的
if(s.charAt(i) == '1'){//前面有1了,
cnt++;
}
}
return true;
}
}