forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
37 lines (28 loc) · 954 Bytes
/
main2.cpp
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
27
28
29
30
31
32
33
34
35
36
37
/// Source : https://leetcode.com/problems/the-kth-factor-of-n/
/// Author : liuyubobobo
/// Time : 2020-06-27
#include <iostream>
#include <vector>
using namespace std;
/// Just calculate all the factors once
/// Time Complexity: O(sqrt(n))
/// Space Complexity: O(n)
class Solution {
public:
int kthFactor(int n, int k) {
vector<int> factors;
for(int i = 1; i * i <= n; i ++) // 使用 i * i <= n,避免 sqrt(n) 运算的性能和精度问题
if(n % i == 0){
factors.push_back(i);
// 对于 i * i == n 的情况要进行以下判断,
// 如果 i * i == n,则 i 和 n / i 是一个数字,不能重复添加进 factors
if(i * i != n)
factors.push_back(n / i);
}
sort(factors.begin(), factors.end());
return k - 1 < factors.size() ? factors[k - 1] : -1;
}
};
int main() {
return 0;
}