【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 ...
随机推荐
- springboot与数据访问之jdbc
官网的starthttps://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-starter 添加依 ...
- 【机器学习与R语言】11- Kmeans聚类
目录 1.理解Kmeans聚类 1)基本概念 2)kmeans运作的基本原理 2.Kmeans聚类应用示例 1)收集数据 2)探索和准备数据 3)训练模型 4)评估性能 5)提高模型性能 1.理解Km ...
- mysql—从字符串中提取数字(类型1)
select reason,CHAR_LENGTH(reason),mid(reason,5,CHAR_LENGTH(reason)-5)+0 from `table` 解释: CHAR_LENGTH ...
- Linux—find在指定路径下查找文件或目录
find /指定路径 -name "*filename*" find /指定路径 -name "*filename*" 2>/dev/null ...
- 1.TwoSum-Leetcode
#include<iostream> #include<algorithm> #include<map> using namespace std; class So ...
- 零基础学习java------day11------常用API---Object、Scanner、String、StringBufer/StringBuilder
API概述 API(application Programming Interface, 应用程序编程接口),是一些预先定义的函数.目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力, ...
- ES6必知,箭头函数与普通函数的区别。
1. 箭头函数没有prototype(原型),所以箭头函数本身没有this let a = () =>{}; console.log(a.prototype); // undefined 2. ...
- Oracle—全局变量
Oracle全局变量 一.数据库程序包全局变量 在程序实现过程中,经常用遇到一些全局变量或常数.在程序开发过程中,往往会将该变量或常数存储于临时表或前台程序的全局变量中,由此带来运行效率降 ...
- Can namespaces be nested in C++?
In C++, namespaces can be nested, and resolution of namespace variables is hierarchical. For example ...
- SSO(单点登录)示例
此文为转载文章,出处:https://www.cnblogs.com/jpfss/p/9273680.html SSO在我们的应用中非常常见,例如我们在OA系统登录了,我们就可以直接进入采购系统,不需 ...