我自己的写得很丑很慢,开平方计算太慢了,没什么好说的,看看人家的。
我的解法
class Solution {
public:
vector<int> constructRectangle(int area) {
vector<int> output;
int w = sqrt(area) / 1;
int a = 0, b = 0;
for (int i = w; i <= area; i ++)
if (area % i == 0){
b = area / i;
a = i >= b ? i : b;
output.push_back(a);
output.push_back(area / a);
return output;
}
}
};
人家的解法
平方比开方运算快得多= =
class Solution {
public:
vector<int> constructRectangle(int area) {
if (area <= 0) return vector<int> {};
vector<int> res;
int w = area;
for (int i = 1; i * i <= area; ++i) {
if (area % i == 0) w = i;
}
return vector<int> {area / w, w};
}
};