当前位置:网站首页>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
边栏推荐
- Vscode Chinese plug-in doesn't work. Problem solving
- Redis master-slave synchronization
- UML学习_day2
- 牛客网数据库SQL实战详细剖析(26-30)
- The life cycle of key value in redis module programming
- [stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
- 1n5408-asemi rectifier diode
- Analysis of common storage types and FTP active and passive modes
- [thymeleaf] handle null values and use safe operators
- Swift protocol Association object resource name management multithreading GCD delay once
猜你喜欢
LeetCode151-颠倒字符串中的单词-字符串-模拟
Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]
Mysql连接查询详解
我的树莓派 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
[NLP] HMM hidden Markov + Viterbi word segmentation
nuxt项目:全局获取process.env信息
Role of asemi rectifier module mdq100-16 in intelligent switching power supply
Do (local scope), initializer, memory conflict, swift pointer, inout, unsafepointer, unsafebitcast, success
Leetcode149 - maximum number of points on a line - Math - hash table
Programming philosophy - automatic loading, dependency injection and control inversion
随机推荐
On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)
分布式事务Seata介绍
C language super complete learning route (collection allows you to avoid detours)
OC to swift conditional compilation, marking, macro, log, version detection, expiration prompt
Thinkphp5 + data large screen display effect
Modify the default listening IP of firebase emulators
JS - implémenter la fonction de copie par clic
Subnet division of flannel principle
async void 导致程序崩溃
Basic operation of circular queue (Experiment)
LeetCode162-寻找峰值-二分-数组
Unity_ Code mode add binding button click event
JUC学习记录(2022.4.22)
Mysql连接查询详解
我的树莓派 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
A series of problems about the best time to buy and sell stocks
大文件如何快速上传?
async关键字
封面和标题中的关键词怎么写?做自媒体为什么视频没有播放量
Flink datastream type system typeinformation