当前位置:网站首页>Day 9 of leetcode question brushing
Day 9 of leetcode question brushing
2022-07-16 07:50:00 【The sun is falling】
1. Permutation of prime numbers
Please help me to 1 To n Number design arrangement scheme , Make all of 「 Prime number 」 Should be placed in 「 Prime index 」( Index from 1 Start ) On ; You need to return the total number of possible solutions .
Let's review 「 Prime number 」: The prime number must be greater than 1 Of , And it cannot be expressed by the product of two positive integers less than it . Because the answer could be big , So please return to the answer model mod 10^9 + 7 Then the result is .
analysis :
According to the meaning of the question, prime numbers can only be arranged in the position of prime numbers , Then it has a factorial arrangement of prime numbers , Total number of programmes = Factorial of prime number * Factorial of composite numbers . So first we need to know the number of prime numbers , Define a function to determine whether the current number is a prime number , Then count 1 To n The number of prime numbers , Finally, the total number of schemes is obtained .
class Solution {
static final int MOD = 1000000007;
public int numPrimeArrangements(int n) {
int primeNum = 0;
for (int i = 1; i <= n ; i++) {
if (isPrime(i)) primeNum++;
}
long res = 1;
for (int i = primeNum; i > 0; i--) {
res *= i;
res = res%MOD;
}
int other = n - primeNum;
for (int i = other; i > 0; i--) {
res *= i;
res = res%MOD;
}
return (int) res;
}
boolean isPrime(int n){
if (n==1){
return false;
}
else {
for (int i = 2; i < n; i++) {
if (n%i==0){
return false;
}
}
}
return true;
}
}2. A digit in a sequence of numbers
The numbers are in 0123456789101112131415… Serializes the format of a string into a sequence of characters . In this sequence , The first 5 position ( From the subscript 0 Start counting ) yes 5, The first 13 Is it 1, The first 19 Is it 4, wait .
Please write a function , Ask for any n The number corresponding to bit .
analysis :
First of all, we need to find the rules in the number sequence :

Look at the table above , Can launch various digit The number of digits below count Calculation formula :count=9×start×digit .
Based on the above analysis , The solution can be divided into three steps :
- determine n Where Numbers Of digit , Write it down as digit;
- determine n Where Numbers , Write it down as num ;
- determine n yes num Which digit in , And return the result .
class Solution {
public int findNthDigit(int n) {
int digit = 1;
long start = 1;
long count = 9;
while (n > count) { // 1.
n -= count;
digit += 1;
start *= 10;
count = digit * start * 9;
}
long num = start + (n - 1) / digit; // 2.
return Long.toString(num).charAt((n - 1) % digit) - '0'; // 3.
}
}
3. Make the array the smallest number
Enter an array of nonnegative integers , Put all the numbers in the array together to form a number , Print the smallest of all the numbers that can be spliced .
analysis :
Two Numbers x ,y, Is to use String Type saved , If x+y<y+x, Then we should put x In front of , According to this rule, the whole array can be sorted , From small to large , Then splice it into a string and return . The steps are generally :
1. First put the integer array nums Turn into String Array str
2. Use built-in functions to define comparison rules , Yes str Sort
3. Yes, ordered str Traverse , utilize StringBuilder Splice each item
4. Return the spliced value
class Solution {
public String minNumber(int[] nums) {
String[] strs = new String[nums.length];
for(int i = 0; i < nums.length; i++)
strs[i] = String.valueOf(nums[i]);
Arrays.sort(strs, (x, y) -> (x + y).compareTo(y + x));
StringBuilder res = new StringBuilder();
for(String s : strs)
res.append(s);
return res.toString();
}
}边栏推荐
- Are you still using the strategy mode to solve if else? Map+ functional interface method is yyds
- Longest ascending subsequence longest common subsequence maximum field and longest non repeating subsequence
- 26 years old, has worked in automation for three years, and the monthly salary is only 12K. Can you change jobs and find a higher salary job?
- Idea annotation template, such configuration is enough!
- CCF 202012-2 期末预测之最佳阈值
- mysql基础相关(重要)
- Flask基础入门六-上下文
- IDEA 注释模板,这样配置才够逼格!
- Why can't we use redis expiration monitoring to close orders?
- Byte test director stayed up for 10 days, and the test post interview script came out of the liver, giving you wings to your big factory dream~
猜你喜欢

"Finally I left bytek..." confession of a test engineer with an annual salary of 40W

(一)输入输出

接口测试与接口测试自动化

基于SonarQube代码质量检查

Five years' experience: the monthly salary is 3000 to 30000, and the change of Test Engineers

“挤破脑袋进的腾讯,你凭什么要辞职?”

CCF 202012-2 期末预测之最佳阈值

Installing stand-alone redis under Linux

Redis主从集群搭建及哨兵模式配置

字典树
随机推荐
Detailed explanation of sliding window
ABAP Bapi copy the standard project template to achieve project initiation
Installing stand-alone redis under Linux
TCP protocol details
Semaphore, countdownlatch use and talking about the source code
“挤破脑袋进的腾讯,你凭什么要辞职?”
MySQL foundation related (important)
As an interviewer for the test development post, how do I choose people?
How to quickly test the new product just taken over
This should be the most complete software test interview question in the whole network [quick look]~
爬虫——有道翻译
全排列next_permutation()函数
On the meaning of XML file format in the framework
Installing redis on Linux
Dynamic open point segment tree
几行代码就能实现复杂的 Excel 导入导出,这个工具类真心强大!
Decompose a number into the form of adding multiple addends
MySQL learning records
mysql基础相关(重要)
RAID disk array