当前位置:网站首页>Find out the missing numbers from the natural numbers arranged in order from 0 to 100, and the solution provides
Find out the missing numbers from the natural numbers arranged in order from 0 to 100, and the solution provides
2022-06-13 05:38:00 【Flying it people】
The first one is : Dichotomy treatment , It is applicable to the lack of one data and the large amount of data :
public static double searchOne(int [] nums){
int L = 0;
int R = nums.length -1 ;
double lowNum = 0;
while (L<R){
// Normal data
double middle = (nums[R] + nums[L])/2d;
double nowMiddle= 0L;
if(((R - L) + 1) % 2 == 0){
// even numbers
nowMiddle = (nums[(R - L)/2 + L] + nums [(R - L)/2 + 1 + L])/2d;
}
else {
// Odd number
nowMiddle = nums[(R - L )/2 + L];
}
if(nowMiddle == middle){
lowNum = middle;
L = L + 1 ;
R = R - 1;
}
else if (middle > nowMiddle){
L = (R - L)/2 + L;
}
else {
R = (R - L) / 2 ;
}
}
return lowNum;
}
The second kind : Suitable for less data , Lack of partial data , For example, find out the smallest or largest number in the real number :
public static List<Integer> searchMore(int [] nums){
int R = nums.length - 1;
List<Integer> list = new ArrayList<>();
if(nums[R] == R ){
// No missing value
}
else {
for (int i = 0 ;i<nums.length ;i++){
if(i != nums[i] - list.size()){
list.add(i + list.size());
}
}
}
return list;
}
边栏推荐
- Unity游戏优化[第二版]学习记录6
- 17 servicetask of flowable task
- [reprint] complete collection of C language memory and character operation functions
- Getclassloader() returns null, getclassloader() gets null
- 使用cmake交叉編譯helloworld
- Fast power code
- August 15, 2021 another week
- Case - simulated landlords (upgraded version)
- Detailed explanation of R language sparse matrix
- Case - recursive factorial (recursive)
猜你喜欢
Metaltc4.0 stable release
Basic operations of MySQL auto correlation query
890. Find and Replace Pattern
10 signalstartevent and signalcatchingevent of flowable signal events
Pycharm错误解决:Process finished with exit code -1073741819 (0xC0000005)
Django uploads local binaries to the database filefield field
1 Introduction to drools rule engine (usage scenarios and advantages)
Enhanced for loop
Pychart error resolution: process finished with exit code -1073741819 (0xc0000005)
Dynamic and static libraries
随机推荐
Mongodb Multi - field Aggregation group by
安装harbor(在线|离线)
priority inversion problem
Mongodb multi field aggregation group by
使用cmake交叉编译helloworld
Anaconda configuring the mirror source
Django uploads local binaries to the database filefield field
Std:: Map empty example
Case - traversing the directory (file class & recursive call)
11 signalthrowingevent and signalboundaryevent of flowable signal event
【多线程编程】Future接口获取线程执行结果数据
19 calling subprocess (callactivity) of a flowable task
Dup2 use
Use the browser to cut the entire page (take chrome as an example)
2 first experience of drools
MySQL advanced query
MySQL table data modification
【线程/多线程】线程的执行顺序
Etcd fast cluster building
Unity game optimization (version 2) learning record 7