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

leetcode-2283-easy

时间:2022-11-04 19:24:43浏览次数:54  
标签:map Digit charAt int 2283 length num easy leetcode

Check if Number Has Equal Digit Count and Digit Value

You are given a 0-indexed string num of length n consisting of digits.

Return true if for every index i in the range 0 <= i < n, the digit i occurs num[i] times in num, otherwise return false.

Example 1:

Input: num = "1210"
Output: true
Explanation:
num[0] = '1'. The digit 0 occurs once in num.
num[1] = '2'. The digit 1 occurs twice in num.
num[2] = '1'. The digit 2 occurs once in num.
num[3] = '0'. The digit 3 occurs zero times in num.
The condition holds true for every index in "1210", so return true.
Example 2:

Input: num = "030"
Output: false
Explanation:
num[0] = '0'. The digit 0 should occur zero times, but actually occurs twice in num.
num[1] = '3'. The digit 1 should occur three times, but actually occurs zero times in num.
num[2] = '0'. The digit 2 occurs zero times in num.
The indices 0 and 1 both violate the condition, so return false.
Constraints:

n == num.length
1 <= n <= 10
num consists of digits.

思路一:map 存储,然后遍历,没有难度,就是题目稍微有点绕

public boolean digitCount(String num) {
    Map<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < num.length(); i++) {
        map.compute(num.charAt(i) - '0', (k, v) -> v == null ? 1 : v + 1);
    }

    for (int i = 0; i < num.length(); i++) {
        if (map.getOrDefault(i, 0) != num.charAt(i) - '0') {
            return false;
        }
    }

    return true;
}

标签:map,Digit,charAt,int,2283,length,num,easy,leetcode
From: https://www.cnblogs.com/iyiluo/p/16858855.html

相关文章

  • leetcode-754-medium
    ReachaNumberYouarestandingatposition0onaninfinitenumberline.Thereisadestinationatpositiontarget.YoucanmakesomenumberofmovesnumMove......
  • [oeasy]python0010_hello_world_unix_c历史迷因
    ​ HelloWorld!回忆上次内容我们这次设置了断点设置断点的目的是更快地调试调试的目的是去除​​bug​​别害怕​​bug​​一步步地总能找到​​bug​​......
  • leetcode-657-easy
    RobotReturntoOriginThereisarobotstartingattheposition(0,0),theorigin,ona2Dplane.Givenasequenceofitsmoves,judgeifthisrobotendsup......
  • leetcode-1417-easy
    ReformatTheStringYouaregivenanalphanumericstrings.(AlphanumericstringisastringconsistingoflowercaseEnglishlettersanddigits).Youhaveto......
  • leetcode-771-easy
    JewelsandStonesYou'regivenstringsjewelsrepresentingthetypesofstonesthatarejewels,andstonesrepresentingthestonesyouhave.Eachcharacterin......
  • leetcode-1200-easy
    MinimumAbsoluteDifferenceGivenanarrayofdistinctintegersarr,findallpairsofelementswiththeminimumabsolutedifferenceofanytwoelements.Retu......
  • leetcode-1984-easy
    MinimumDifferenceBetweenHighestandLowestofKScoresYouaregivena0-indexedintegerarraynums,wherenums[i]representsthescoreoftheithstudent.......
  • EasyCVR国标GB28181协议接入下的TCP和UDP模式说明及差异
    有用户在使用我们的平台时,经常会出现对于端口的疑问,同时也不了解端口的差别。今天我们来解释说明下EasyCVR平台关于国标GB28181协议接入下的TCP和UDP模式的说明及差异。......
  • 视频融合平台EasyCVR各项数据正常,却无法用海康NVR接入是什么原因?
    EasyCVR视频融合云平台开放度高、兼容性强、可支持灵活拓展与第三方集成,目前已经成为安防市场主流的视频能力层服务平台。平台可支持多协议、多类型的设备接入,包括国标GB28......
  • 我用EasyExcel优化了公司的导出(附踩坑记录)
    背景介绍最近要改一个导出的功能,在原有的基础上,在导出一份明细数据,要求导出内容加在原有excel的第二个sheet上。考虑到数据量还比较大,干脆引入阿里的EasyExcel来做......