-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1015.java
More file actions
55 lines (54 loc) · 1.27 KB
/
Copy path1015.java
File metadata and controls
55 lines (54 loc) · 1.27 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// 1015. Smallest Integer Divisible by K
//
// Given a positive integer k, you need to find the length of the smallest positive integer n such that n is divisible by k, and n only contains the digit 1.
//
// Return the length of n. If there is no such n, return -1.
//
// Note: n may not fit in a 64-bit signed integer.
//
//
//
// Example 1:
//
// Input: k = 1
// Output: 1
// Explanation: The smallest answer is n = 1, which has length 1.
// Example 2:
//
// Input: k = 2
// Output: -1
// Explanation: There is no such positive integer n divisible by 2.
// Example 3:
//
// Input: k = 3
// Output: 3
// Explanation: The smallest answer is n = 111, which has length 3.
//
//
// Constraints:
//
// 1 <= k <= 105
//
// Runtime 2 ms Beats 57.48%
// Memory 40.1 MB Beats 22.83%
class Solution {
public int smallestRepunitDivByK(int k) {
if (k % 2 == 0 || k % 5 == 0) return -1;
int n = 1;
int len = 1;
if (k == 1) return len;
boolean[] remainder = new boolean[k];
while (n != 0) {
n = (n * 10 + 1) % k;
len++;
if (remainder[n] == true) {
return -1;
}
remainder[n] = true;
}
return len;
}
}
// 1 % 7 = 1
// 11 % 7 = 4
// 111 % 7 = (11 % 7 * 10 + 1) % 7