当前位置:网站首页>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
边栏推荐
- ffmpeg安装遇错:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
- SQL中HAVING和WHERE的区别
- Compiling OpenSSL
- Lotus DB design and Implementation - 1 Basic Concepts
- Subnet division of flannel principle
- LeetCode149-直线上最多的点数-数学-哈希表
- async void 导致程序崩溃
- Practice of unified storage technology of oppo data Lake
- C language super complete learning route (collection allows you to avoid detours)
- 博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
猜你喜欢
Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
API gateway / API gateway (II) - use of Kong - load balancing
中富金石财富班29800效果如何?与专业投资者同行让投资更简单
Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]
TLS / SSL protocol details (30) RSA, DHE, ecdhe and ecdh processes and differences in SSL
Detailed explanation of C language knowledge points -- data types and variables [1] - carry counting system
LeetCode151-颠倒字符串中的单词-字符串-模拟
Leetcode153 - find the minimum value in the rotation sort array - array - binary search
Swift protocol Association object resource name management multithreading GCD delay once
随机推荐
OC to swift conditional compilation, marking, macro, log, version detection, expiration prompt
UML learning_ Day2
For 22 years, you didn't know the file contained vulnerabilities?
Nacos程序连接MySQL8.0+ NullPointerException
Comparaison du menu de l'illustrateur Adobe en chinois et en anglais
Leetcode162 - find peak - dichotomy - array
Redis cluster principle
大文件如何快速上传?
Error: unable to find remote key "17f718f726"“
多语言通信基础 06 go实现grpc的四种数据流模式实现
买卖股票的最佳时机系列问题
冰冰学习笔记:一步一步带你实现顺序表
JS -- realize click Copy function
SSH connects to the remote host through the springboard machine
Contraction mapping theorem
Llvm - generate for loop
The life cycle of key value in redis module programming
Sqlserver transaction and lock problem
Adobe Illustrator menu in Chinese and English
API gateway / API gateway (III) - use of Kong - current limiting rate limiting (redis)