-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1004 from 0xff-dev/1492
Add solution and test-cases for problem 1492
- Loading branch information
Showing
3 changed files
with
58 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# [1492.The kth Factor of n][title] | ||
|
||
## Description | ||
You are given two positive integers `n` and `k`. A factor of an integer `n` is defined as an integer `i` where `n % i == 0`. | ||
|
||
Consider a list of all factors of `n` sorted in **ascending order**, return the `kth` factor in this list or return `-1` if n has less than `k` factors. | ||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: n = 12, k = 3 | ||
Output: 3 | ||
Explanation: Factors list is [1, 2, 3, 4, 6, 12], the 3rd factor is 3. | ||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: n = 7, k = 2 | ||
Output: 7 | ||
Explanation: Factors list is [1, 7], the 2nd factor is 7. | ||
``` | ||
|
||
**Example 3:** | ||
|
||
``` | ||
Input: n = 4, k = 4 | ||
Output: -1 | ||
Explanation: Factors list is [1, 2, 4], there is only 3 factors. We should return -1. | ||
``` | ||
|
||
## 结语 | ||
|
||
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] | ||
|
||
[title]: https://leetcode.com/problems/the-kth-factor-of-n/ | ||
[me]: https://github.com/kylesliu/awesome-golang-algorithm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
func Solution(n int, k int) int { | ||
i := 1 | ||
for ; i <= n; i++ { | ||
if n%i == 0 { | ||
k-- | ||
if k == 0 { | ||
return i | ||
} | ||
} | ||
} | ||
return -1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters