1 <= k <= n <= 1000 【解法】 classSolution:defkthFactor(self, n: int, k: int) ->int:forfactorinrange(1, n + 1):ifn % factor ==0: k-= 1ifk ==0:returnfactorreturn-1 Runtime:32 ms, faster than82.00% of Python3 online submissions for The kth Factor of n. Memory Usage:1...
给你两个正整数 n 和 k 。 如果正整数 i 满足 n % i == 0 ,那么我们就说正整数 i 是整数 n 的因子。 考虑整数 n 的所有因子,将它们 升序排列 。请你返回第 k 个因子。如果 n 的因子数少于 k ,请你返回 -1 。 来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/the-kth-factor-of-n...
暴力枚举所有情况,然后判断是否是第k个返回,如果枚举完了,还没有返回,那就是返回false。 3 代码 classSolution{ public: intkthFactor(intn,intk) { intindex=1; for(inti=1;i<=n;i++){ if(n%i==0){ if(k==index)returni; index++; } } return-1; } }; 1. 2. 3. 4. ...
Given two positive integersnandk. A factor of an integernis defined as an integeriwheren % i == 0. Consider a list of all factors ofnsorted inascending order, returnthekthfactorin this list or return-1ifnhas less thankfactors. Example 1: Input:n =12, k =3Output:3Explanation:Factors ...
1 <= k <= n <= 1000 解题思路:n最大才1000,把n全部的因子求出来排序就可以了。 代码如下: classSolution(object):defkthFactor(self, n, k):""":type n: int :type k: int :rtype: int"""factor=[]foriinrange(1,n+1):ifn%i ==0: ...