思路显然是暴力枚举.

但是两个问题:

1.当1的位数非常大时,模运算很费时间,会超时.

其实每次不用完全用'11111...'来%K,上一次的余数*10+1后再%K就行.

证明:

令f(n)=111111...(n个1);  

 g(n)=f(n)%K

 因为f(n)=f(n-1)*10+1

 所以f(n)%K=(f(n-1)*10+1)%K

 即g(n)=g(n-1)*10+1

2.枚举何时停止?

一种方法是可以设置一个大数,比如10的6次方,可以Accepted.

更精确的方法是:从1个1到K个1,如果这里都没有答案,后面也没了.

因为K的余数不包括0的话有K-1个,我们算了K个,K个里面没有0的话,里面必然至少有两个相等的(抽屉原理),而根据第一个问题所示,相邻的余数有关系,所以一相等之后就是重复循环这些数了,前面找不到后面也肯定没有了.例如K=6:

  • 1 % 6 = 1
  • 11 % 6 = 5
  • 111 % 6 = 3
  • 1111 % 6 = 1
  • 11111 % 6 = 5
  • 111111 % 6 = 3
class Solution:
def smallestRepunitDivByK(self, K: int) -> int:
if K % 2 == 0 or K % 5 == 0:
return -1
g = 0
for i in range(1, K+1):
g = (g * 10 + 1) % K
if g == 0:
return i
return -1

Leetcode 1015. Smallest Integer Divisible by K的更多相关文章

  1. 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【leetcode】1022. Smallest Integer Divisible by K

    题目如下: Given a positive integer K, you need find the smallest positive integer N such that N is divis ...

  3. [Swift]LeetCode1015. 可被 K 整除的最小整数 | Smallest Integer Divisible by K

    Given a positive integer K, you need find the smallest positive integer N such that N is divisible b ...

  4. [LeetCode] K-th Smallest Prime Fraction 第K小的质分数

    A sorted list A contains 1, plus some number of primes.  Then, for every p < q in the list, we co ...

  5. Leetcode 974. Subarray Sums Divisible by K

    前缀和(prefix sum/cumulative sum)的应用. 还用了一个知识点: a≡b(mod d) 则 a-b被d整除. 即:a与b对d同余,则a-b被d整除. class Solutio ...

  6. [LeetCode] K-th Smallest in Lexicographical Order 字典顺序的第K小数字

    Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. N ...

  7. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  8. 【LeetCode】974. Subarray Sums Divisible by K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 前缀和求余 日期 题目地址:https:/ ...

  9. 119th LeetCode Weekly Contest Subarray Sums Divisible by K

    Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...

随机推荐

  1. git命令与协同开发

    一 git命令 1.简单命令 git init # 初始化 (建立git 版本相关文件关系都放这里) git config --global user.email "you@example. ...

  2. day11 函数和命名空间

    # def my_sum(*args):# sum_2=0# for i in args:# sum_2+=i# return sum_2### ret=my_sum(1,2,3,4)# print( ...

  3. JavaScript:判断当前浏览器是否为微信浏览器

    <script type="text/javascript"> function CheckWeChatBrowser() { var ua = navigator.u ...

  4. 笔记-Markdown常用语法

    其实应该很早就已经接触到了Markdown这种简洁却彪悍的标记语言,比如Github的README.md,只不过被不走心的我当作txt文档来用了.直到前个看到一位大神的读书列表清单,觉得很新奇,就有意 ...

  5. Spring Data Jpa示例(IntelliJ maven项目)

    1. 在IntelliJ中新建maven项目 给出一个建好的示例,(本示例中省略了业务逻辑组件UserService) 2. 在pom.xml中配置依赖 包括: spring-context spri ...

  6. 用python实现0到9之间10个数字排列不重复的个数

      """ product 笛卡尔积 permutations 排列 combinations 组合,没有重复 combinations_with_replacement ...

  7. 【笔记】css3实现网页平滑过渡效果...

    参考:http://www.imooc.com/video/7142 未完. <!DOCTYPE html> <html> <head> <meta char ...

  8. pycharm中创建并设置docker解释器

    在Windows上使用Docker的其中一个目的是使其与PyCharm结合,形成Python代码的解释器,避免重复的Python解释环境搭建的问题,同时保持Windows开发环境和部署环境所用的Pyt ...

  9. Python 中lambda 简单介绍

    转自:https://www.cnblogs.com/AlwaysWIN/p/6202320.html 在学习python的过程中,lambda的语法经常出现,现在将它整理一下,以备日后查看. 1.l ...

  10. OpenStack Ceilometer -- 后台数据存储优化之MongoDB的分片存储设置

    https://xiaofandh12.github.io/Mongo-Shard 关于MongoDB MongoDB中的概念与关系型数据库之间的对应: Database --> Databas ...