首页 > 其他分享 >leetcode-1784-easy

leetcode-1784-easy

时间:2022-11-08 20:00:15浏览次数:54  
标签:01 return contiguous 1784 easy Input false leetcode

Check if Binary String Has at Most One Segment of Ones

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'.

思路一:刚开始理解错了,意味连续两个'1'才是连续。搞清楚后直接用正则 0+ 分割字符串

public boolean checkOnesSegment(String s) {
    return s.split("0+").length == 1;
}

思路二:看了题解,发现只要判断字符是否包含"01"就行,因为字符已经是"1"开头,所以只要出现"01",说明已经不符合条件

标签:01,return,contiguous,1784,easy,Input,false,leetcode
From: https://www.cnblogs.com/iyiluo/p/16870939.html

相关文章

  • leetcode-819-easy
    MostCommonWordGivenastringparagraphandastringarrayofthebannedwordsbanned,returnthemostfrequentwordthatisnotbanned.Itisguaranteedthe......
  • LeetCode 435. Non-overlapping Intervals
    贪心按照有边界排序,只有先选择了右边边界小的,才可以放下更多的区间classSolution{public:interaseOverlapIntervals(vector<vector<int>>&intervals){......
  • leetcode-1260-easy
    Shift2DGridGivena2Dgridofsizemxnandanintegerk.Youneedtoshiftthegridktimes.Inoneshiftoperation:Elementatgrid[i][j]movestogrid......
  • leetcode-561-easy
    ArrayPartitionGivenanintegerarraynumsof2nintegers,grouptheseintegersintonpairs(a1,b1),(a2,b2),...,(an,bn)suchthatthesumofmin(ai,bi......
  • leetcode-1854-easy
    MaximumPopulationYearYouaregivena2Dintegerarraylogswhereeachlogs[i]=[birthi,deathi]indicatesthebirthanddeathyearsoftheithperson.The......
  • leetcode-1684-easy
    CounttheNumberofConsistentStringsYouaregivenastringallowedconsistingofdistinctcharactersandanarrayofstringswords.Astringisconsistenti......
  • LeetCode 135. Candy
    贪心算法贪心策略:在每次遍历中,仅考虑并更新相邻一侧的大小关系classSolution{public:intcandy(vector<int>&ratings){intsize=ratings.size();......
  • LeetCode 455. Assign Cookies
    贪心classSolution{public:intfindContentChildren(vector<int>&g,vector<int>&s){sort(g.begin(),g.end());sort(s.begin(),s.end());......
  • Leetcode练题系列(六): 字符串相关的算法
    LeetCode  ​​英文官网(推荐)​​  ​​中文官网​​  从2016年大二左右开始就接触算法,起初也简单练习过,但现在工作一段时间后,随着代码水平的提高(​​自我感觉​​)......
  • 视频融合平台EasyCVR首路录像无法播放是什么原因?该如何解决?
    EasyCVR具备较强的视频能力,可支持海量设备接入、汇聚与管理、视频监控、视频录像、云存储、回放与检索、智能告警、平台级联等功能。平台可支持多协议接入,包括:国标GB/T2818......