算法岗位:
第一题:通过率为70%,时间复杂度不够,求赐教~~~
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long n = in.nextLong();
System.out.println(solution(n));
in.close();
}
public static long solution(long n){
if(n == 0){
return 0;
}
long result = 1;
while(true){
++result;
if(n>(result*(result+1)/2) && n<(result+1)*(result+2)/2){
break;
}
}
return result+1;
}
}
第二题目:(不知道能不能通过,因为我没时间了,哎~~~,我感觉即使能过,时间复杂度也过不了
没有中间的四层for循环,通过率为10%,京东笔试也就这样了)
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long n = in.nextLong();
System.out.println(solution(n));
in.close();
}
public static long solution(long n){
if(n == 0){
return 0;
}
long result = 0;
long m = 10000000007L;
result+= Math.pow(n, n) + (n-1)*n;
for(int a = 2;a<=n;a++){
for(int b = 2;a<=n;a++){
for(int c = 2;a<=n;a++){
for(int d = 2;a<=n;a++){
if(a!=b && c!=d && a!=c){
if(Math.pow(a, b) == Math.pow(c,d)){
result ++;
}
}
}
}
}
}
return result%m;
}
}