<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 范圍內沒有重復數字的總數 > 原文: [https://www.geeksforgeeks.org/total-numbers-no-repeated-digits-range/](https://www.geeksforgeeks.org/total-numbers-no-repeated-digits-range/) 給定一個范圍![L, R](https://img.kancloud.cn/82/52/82520ea88f7d7e2066cb0817d42d9e54_49x24.png "Rendered by QuickLaTeX.com"),可以找到給定范圍內的總數,以使它們沒有重復的數字。 例如: 12 沒有重復的數字。 22 具有重復的數字。 102、194 和 213 沒有重復的數字。 212、171 和 4004 具有重復的數字。 **示例**: ``` Input : 10 12 Output : 2 Explanation : In the given range 10 and 12 have no repeated digit where as 11 has repeated digit. Input : 1 100 Output : 90 ``` **暴力** 我們將遍歷給定范圍內的每個元素,并對沒有重復數字的數字進行計數。 ## C++ ```cpp // C++ implementation of brute // force solution. #include <bits/stdc++.h> using namespace std; // Function to check if the given // number has repeated digit or not int repeated_digit(int n) { ????unordered_set<int> s; ????// Traversing through each digit ????while(n != 0) ????{ ????????int d = n % 10; ????????// if the digit is present ????????// more than once in the ????????// number ????????if(s.find(d) != s.end()) ????????{ ????????????// return 0 if the number ????????????// has repeated digit ????????????return 0; ????????} ????????s.insert(d); ????????n = n / 10; ????} ????// return 1 if the number has? ????// no repeated digit ????return 1; } // Function to find total number // in the given range which has // no repeated digit int calculate(int L,int R) { ????int answer = 0; ????// Traversing through the range ????for(int i = L; i < R + 1; ++i) ????{ ????????// Add 1 to the answer if i has ????????// no repeated digit else 0 ????????answer = answer + repeated_digit(i); ????} ????return answer ; } // Driver Code int main() { ????int L = 1, R = 100; ????// Calling the calculate ????cout << calculate(L, R); ????return 0; } // This code is contributed by // Sanjit_Prasad ``` ## Java ```java // Java implementation of brute? // force solution.? import java.util.LinkedHashSet; class GFG { // Function to check if the given? // number has repeated digit or not? static int repeated_digit(int n)? { ????LinkedHashSet<Integer> s = new LinkedHashSet<>(); ????// Traversing through each digit? ????while (n != 0)? ????{ ????????int d = n % 10; ????????// if the digit is present? ????????// more than once in the? ????????// number? ????????if (s.contains(d)) ????????{ ????????????// return 0 if the number? ????????????// has repeated digit? ????????????return 0; ????????} ????????s.add(d); ????????n = n / 10; ????} ????// return 1 if the number has? ????// no repeated digit? ????return 1; } // Function to find total number? // in the given range which has? // no repeated digit? static int calculate(int L, int R)? { ????int answer = 0; ????// Traversing through the range? ????for (int i = L; i < R + 1; ++i)? ????{ ????????// Add 1 to the answer if i has? ????????// no repeated digit else 0? ????????answer = answer + repeated_digit(i); ????} ????return answer; } // Driver Code? public static void main(String[] args)? { ????int L = 1, R = 100; ????// Calling the calculate? ????System.out.println(calculate(L, R)); } } // This code is contributed by RAJPUT-JI ``` ## Python3 ```py # Python implementation of brute? # force solution. # Function to check if the given? # number has repeated digit or not? def repeated_digit(n): ????a = [] ????# Traversing through each digit ????while n != 0: ????????d = n%10 ????????# if the digit is present ????????# more than once in the ????????# number ????????if d in a: ????????????# return 0 if the number ????????????# has repeated digit ????????????return 0 ????????a.append(d) ????????n = n//10 ????# return 1 if the number has no ????# repeated digit ????return 1 # Function to find total number # in the given range which has? # no repeated digit def calculate(L,R): ????answer = 0 ????# Traversing through the range ????for i in range(L,R+1): ????????# Add 1 to the answer if i has ????????# no repeated digit else 0 ????????answer = answer + repeated_digit(i) ????# return answer ????return answer # Driver's Code? L=1 R=100 # Calling the calculate print(calculate(L, R)) ``` ## C# ```cs // C# implementation of brute? // force solution.? using System;? using System.Collections.Generic;? class GFG? { // Function to check if the given? // number has repeated digit or not? static int repeated_digit(int n) { ????var s = new HashSet<int>(); ????// Traversing through each digit? ????while (n != 0) ????{ ????????int d = n % 10; ????????// if the digit is present? ????????// more than once in the? ????????// number? ????????if (s.Contains(d))? ????????{ ????????????// return 0 if the number? ????????????// has repeated digit? ????????????return 0; ????????} ????????s.Add(d); ????????n = n / 10; ????} ????// return 1 if the number has? ????// no repeated digit? ????return 1; } // Function to find total number? // in the given range which has? // no repeated digit? static int calculate(int L, int R)? { ????int answer = 0; ????// Traversing through the range? ????for (int i = L; i < R + 1; ++i)? ????{ ????????// Add 1 to the answer if i has? ????????// no repeated digit else 0? ????????answer = answer + repeated_digit(i); ????} ????return answer; } // Driver Code? public static void Main(String[] args) { ????int L = 1, R = 100; ????// Calling the calculate? ????Console.WriteLine(calculate(L, R)); } } // This code is contributed by RAJPUT-JI ``` ## PHP ```php <?php // PHP implementation of? // brute force solution. // Function to check if? // the given number has // repeated digit or not? function repeated_digit($n) { ????$c = 10; ????$a = array_fill(0, $c, 0); ????// Traversing through? ????// each digit ????while($n > 0) ????{ ????????$d = $n % 10; ????????// if the digit is present ????????// more than once in the ????????// number ????????if ($a[$d] > 0) ????????{ ????????????// return 0 if the number ????????????// has repeated digit ????????????return 0; ????????} ????????$a[$d]++; ????????$n = (int)($n / 10); ????} ????// return 1 if the number? ????// has no repeated digit ????return 1; } // Function to find total? // number in the given range? // which has no repeated digit function calculate($L, $R) { ????$answer = 0; ????// Traversing through ????// the range ????for($i = $L; $i <= $R; $i++) ????{ ????????// Add 1 to the answer if? ????????// i has no repeated digit ????????// else 0 ????????$answer += repeated_digit($i); ????} ????// return answer ????return $answer; }? // Driver Code? $L = 1; $R = 100; // Calling the calculate echo calculate($L, $R); // This code is contributed by mits ?> ``` **輸出**: ``` 90 ``` 此方法將在 **`O(n)`**時間內回答每個查詢。 **有效方法** 我們將計算沒有重復數字的數字的前綴數組。 `Prefix[i] =`沒有重復位數小于或等于 1 的總數。 因此,每個查詢都可以在`O(1)`時間內解決。 ![ Answer = Prefix[R] - Prefix[L-1]](https://img.kancloud.cn/f5/5d/f55d864065570062c611a577fdd9a7d7_441x28.png "Rendered by QuickLaTeX.com") **下面是上述想法的實現。** ## C++ ``` // C++ implementation of above idea? #include <bits/stdc++.h>? using namespace std; // Maximum? int MAX = 1000; // Prefix Array? vector<int> Prefix = {0}; // Function to check if the given? // number has repeated digit or not? int repeated_digit(int n) { ????unordered_set<int> a; ????int d; ????// Traversing through each digit? ????while (n != 0) ????{ ????????d = n % 10; ????????// if the digit is present? ????????// more than once in the? ????????// number? ????????if (a.find(d) != a.end())? ????????????// return 0 if the number? ????????????// has repeated digit? ????????????return 0; ????????a.insert(d);? ????????n = n / 10; ????} ????// return 1 if the number has no? ????// repeated digit? ????return 1; } // Function to pre calculate? // the Prefix array? void pre_calculation(int MAX) { ????Prefix.push_back(repeated_digit(1)); ????// Traversing through the numbers? ????// from 2 to MAX? ????for (int i = 2; i < MAX + 1; i++)? ????????// Generating the Prefix array? ????????Prefix.push_back(repeated_digit(i) + Prefix[i-1]); } // Calclute Function? int calculate(int L,int R) {? ????// Answer? ????return Prefix[R] - Prefix[L-1]; } // Driver code int main() { ????int L = 1, R = 100; ????// Pre-calculating the Prefix array.? ????pre_calculation(MAX); ????// Calling the calculate function? ????// to find the total number of number? ????// which has no repeated digit? ????cout << calculate(L, R) << endl;? ????return 0; } // This code is contributed by Rituraj Jain ``` ## Java ```java // Java implementation of above idea import java.util.*; class GFG? { ????// Maximum ????static int MAX = 100; ????// Prefix Array ????static Vector<Integer> Prefix = new Vector<>(); ????// Function to check if the given ????// number has repeated digit or not ????static int repeated_digit(int n) ????{ ????????HashSet<Integer> a = new HashSet<>(); ????????int d; ????????// Traversing through each digit ????????while (n != 0)? ????????{ ????????????d = n % 10; ????????????// if the digit is present ????????????// more than once in the ????????????// number ????????????if (a.contains(d)) ????????????????// return 0 if the number ????????????????// has repeated digit ????????????????return 0; ????????????a.add(d); ????????????n /= 10; ????????} ????????// return 1 if the number has no ????????// repeated digit ????????return 1; ????} ????// Function to pre calculate ????// the Prefix array ????static void pre_calculations()? ????{ ????????Prefix.add(0); ????????Prefix.add(repeated_digit(1)); ????????// Traversing through the numbers ????????// from 2 to MAX ????????for (int i = 2; i < MAX + 1; i++) ????????????// Generating the Prefix array ????????????Prefix.add(repeated_digit(i) + Prefix.elementAt(i - 1)); ????} ????// Calclute Function ????static int calculate(int L, int R) ????{ ????????// Answer ????????return Prefix.elementAt(R) - Prefix.elementAt(L - 1); ????} ????// Driver Code ????public static void main(String[] args) ????{ ????????int L = 1, R = 100; ????????// Pre-calculating the Prefix array. ????????pre_calculations(); ????????// Calling the calculate function ????????// to find the total number of number ????????// which has no repeated digit ????????System.out.println(calculate(L, R)); ????} } // This code is contributed by // sanjeev2552 ``` ## Python3 ```py # Python implementation of? # above idea # Prefix Array Prefix = [0] # Function to check if? # the given number has? # repeated digit or not? def repeated_digit(n): ????a = [] ????# Traversing through each digit ????while n != 0: ????????d = n%10 ????????# if the digit is present ????????# more than once in the ????????# number ????????if d in a: ????????????# return 0 if the number ????????????# has repeated digit ????????????return 0 ????????a.append(d) ????????n = n//10 ????# return 1 if the number has no ????# repeated digit ????return 1 # Function to pre calculate # the Prefix array def pre_calculation(MAX): ????# To use to global Prefix array ????global Prefix ????Prefix.append(repeated_digit(1)) ????# Traversing through the numbers ????# from 2 to MAX ????for i in range(2,MAX+1): ????????# Generating the Prefix array? ????????Prefix.append( repeated_digit(i) + ???????????????????????Prefix[i-1] ) # Calclute Function def calculate(L,R): ????# Answer ????return Prefix[R]-Prefix[L-1] # Driver Code # Maximum? MAX = 1000 # Pre-calculating the Prefix array. pre_calculation(MAX) # Range L=1 R=100 # Calling the calculate function # to find the total number of number # which has no repeated digit print(calculate(L, R)) ``` ## C# ``` // C# implementation of above idea using System; using System.Collections.Generic; class GFG? { ????// Maximum ????static int MAX = 100; ????// Prefix Array ????static List<int> Prefix = new List<int>(); ????// Function to check if the given ????// number has repeated digit or not ????static int repeated_digit(int n) ????{ ????????HashSet<int> a = new HashSet<int>(); ????????int d; ????????// Traversing through each digit ????????while (n != 0)? ????????{ ????????????d = n % 10; ????????????// if the digit is present ????????????// more than once in the ????????????// number ????????????if (a.Contains(d)) ????????????????// return 0 if the number ????????????????// has repeated digit ????????????????return 0; ????????????a.Add(d); ????????????n /= 10; ????????} ????????// return 1 if the number has no ????????// repeated digit ????????return 1; ????} ????// Function to pre calculate ????// the Prefix array ????static void pre_calculations()? ????{ ????????Prefix.Add(0); ????????Prefix.Add(repeated_digit(1)); ????????// Traversing through the numbers ????????// from 2 to MAX ????????for (int i = 2; i < MAX + 1; i++) ????????????// Generating the Prefix array ????????????Prefix.Add(repeated_digit(i) +? ???????????????????????????Prefix[i - 1]); ????} ????// Calclute Function ????static int calculate(int L, int R) ????{ ????????// Answer ????????return Prefix[R] - Prefix[L - 1]; ????} ????// Driver Code ????public static void Main(String[] args) ????{ ????????int L = 1, R = 100; ????????// Pre-calculating the Prefix array. ????????pre_calculations(); ????????// Calling the calculate function ????????// to find the total number of number ????????// which has no repeated digit ????????Console.WriteLine(calculate(L, R)); ????} } // This code is contributed by 29AjayKumar ``` **輸出**: ``` 90 ``` * * * * * *
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看