google笔试题收集
1. 异或
1^1=0
0^0=0
1^0=1
0^1=1
2. 斐波那契数列(Fibonacci)
3.存储空间上不具有优势
4. 由“主定理”
T(n)=25T(n/5) + n2
a=25 b=5 f(n)=n2
logba=2
将nlogba与f(n)比较: 相等
则时间复杂度为O(n2logn)
1.
2.
3
#include <iostream>
#include <limits>
using namespace std;
int foo(int arr[], int n)
{
int max = numeric_limits<int>::min();
//cout << max << "\n";
for(int i = 0; i < n; i++)
{
int product = 1;
for(int j =0; j < n; j++)
{
if(j != i )
product *= arr[j];
}
max = product > max ? product : max;
}
return max;
}
int main()
{
int arr[4] = { -1, -5, 2, 3};
cout << foo(arr, 4) << "\n";
return 0;
}
#include <limits>
using namespace std;
int foo(int arr[], int n)
{
int max = numeric_limits<int>::min();
//cout << max << "\n";
for(int i = 0; i < n; i++)
{
int product = 1;
for(int j =0; j < n; j++)
{
if(j != i )
product *= arr[j];
}
max = product > max ? product : max;
}
return max;
}
int main()
{
int arr[4] = { -1, -5, 2, 3};
cout << foo(arr, 4) << "\n";
return 0;
}
时间复杂度分析:
总共的操作步骤 n*{[n+(n-1)]+1}=2n2
时间复杂度为O(n2)
空间复杂度为O(1) 4*sizeof(int)
改进算法: