Newyorkcity
V2EX  ›  问与答

请帮我看看为啥我这个代码不对呢?

  •  
  •   Newyorkcity · May 18, 2020 · 903 views
    This topic created in 2215 days ago, the information mentioned may be changed or developed.

    leetcode 1012

    public class Solution {
        public int numDupDigitsAtMostN(int N) {
            if (N < 11) return 0;
            String Nstr = String.valueOf(N);
            int Nlength = Nstr.length();
            int nonrepeatableNumTotal = 0;
    
            //首先数清楚位数小于 N 的数中发生了重复的数
            //位数等于 n 时,最高位也就是第一位只有 9 种选择(不能选 0 )但
            //但之后的 n-1 位可从(10-1)个数字中任一选择
            for (int i = 0; i + 1 < Nlength; i++) {
                nonrepeatableNumTotal += (9 * A(i, 9));
            }
    
    
            //然后数清楚和给出的数据等位数的数里有发生了重复的数
            //由于规定了位数要相同,所以最高位即 i==1 时不能取 0,故有 t-1
            for (int i = 0; i < Nlength; i++) {
                int t = Nstr.charAt(i) - '0';
                if (i == 0) {
                    nonrepeatableNumTotal += ((t - 1) * A(Nlength - 1, 9));
                } else {
                    nonrepeatableNumTotal += (t * A(Nlength - 1 - i, 9 - i));
                }
            }
    
            int used = 0;
            for (char c : Nstr.toCharArray()) {
                int t = (1 << (c - '0'));
                if ((used & t) == 0) {
                    used |= t;
                } else {
                    return N - nonrepeatableNumTotal + 1;
                }
            }
    
            return N - nonrepeatableNumTotal;
        }
    
        private int factorial(int n) {
            if (n == 1 || n == 0) return 1;
            return n * factorial(n - 1);
        }
    
        private int A(int m, int n) {
            return factorial(n) / factorial(n - m);
        }
    }
    

    谢谢

    1 replies    2020-05-18 19:30:44 +08:00
    lysS
        1
    lysS  
       May 18, 2020
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1179 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 17:58 · PVG 01:58 · LAX 10:58 · JFK 13:58
    ♥ Do have faith in what you're doing.