Skip to content

Commit 710f392

Browse files
committed
Runtime 5 ms (Top 46.73%) | Memory 5.0 MB (Top 64.2%)
1 parent b53b7fa commit 710f392

File tree

1 file changed

+6
-21
lines changed

1 file changed

+6
-21
lines changed
Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
class Solution {
2-
public:
3-
int smallestRepunitDivByK(int k) {
4-
long long n=1,res=1;
5-
while(n<k){
6-
n=n*10+1;
7-
res++;
8-
}
9-
if(n==k)
10-
return res;
11-
set<int> a;
12-
while(true){
13-
n%=k;
14-
if(n==0)return res;
15-
if(a.count(n)){
16-
break;
17-
}
18-
a.insert(n);
19-
n=n*10+1;
20-
res++;
21-
}
2+
public:
3+
int smallestRepunitDivByK(int K) {
4+
for (int r = 0, N = 1; N <= K; ++N)
5+
if ((r = (r * 10 + 1) % K) == 0)
6+
return N;
227
return -1;
238
}
24-
};
9+
};

0 commit comments

Comments
 (0)