返回介绍

solution / 1000-1099 / 1015.Smallest Integer Divisible by K / README_EN

发布于 2024-06-17 01:03:32 字数 2912 浏览 0 评论 0 收藏 0

1015. Smallest Integer Divisible by K

中文文档

Description

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

Solutions

Solution 1

class Solution:
  def smallestRepunitDivByK(self, k: int) -> int:
    n = 1 % k
    for i in range(1, k + 1):
      if n == 0:
        return i
      n = (n * 10 + 1) % k
    return -1
class Solution {
  public int smallestRepunitDivByK(int k) {
    int n = 1 % k;
    for (int i = 1; i <= k; ++i) {
      if (n == 0) {
        return i;
      }
      n = (n * 10 + 1) % k;
    }
    return -1;
  }
}
class Solution {
public:
  int smallestRepunitDivByK(int k) {
    int n = 1 % k;
    for (int i = 1; i <= k; ++i) {
      if (n == 0) {
        return i;
      }
      n = (n * 10 + 1) % k;
    }
    return -1;
  }
};
func smallestRepunitDivByK(k int) int {
  n := 1 % k
  for i := 1; i <= k; i++ {
    if n == 0 {
      return i
    }
    n = (n*10 + 1) % k
  }
  return -1
}
function smallestRepunitDivByK(k: number): number {
  let n = 1 % k;
  for (let i = 1; i <= k; ++i) {
    if (n === 0) {
      return i;
    }
    n = (n * 10 + 1) % k;
  }
  return -1;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文