欢迎光临散文网 会员登陆 & 注册

LeetCode 2283. Check if Number Has Equal Digit Count and Digit V

2023-03-21 15:24 作者:您是打尖儿还是住店呢  | 我要投稿

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中,然后遍历map中的数字跟值去判断是否是一样的,如果不一样则返回flag。

  • 简单的题目,但是通过的空间跟时间复杂度都是不好的。。

Runtime: 13 ms, faster than 6.62% of Java online submissions for Check if Number Has Equal Digit Count and Digit Value.

Memory Usage: 42.7 MB, less than 6.74% of Java online submissions for Check if Number Has Equal Digit Count and Digit Value.


LeetCode 2283. Check if Number Has Equal Digit Count and Digit V的评论 (共 条)

分享到微博请遵守国家法律