测试一

Implement int sqrt(int x).

Compute and return the square root of x.

实现开根号运算,由于输入输出是int,所以,可以暴力遍历一遍,但这肯定超时

想到暴力就应该想到二分查找,
这里有几个可能会溢出的地方要注意:

1
2
3
4
//计算中点时
int mid=start+(end-start)/2;
//计算平方时
if(mid==x/mid)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
int mySqrt(int x) {
if(x==0||x==1)
return x;
int end=x/2+1;
int start=1;
while(start<=end)
{
int mid=start+(end-start)/2;
if(mid==x/mid) return mid;
if(mid<x/mid)
{
start=mid+1;
}
else
{
end=mid-1;
}
}
if(start>x/start)
return start-1;
else
return start;
}
};

问题一:HTTP的交互方式有哪些

问题二:TCP与UDP的区别
何种场景下用TCP,何种场景下用UDP

问题三:struct与class的区别

问题四:什么是多态

问题五: