当前位置:网站首页>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
边栏推荐
- Llvm - generate addition
- Swift - literal, literal protocol, conversion between basic data types and dictionary / array
- Three uses of kprobe
- 调度系统使用注意事项
- UML learning_ Day2
- Set onedrive or Google drive as a drawing bed in upic for free
- The win10 taskbar notification area icon is missing
- Flink DataStream 类型系统 TypeInformation
- 1n5408-asemi rectifier diode
- Little red book timestamp2 (2022 / 04 / 22)
猜你喜欢
8.4 realization of recurrent neural network from zero
我的 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
1n5408-asemi rectifier diode
Introduction to Arduino for esp8266 serial port function
Explanation and example application of the principle of logistic regression in machine learning
How does eolink help telecommuting
nuxt项目:全局获取process.env信息
Daily question - leetcode396 - rotation function - recursion
API gateway / API gateway (II) - use of Kong - load balancing
Thinkphp5 + data large screen display effect
随机推荐
Comparaison du menu de l'illustrateur Adobe en chinois et en anglais
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
Have you really learned the operation of sequence table?
LeetCode 练习——396. 旋转函数
1990年1月1日是星期一,定义函数date_to_week(year,month,day),实现功能输入年月日后返回星期几,例如date_to_week(2020,11,1),返回:星期日。 提示:
中富金石财富班29800效果如何?与专业投资者同行让投资更简单
博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
LeetCode165-比较版本号-双指针-字符串
C语言超全学习路线(收藏让你少走弯路)
API gateway / API gateway (III) - use of Kong - current limiting rate limiting (redis)
Llvm - generate if else and pH
Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
Llvm - generate for loop
MySQL sync could not find first log file name in binary log index file error
Swift: entry of program, swift calls OC@_ silgen_ Name, OC calls swift, dynamic, string, substring
Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
LeetCode162-寻找峰值-二分-数组
Unity_ Code mode add binding button click event
MySQL error packet out of order
SSH connects to the remote host through the springboard machine