【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 ...
随机推荐
- 【Unity优化】Unity中究竟能不能使用foreach?
关于这个话题,网络上讨论的很多,我也收集了一些资料,都不是很齐全,所以自己亲自测试,这里把结果分享给大家. foreach究竟怎么了? 研究过这个问题的人都应该知道,就是它会引起频繁的GC Alloc ...
- 全国地区sql表
/** * 中国省市区--地区SQL表 */ CREATE TABLE `rc_district` ( `id` smallint(5) unsigned NOT NULL AUTO_INCREMEN ...
- PHP之导出CSV文件
序言 Q1:什么是csv文件? A1:csv即 comma-separated values ,逗号分隔值.是一种通用的相对简单地文件格式,目前被较为广泛的使用.其最广泛的应用就是用来转移表数据. “ ...
- 128、TensorFlow元数据MetaData
#tf.Session.run也接收一个可选的参数options #能够让你来配置训练时的参数 #run_metadata参数让你能够收集关于训练的元信息 #列如你可以使用这些可选项来追踪执行的信息 ...
- VUEJS(vuejs) 数组数据不及时刷新
在Vue对象中的methods属性中构建一个方法用于刷新data使用Vue.set方法进行手动刷新methods:{update:function(o){Vue.set(this,'list',o); ...
- CodeForce-1196D2-RGB Substring (hard version)
原题链接 题目大意与上题完全一样,只是数据规模更大. 思路: 再用上题的暴力肯定TLE,所以需要优化一下搜索过程.上一题我们是外层遍历n,内层遍历3种情况.这题我们外层遍历3种情况,内层遍历数组,记录 ...
- onblur和onkeyup事件
onblur:事件会在对象失去焦点时发生 提示:onblur 相反事件为onfocus事件 . onkeyup: 事件会在键盘按键被松开时发生. 提示:与onkeyup 事件相关的事件发生次序: on ...
- Hexo-博客yilia主题创建分类(categories)和标签(tags)首页
转载自:http://orzcss.com/posts/5a207d64/ 概述 默认安装的 hexo 本身是没有分类和标签首页的,例如:http://orzcss.com/categories/页面 ...
- mysql 数据库表结构对比语句
判断两个数据库互相不存在的表 select a.TABLE_SCHEMA,a.TABLE_NAME from information_schema.TABLES a where a.TABLE_SCH ...
- redis setNx方法
Redis有一系列的命令,特点是以NX结尾,NX是Not eXists的缩写,如SETNX命令就应该理解为:SET if Not eXists.这系列的命令非常有用,这里讲使用SETNX来实现分布式锁 ...