harrytothemoon / leetcodeAplus

Leetcode meeting note
2 stars 0 forks source link

[1492] The kth Factor of n #58

Open tsungtingdu opened 4 years ago

tsungtingdu commented 4 years ago
var kthFactor = function(n, k) {
    let set = new Set()

    let left = 1
    let right = n

    while (left <= right) {
        if (n % left === 0) {
            right = n/left
            set.add(left).add(right)
        }
        left++
    }
    set = Array.from(set).sort((a, b) => a - b)
    return set[k - 1] ? set[k - 1] : -1
};