Leetcode 1015. Smallest Integer Divisible by K
思路显然是暴力枚举.
但是两个问题:
1.当1的位数非常大时,模运算很费时间,会超时.
其实每次不用完全用'11111...'来%K,上一次的余数*10+1后再%K就行.
证明:
令f(n)=111111...(n个1);
g(n)=f(n)%K
因为f(n)=f(n-1)*10+1
所以f(n)%K=(f(n-1)*10+1)%K
即g(n)=g(n-1)*10+1
2.枚举何时停止?
一种方法是可以设置一个大数,比如10的6次方,可以Accepted.
更精确的方法是:从1个1到K个1,如果这里都没有答案,后面也没了.
因为K的余数不包括0的话有K-1个,我们算了K个,K个里面没有0的话,里面必然至少有两个相等的(抽屉原理),而根据第一个问题所示,相邻的余数有关系,所以一相等之后就是重复循环这些数了,前面找不到后面也肯定没有了.例如K=6:
- 1 % 6 = 1
- 11 % 6 = 5
- 111 % 6 = 3
- 1111 % 6 = 1
- 11111 % 6 = 5
- 111111 % 6 = 3
class Solution:
def smallestRepunitDivByK(self, K: int) -> int:
if K % 2 == 0 or K % 5 == 0:
return -1
g = 0
for i in range(1, K+1):
g = (g * 10 + 1) % K
if g == 0:
return i
return -1
Leetcode 1015. Smallest Integer Divisible by K的更多相关文章
- 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【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 ...
- [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] K-th Smallest Prime Fraction 第K小的质分数
A sorted list A contains 1, plus some number of primes. Then, for every p < q in the list, we co ...
- Leetcode 974. Subarray Sums Divisible by K
前缀和(prefix sum/cumulative sum)的应用. 还用了一个知识点: a≡b(mod d) 则 a-b被d整除. 即:a与b对d同余,则a-b被d整除. class Solutio ...
- [LeetCode] K-th Smallest in Lexicographical Order 字典顺序的第K小数字
Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. N ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- 【LeetCode】974. Subarray Sums Divisible by K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 前缀和求余 日期 题目地址:https:/ ...
- 119th LeetCode Weekly Contest Subarray Sums Divisible by K
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...
随机推荐
- window.event.keycode值大全
window.event.keycode值大全 event.keycode值大全 1 keycode 8 = BackSpace BackSpace 2 keycode 9 = Tab Tab 3 k ...
- python2中range和xrange的区别
range和xrange用法相同,不同的是xrange不是生成一个序列,而是作为一个生成器,即生成一个取出一个 相对来说,xrange比range性能优化很多,因为不需要一下子开辟一块很大的内存,特别 ...
- JSP SERVLET 基础知识
jsp(java server page)和servlet是JAVA EE规范的两个基本成员,是JAVA WEB开发的重点也是基础知识.JSP本质上也需要编译成SERVLET运行. JSP比较简单,可 ...
- hive--udf函数(开发-4种加载方式)
UDF函数开发 标准函数(UDF):以一行数据中的一列或者多列数据作为参数然后返回解雇欧式一个值的函数,同样也可以返回一个复杂的对象,例如array,map,struct. 聚合函数(UDAF):接受 ...
- Java遍历List集合的三种方法
Java遍历List集合的三种方法 List<String> list = new ArrayList<String>(); list.add("aaa") ...
- jQuery单选框跟复选框美化
在线演示 本地下载
- spring security结合数据库验证用户-XML配置方式
之前的用户信息我们都是使用的内存用户,测试例子可以,实际中使用肯定不行,需要结合数据库进行验证用户.这就是本节的重点: 项目目录如下: 在之前的项目中的依赖中添加两个依赖: <dependen ...
- TP多条件sql查询,分组排序
$k=M('order a'); $bj=$k->join("left join __CHANGE__ b on b.tb_name='order'and a.order_id=b.t ...
- 第二篇:Spark SQL Catalyst源码分析之SqlParser
/** Spark SQL源码分析系列文章*/ Spark SQL的核心执行流程我们已经分析完毕,可以参见Spark SQL核心执行流程,下面我们来分析执行流程中各个核心组件的工作职责. 本文先从入口 ...
- Pandas排序
Pandas有两种排序方式,它们分别是 - 按标签 按实际值 下面来看看一个输出的例子. import pandas as pd import numpy as np unsorted_df=pd.D ...