当前位置:网站首页>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 .
边栏推荐
- 原则、语言、编译、解释
- 04_ 栈
- 08_ strand
- . Net again! Happy 20th birthday
- Solve the problem that El radio group cannot be edited after echo
- 18_ Redis_ Redis master-slave replication & cluster building
- How does the computer set up speakers to play microphone sound
- yolo格式数据集处理(xml转txt)
- I made an istio workshop. This is the first introduction
- LeetCode刷题——去除重复字母#316#Medium
猜你喜欢

Steps for Navicat to create a new database

LeetCode刷题——验证二叉树的前序序列化#331#Medium

02_ Linear table_ Sequence table

Evaluation of embedded rz/g2l processor core board and development board of Feiling

Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board

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

How does the computer set up speakers to play microphone sound

19_Redis_宕机后手动配置主机

LeetCode刷题——两整数之和#371#Medium

Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
随机推荐
TiDB跨数据中心部署拓扑
php获取数组中键值最大数组项的索引值的方法
数据分析常见的英文缩写(一)
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
Deploy tidb cluster with tiup
你不知道的Set集合
Case introduction and problem analysis of microservice
List set & UML diagram
yolo格式数据集处理(xml转txt)
TiDB数据迁移工具概览
06_ Stack and queue conversion
LeetCode刷题——验证二叉树的前序序列化#331#Medium
21_Redis_浅析Redis缓存穿透和雪崩
Key points of compilation principle examination in 2021-2022 academic year [overseas Chinese University]
Common English abbreviations for data analysis (I)
LeetCode刷题——去除重复字母#316#Medium
TiDB混合部署拓扑
Internet Explorer officially retired
Force deduction solution summarizes the lucky numbers in 1380 matrix
. Solution to the problem of Chinese garbled code when net core reads files