【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/smallest-integer-divisible-by-k/
题目描述
Given a positive integer K, you need find 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.
Example 1:
Input: 1
Output: 1
Explanation: The smallest answer is N = 1, which has length 1.
Example 2:
Input: 2
Output: -1
Explanation: There is no such positive integer N divisible by 2.
Example 3:
Input: 3
Output: 3
Explanation: The smallest answer is N = 111, which has length 3.
Note:
1 <= K <= 10^5
题目大意
给了一个整数K,现在要求一个全部由1构成的十进制整数N,使得N能被K整除,并且要求返回最小满足条件的N的位数。如果N不存在,则返回-1.
解题方法
这个题有点不好想,如果不去考虑技巧的话,根本想不出来还有什么解法。
首先,如果K的尾数是2,4,5,6,8的话,一定不存在N。简单说明:我们要求的N结尾一定是1,那么一定不能被2的倍数整除。另外我们知道能被5整除的数字的结尾必须是0或者5,所以得证。
然后,我们要证明N的长度不会超过K。
我们要判断对于每个N其对K的余数:1 % K, 11 % K, 111 % K, ..., 11...1 (K '1's) % K.
- 如果这K个余数中有一个余数是0,那么当前的N能被K整除直接返回。
- 如果这K个余数中都不为0时,一定有重复的余数!我们知道一个数对K的余数只能是
0 ~ K - 1其中的一个,所以如果K个数字的余数中没有0,那么肯定有重复的余数。如果出现重复的余数,那么后面再增大N时,对K的余数就会形成循环,则再也不可能出现余数为0的情况。
总之,如果遍历到了长度为K的N时仍然不存在余数是0,那么后面就不用搜索了。
举个例子,我们发现长度 <= 6 = K的N的余数是循环的。
- 1 % 6 = 1
- 11 % 6 = 5
- 111 % 6 = 3
- 1111 % 6 = 1
- 11111 % 6 = 5
- 111111 % 6 = 3
严谨的证明应该是如果N2 % K == N1 % K的话,证明(10 * N2 + 1) % K == (10 * N1 + 1) % K. 留给读者证明吧。
另外,我们在求的过程中,并不是直接维护的N,而是维护的N % K,这里的假设是(10 * N + 1) % K的变化规律和(10 * (N % K) + 1) % K变化规律一致。
Python代码如下:
class Solution(object):
def smallestRepunitDivByK(self, K):
"""
:type K: int
:rtype: int
"""
if K % 10 not in {1, 3, 7, 9}: return -1
r = 0
for i in range(1, K + 1):
r = (10 * r + 1) % K
if r == 0:
return i
return -1
日期
2019 年 3 月 24 日 —— 这个周赛太悲催了
【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)的更多相关文章
- 【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 ...
- Leetcode 1015. Smallest Integer Divisible by K
思路显然是暴力枚举. 但是两个问题: 1.当1的位数非常大时,模运算很费时间,会超时. 其实每次不用完全用'11111...'来%K,上一次的余数*10+1后再%K就行. 证明: 令f(n)=1111 ...
- [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 ...
- 【LeetCode】974. Subarray Sums Divisible by K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 前缀和求余 日期 题目地址:https:/ ...
- 【LeetCode】560. Subarray Sum Equals K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
随机推荐
- (亿级流量)分布式防重复提交token设计
大型互联网项目中,很多流量都达到亿级.同一时间很多的人在使用,而每个用户提交表单的时候都可能会出现重复点击的情况,此时如果不做好控制,那么系统将会产生很多的数据重复的问题.怎样去设计一个高可用的防重复 ...
- A Child's History of England.43
PART THE SECOND When the King heard how Thomas à Becket had lost his life in Canterbury Cathedral, t ...
- day02 Rsyuc备份服务器
day02 Rsyuc备份服务器 一.备份 1.什么是备份 备份就是把重要的数据或者文件复制一份保存到另一个地方,实现不同主机之间的数据同步 一般数据比较重要的情况下,数据如果丢失很容易找不回来了的, ...
- 【Linux】【Commands】基础概念及常用基础命令
命令的语法通用格式: ------------------------------------------------ #COMMAND OPTIONS ARGUMENTS 发起命令:请求内核将某个二 ...
- Java 使用slf4j记录日志
引入依赖 <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12< ...
- Leetcode 78题-子集
LeetCode 78 网上已经又很多解这题的博客了,在这只是我自己的解题思路和自己的代码: 先贴上原题: 我的思路: 我做题的喜欢在本子或别处做写几个示例,以此来总结规律:下图就是我从空数组到数组长 ...
- 注册页面html版本
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- JDBCUtils工具类的属性
package cn.itcast.util;import java.io.FileReader;import java.io.IOException;import java.net.URL;impo ...
- 2.使用Lucene开发自己的搜索引擎–indexer索引程序中基本类介绍
(1)Directory:Directory类描述了Lucene索引的存放位置,它是一个抽象,其子类负责具体制定索引的存储路径.FSDirectory.open方法来获取真实文件在文件系统中的存储路径 ...
- 机器学习——sklearn中的API
import matplotlib.pyplot as pltfrom sklearn.svm import SVCfrom sklearn.model_selection import Strati ...