【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 ...
随机推荐
- Sublime Text3 代码编辑器使用笔记
Sublime Text3 作为一款代码的文本编辑器,有许多插件,这一点是我认为 Sublime Text3 很强大的原因之一.插件的安装可以参考下面的文章. Sublime Text3 插件安装教程 ...
- EZOJ #389点分治好题
分析 一层一层把叶子去掉 看最多能去掉多少层即可 代码 #include<bits/stdc++.h> using namespace std; ],du[],fa[],n,m,ans; ...
- threading.get_ident()
https://docs.python.org/3/library/threading.html Return the 'thread identifier' of the current threa ...
- day26—JavaScript对CSS样式的获取和修改实践
转行学开发,代码100天——2018-04-11 通过JavaScript获取和修改HTML元素及CSS属性是其一个基本功能.对于CSS样式通常有行内样式,外部样式,内嵌样式之分. 如: 行内样式: ...
- jmeter之JDBC请求
jmeter不仅可以测试http请求,也可以执行JDBC请求的测试.本次以mysql为例,介绍JDBC请求如何完成发送 目录 1.环境配置 2.数据库连接配置 3.添加一个JDBC请求 1.环境配置 ...
- linux下的sleep()和usleep()的使用和区别
函数名: sleep头文件: #include<windows.h> // 在VC中使用带上头文件 #include<unistd.h> // ...
- Python3数据科学入门与实践学习教程
整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大家看的时候可以关注下面几点: 1.为了追求精 ...
- 洛谷P5018 对称二叉树——hash
给一手链接 https://www.luogu.com.cn/problem/P5018 这道题其实就是用hash水过去的,我们维护两个hash 一个是先左子树后右子树的h1 一个是先右子树后左子树的 ...
- spring-第六篇之创建bean的3种方式
1.创建bean的方式有3种: 1>使用构造器创建bean,即设值注入.构造注入本质都是使用bean的构造器创建bean的. 2>使用静态工厂方法创建bean. 3>调用实例工厂方法 ...
- Hibernate使用时需要注意的几个小问题
今天晚上玩了一下JDBC连接数据库,之后又利用Hibernate进行了数据库的访问,感觉利用Hibernate对数据库访问在文件配置好了之后确实更加简单快捷. 但是在操作的过程中也有一些细节需要注意一 ...