【leetcode】1022. Smallest Integer Divisible by K
题目如下:
Given a positive integer
K, you need find the smallest positive integerNsuch thatNis divisible byK, andNonly contains the digit 1.Return the length of
N. If there is no suchN, 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
解题思路:题目要求找出最小的一个X,使得K*X = 111.....111,一开始我的思路是找出这样的X,例如K=19937,那么X的最后一位一定是3,接下来再计算X的倒数第二位,但是这样会超时,可能是掉入了死循环。其实反过来想想,我们可以111.....111去除以K,判断能否被K整除。首先找出大于或等于K的最小的11...11,然后除以K得到余数,余数后面继续加上最少数量的1使得余数大于K,然后再除以K直到余数为0为止。对于无法被整除的情况,经过若干次操作之后,一定会出现重复的余数,只要用字典记录出现过的余数,如果有重复出现则返回-1。
代码如下:
class Solution(object):
def smallestRepunitDivByK(self, K):
"""
:type K: int
:rtype: int
"""
len_k = len(str(K))
res = len_k
div = '' * (len_k)
if int(div) < K:
div += ''
res += 1
dic_remainder = {}
while True:
remainder = int(div) % K
if remainder == 0:
return res
elif remainder in dic_remainder:
return -1
dic_remainder[remainder] = 1
sr = str(remainder)
div = sr + '' * (len_k - len(sr))
res += (len_k - len(sr))
if int(div) < K:
div += ''
res += 1
【leetcode】1022. Smallest Integer Divisible by K的更多相关文章
- 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
 - 【LeetCode】974. Subarray Sums Divisible by K 解题报告(C++)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 前缀和求余 日期 题目地址:https:/ ...
 - 【leetcode】974. Subarray Sums Divisible by K
		
题目如下: Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have ...
 - 【LeetCode】String to Integer (atoi) 解题报告
		
这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...
 - 【LeetCode】#7 Reverse Integer
		
[Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...
 - [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】910. Smallest Range II 解题报告(Python & C++)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
 - 【LeetCode】908. Smallest Range I 解题报告(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...
 - 【leetcode】Roman to Integer
		
题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
 
随机推荐
- mysql中 key 、primary key 、unique key 和 index 有什么不同
			
mysql中 key .primary key .unique key 和 index 有什么不同 key 是数据库的物理结构,它包含两层意义和作用, 一是约束(偏重于约束和规范数据库的结构完整性), ...
 - JS-格式化json
			
一 使用原生 JSON.stringify 实现 <textarea name="" id="myTA" cols="30" rows ...
 - MVC终极解释
			
之前校招笔面试老师被问起MVC,虽然都知道怎么回事.但每次组织语言总觉得答得的简洁明了和突出重点.下面是我总结,希望未来找工作的学弟学妹们能不在受此大路边上的问题困扰. M-V-C 即Model-Vi ...
 - 16/8/21_PHP-有关类函数,this,static,面向对象思想介绍
			
class_exists():判断某个类是否存在(定义过) interface_existe():判断接口是否存在 get_class():获取某个对象的"所属类名" get_pa ...
 - 【ABAP系列】SAP ABAP模块-查找系统出口或BADI的方法
			
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP模块-查找系统出 ...
 - OJ测试数据追溯方法
			
https://blog.csdn.net/iwts_24/article/details/79240987 我是从这个博客知道还有从比赛官方网站扒测试数据的方法,但是下面的链接有些是失效的. 所以我 ...
 - Win7崩溃程序目录
			
很烦,占用系统空间,毫无用处 C:\Users\你的用户名\AppData\Local\CrashDumps C:\Users\你的用户名\AppData\Local\Microsoft\Window ...
 - 高德WMTS规则
			
新版 http://wprd0{1-4}.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1& ...
 - Mac007--Mysq服务端&客户端安装
			
一.安装Mysql服务端与Navicat Premium客户端 参见博客:https://blog.csdn.net/wtdask/article/details/79025674 安装mysql服务 ...
 - PAT甲级【2019年9月考题】——A1164 DijkstraSequence【30】
			
7-4 Dijkstra Sequence (30 分) Dijkstra's algorithm is one of the very famous greedy algorithms. It is ...