当前位置:网站首页>Example of time complexity calculation
Example of time complexity calculation
2022-04-23 15:07:00 【White horse is not a horse·】
subject :x Of n Power , Results output
package demo4_8;
public class leijia {
static int temp;
public static void main(String[] args) {
// Think about the time complexity of recursion
// problem :x Of n Power
int x=2;
int n=8;
System.out.println(" Method 1 :"+method1(x,n));
System.out.println(" Method 2 :"+method2(x,n));
System.out.println(" Method 3 :"+method3(x,n));
System.out.println(" Method four :"+method4(x,n));
}
// Method 1 : Solve directly ( The time complexity is O(n)
public static int method1(int x,int n){
int result=1;
for(int i=0;i<n;i++){
result*=x;
}
return result;
}
// Method 2 : Recursive method : Time complexity or O(n)
public static int method2(int x,int n){
if(n==0) return 1;
return method2(x,n-1)*x;
}
// Method 3 : Recursive method : Time complexity or O(n)
// Calculate through full binary tree , Time complexity or O(n)
public static int method3(int x,int n){
if(n==0) return 1;
if(n%2==1) return method3(x,n/2)*method3(x,n/2)*x;
return method3(x,n/2)*method3(x,n/2);
}
// Method four : Recursive method : Time complexity or O(n)
public static int method4(int x,int n){
if(n==0) return 1;
temp=method4(x,n/2); // Record intermediate variables
System.out.println(" frequency ");
if(n%2==1) return temp*temp*x;
return temp*temp;
}
}
版权声明
本文为[White horse is not a horse·]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231407525452.html
边栏推荐
- Explanation and example application of the principle of logistic regression in machine learning
- 买卖股票的最佳时机系列问题
- Error: unable to find remote key "17f718f726"“
- Three uses of kprobe
- SQL中HAVING和WHERE的区别
- go基础 反射
- [stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
- Application of skiplist in leveldb
- Share 20 tips for ES6 that should not be missed
- What is the main purpose of PCIe X1 slot?
猜你喜欢
Reptile exercises (1)
8.5 concise implementation of cyclic neural network
Five data types of redis
MySQL error packet out of order
如何设计一个良好的API接口?
Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
Redis主从同步
How to upload large files quickly?
Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]
大文件如何快速上传?
随机推荐
Leetcode149 - maximum number of points on a line - Math - hash table
The win10 taskbar notification area icon is missing
C语言超全学习路线(收藏让你少走弯路)
How to write the keywords in the cover and title? As we media, why is there no video playback
Redis主从同步
SQLSERVER事物与锁的问题
Realization of four data flow modes of grpc based on Multilingual Communication
Five data types of redis
免费在upic中设置OneDrive或Google Drive作为图床
async关键字
中富金石财富班29800效果如何?与专业投资者同行让投资更简单
Progress in the treatment of depression
A series of problems about the best time to buy and sell stocks
JUC learning record (2022.4.22)
分享 20 个不容错过的 ES6 的技巧
How does eolink help telecommuting
TLS / SSL protocol details (28) differences between TLS 1.0, TLS 1.1 and TLS 1.2
买卖股票的最佳时机系列问题
Thinkphp5 + data large screen display effect
January 1, 1990 is Monday. Define the function date_ to_ Week (year, month, day), which realizes the function of returning the day of the week after inputting the year, month and day, such as date_ to