简体   繁体   English

Javascript - 计算没有重复数字的数字 -- leetcode 1012. 有重复数字的数字

[英]Javascript -Count the Number Without Repeated Digit -- leetcode 1012. Numbers With Repeated Digits

https://leetcode.com/problems/numbers-with-repeated-digits/ https://leetcode.com/problems/numbers-with-repeated-digits/

Given a positive integer N, return the number of positive integers less than or equal to N that have at least 1 repeated digit.给定一个正整数 N,返回小于或等于 N 且至少有 1 个重复数字的正整数的个数。

Example 1:示例 1:

Input: 20 Output: 1 Explanation: The only positive number (<= 20) with at least 1 repeated digit is 11. Example 2:输入:20 输出:1 解释:唯一具有至少 1 个重复数字的正数 (<= 20) 是 11。示例 2:

Input: 100 Output: 10 Explanation: The positive numbers (<= 100) with atleast 1 repeated digit are 11, 22, 33, 44, 55, 66, 77, 88, 99, and 100. Example 3:输入:100 输出:10 解释:至少有 1 个重复数字的正数 (<=100) 是 11、22、33、44、55、66、77、88、99 和 100。 示例 3:

Input: 1000 Output: 262输入:1000 输出:262

Note:笔记:

1 <= N <= 10^9 1 <= N <= 10^9

https://leetcode.com/problems/numbers-with-repeated-digits/discuss/256725/JavaPython-Count-the-Number-Without-Repeated-Digit I found out this solution has many likes. https://leetcode.com/problems/numbers-with-repeated-digits/discuss/256725/JavaPython-Count-the-Number-Without-Repeated-Digit我发现这个解决方案有很多人喜欢。 but it's coded in Java/Python.但它是用 Java/Python 编码的。 Anyone can help to use Javascript to code it by the similar logic.任何人都可以帮助使用 Javascript 通过类似的逻辑对其进行编码。 Really appreciate...非常感谢...

python solution: I have no clue how the set() part, how to use Javascript to do it. python 解决方案:我不知道 set() 部分如何,如何使用 Javascript 来做到这一点。

def numDupDigitsAtMostN(self, N): L = map(int, str(N + 1)) res, n = 0, len(L) def numDupDigitsAtMostN(self, N): L = map(int, str(N + 1)) res, n = 0, len(L)

    def A(m, n):
        return 1 if n == 0 else A(m, n - 1) * (m - n + 1)

    for i in range(1, n): res += 9 * A(9, i - 1)
    s = set()
    for i, x in enumerate(L):
        for y in range(0 if i else 1, x):
            if y not in s:
                res += A(9 - i, n - i - 1)
        if x in s: break
        s.add(x)
    return N - res

While I can't translate the python, I think what you need to do is break the integer to array of char, so 100 would become ["1","0","0"], and do a comparison from the 1st element (array[0]) to the last element to see if any of the char is the same.虽然我无法翻译 python,但我认为您需要做的是将整数分解为字符数组,因此 100 将变为 ["1","0","0"],并与第一个进行比较element (array[0]) 到最后一个元素,看看是否有相同的字符。

Here is a quick function that I do.. maybe it will run slower than the python, but it should do the job:这是我做的一个快速功能..也许它会比python运行得慢,但它应该可以完成这项工作:

var result = [];
function x(n) {
  for (var i = 0; i <= n; i++){
    var nStr = i.toString();
    var nStrArr = nStr.split('');
    if(nStrArr.length > 1){
      for(var j = 0; j < nStrArr.length; j++){
        var idx = nStrArr.findIndex(x => x === nStrArr[j]);
        if(idx >= 0 && idx !== j){
          result.push(parseInt(i));
          break;
        }
      }
    }
  }
  console.log(result); //should list all the numbers with at least 1 repeated digit
  console.log(result.length); //length of the array
}

x(1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM