ARTS Week 42
Algorithm 本周的算法题为 2283. 判断一个数的数字计数是否等于数位的值 给你一个下标从 0 开始长度为 n 的字符串 num ,它只包含数字。 如果对于 每个 0 <= i < n 的下标 i ,都满足数位 i 在 num 中出现了 num[i]次,那么请你返回 true ,否则返回 false 。 示例 1: 解释: num[0] = '1' 。数字 0 在 num 中出现了一次。 num[1] = '2' 。数字 1 在 num 中出现了两次。 num[2] = '1' 。数字 2 在 num 中出现了一次。 num[3] = '0' 。数字 3 在 num 中出现了零次。 "1210" 满足题目要求条件,所以返回 true 。 实现代码如下: const digitCount = function (num) { for (let i = 0; i < num.length; i++) { const n = num[i]; console.log({ i, n }); if (Number(n) !== getCount(i, num)) { return false } } return true function getCount(char, s) { let count = 0 let postion = s.indexOf(char) while (postion !==-1) { count++ postion = s.indexOf(char, postion + 1) } return count } }; 解题思路: 只需求得下标字符i在字符串中出现的次数与num[i]相等即可。 Review Marmoset monkeys call each other by name - Breaking News English Lesson ...