当前位置:网站首页>Leetcode skimming -- count the number of numbers with different numbers 357 medium
Leetcode skimming -- count the number of numbers with different numbers 357 medium
2022-07-02 15:28:00 【Fire breathing dragon and water arrow turtle】
The train of thought and source code of counting the number of different numbers
The topic of counting the number of different figures is shown in the figure below , This problem belongs to mathematics and backtracking type , It mainly examines the use of backtracking methods and the understanding of mathematical ideas of the topic . The title of this article, the author thought 2 Methods , They are permutation and combination method and recursive method , The permutation and combination method uses Java Compiling , The recursive method uses Python Compiling , Of course, this may not be the optimal solution , I also hope you guys can give a faster algorithm .
I think this problem can be solved by using the idea of permutation and combination , First, judge whether the number is 0 perhaps 1, If it is 0 Just go back to 1; If it is 1 Just go back to 10, Then start initializing the result variables and subscript variables . Start traversing the loop , Record the temporary arrangement result with the current subscript result , Then add it to the result variable , Until the end of the traversal returns the final result . Then according to this idea, our Java The code is as follows :
# Fire breathing dragon and water arrow turtle
class Solution {
public int countNumbersWithUniqueDigits(int n) {
if (n == 0) {
return 1;
}
if (n == 1) {
return 10;
}
int resFinal = 10;
int ind = 9;
for (int jr = 0; jr < n - 1; jr++) {
ind = ind * (9 - jr);
resFinal = resFinal + ind;
}
return resFinal;
}
}

obviously , We can see that the effect of the permutation row combination method is ok , At the same time, we can also use the recursive method to solve . First, initialize a list , And the assignment 9 individual 0 Is the initialization result . Let the first list element be 1, The second list element is 10, Then start traversing the loop from the second element , After deduction and summary, use mathematical recurrence formula to calculate , Get the traversal result and return the element of the last list . So according to this idea, we can solve , Here is Python Code :
# Fire breathing dragon and water arrow turtle
class Solution:
def countNumbersWithUniqueDigits(self, n: int) -> int:
vc = []
for ij in range(9):
vc.append(0)
vc[0] = 1
vc[1] = 10
for jr in range(2,9):
vc[jr] = vc[jr-1] + (vc[jr-1]-vc[jr-2])*(10-(jr-1))
return vc[n]

As a result Java The efficiency of the permutation and combination method of version is good , and Python The speed of the recursive method of version is also ok , But there should be more ways to further speed up , I hope friends can give me more advice , Thank you very much .
边栏推荐
- Niuke Practice 101
- 数据分析常见的英文缩写(一)
- Huffman tree: (1) input each character and its weight (2) construct Huffman tree (3) carry out Huffman coding (4) find hc[i], and get the Huffman coding of each character
- CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
- kibana 基础操作
- Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
- Principles, language, compilation, interpretation
- 原则、语言、编译、解释
- Build your own semantic segmentation platform deeplabv3+
- Tidb data migration tool overview
猜你喜欢

XML Configuration File

MySQL calculate n-day retention rate

LeetCode刷题——统计各位数字都不同的数字个数#357#Medium

16_Redis_Redis持久化

How does the computer set up speakers to play microphone sound

17_Redis_Redis发布订阅

18_ Redis_ Redis master-slave replication & cluster building

数据分析思维分析方法和业务知识——业务指标

Mavn 搭建 Nexus 私服

Solution of Queen n problem
随机推荐
Build your own semantic segmentation platform deeplabv3+
TiDB跨数据中心部署拓扑
Points clés de l'examen de principe de compilation pour l'année scolaire 2021 - 2022 [Université chinoise d'outre - mer]
10_Redis_geospatial_命令
Yolov5 code reproduction and server operation
记一次面试
06_ Stack and queue conversion
TiDB数据迁移工具概览
如何对 TiDB 进行 TPC-C 测试
LeetCode刷题——验证二叉树的前序序列化#331#Medium
[solution] educational codeforces round 82
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
Solve the problem of frequent interruption of mobaxterm remote connection
Solve the problem that El radio group cannot be edited after echo
Map介绍
Common English abbreviations for data analysis (I)
The past and present lives of visual page building tools
15_Redis_Redis.conf详解
Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
I made an istio workshop. This is the first introduction