作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/self-dividing-numbers/description/

题目描述

A self-dividing number is a number that is divisible by every digit it contains.

For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.

Also, a self-dividing number is not allowed to contain the digit zero.

Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.

Example 1:

Input:
left = 1, right = 22
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]

Note:

The boundaries of each input argument are 1 <= left <= right <= 10000.

题目大意

如果一个数字能被它自己的各位数字整除,那么这个数字是一个自除数字,求在[left, right]双闭区间内的所有自除数字。

解题方法

循环

用了两个函数,一个用来判断是否是dividing number,另一个用来循环和遍历。

要注意的一点是要判断0是否在num中,否则有除0错误。

dividing number 判断的有点麻烦,就是遍历每位数字。

class Solution:
def isDividingNumber(self, num):
if '0' in str(num):
return False
return 0 == sum(num % int(i) for i in str(num))
def selfDividingNumbers(self, left, right):
"""
:type left: int
:type right: int
:rtype: List[int]
"""
answer = []
for num in range(left, right+1):
print(num)
if self.isDividingNumber(num):
answer.append(num)
return answer

filter函数

参考了https://leetcode.com/problems/self-dividing-numbers/discuss/109445。
有更简单的两个函数:
all()判断是不是所有的元素都满足,
filter过滤掉不满足条件的元素。

class Solution(object):
def selfDividingNumbers(self, left, right):
is_self_dividing = lambda num: '0' not in str(num) and all([num % int(digit) == 0 for digit in str(num)])
return filter(is_self_dividing, range(left, right + 1))

As pointed out by @ManuelP, [num % int(digit) == 0 for digit in str(num)] creates an entire list which is not necessary. By leaving out the [ and ], we can make use of generators which are lazy and allows for short-circuit evaluation, i.e. all will terminate as soon as one of the digits fail the check.

The answer below improves the run time from 128 ms to 95 ms:

class Solution(object):
def selfDividingNumbers(self, left, right):
is_self_dividing = lambda num: '0' not in str(num) and all(num % int(digit) == 0 for digit in str(num))
return filter(is_self_dividing, range(left, right + 1))

数字迭代

转成字符串的方法耗时,其实可以直接使用数字求余的方法节省了大量的时间。

时间复杂度是O(N),空间复杂度是O(1)。打败98%.

class Solution:
def selfDividingNumbers(self, left, right):
"""
:type left: int
:type right: int
:rtype: List[int]
"""
res = []
for num in range(left, right + 1):
if self.isDividing(num):
res.append(num)
return res def isDividing(self, num):
temp = num
while temp:
div = temp % 10
if not div or num % div != 0:
return False
temp //= 10
return True

日期

2018 年 1 月 13 日
2018 年 11 月 5 日 —— 打了羽毛球,有点累

【LeetCode】728. Self Dividing Numbers 解题报告(Python)的更多相关文章

  1. LeetCode 728 Self Dividing Numbers 解题报告

    题目要求 A self-dividing number is a number that is divisible by every digit it contains. For example, 1 ...

  2. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  3. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  4. LeetCode 2. Add Two Numbers 解题报告

    题意: 有两个链表,它们表示逆序的两个非负数.例 (2 -> 4 -> 3)表示342,求两个数字的和,并用同样的方式逆序输出.如342+465 = 807,你需要把结果表达为(7 -&g ...

  5. LeetCode - 728. Self Dividing Numbers

    A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is ...

  6. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  7. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  8. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  9. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

随机推荐

  1. 数据分析体系 — 用户粘性的两个计算指标(DAU/MAU和月人均活跃天数)

    很多运营都了解DAU(日活跃用户数)和MAU(月活跃用户数)的重要性,但在某些情况下这两个数值本身并不能反映出太多问题,这个时候就要引用到[DAU/MAU]的概念,即[日活/月活] 用户粘性的两个计算 ...

  2. 二进制免编译My SQL

    一 下载 MySQL 安装包教程 https://blog.csdn.net/zhan107876/article/details/100701135 ll -h mysql-5.6.47-linux ...

  3. MybatisPlus入门程序

    参考资料:MybatisPlus官网 环境搭建 创建数据库 CREATE DATABASE `mybatisplus` ​ USE `mybatisplus` ​ CREATE TABLE `user ...

  4. C#判断是否有中文

    using System.Text.RegularExpressions; Regex reg = new Regex(@"[\u4e00-\u9fa5]"); if (reg.I ...

  5. 大数据学习day33----spark13-----1.两种方式管理偏移量并将偏移量写入redis 2. MySQL事务的测试 3.利用MySQL事务实现数据统计的ExactlyOnce(sql语句中出现相同key时如何进行累加(此处时出现相同的单词))4 将数据写入kafka

    1.两种方式管理偏移量并将偏移量写入redis (1)第一种:rdd的形式 一般是使用这种直连的方式,但其缺点是没法调用一些更加高级的api,如窗口操作.如果想更加精确的控制偏移量,就使用这种方式 代 ...

  6. ES5中改变this指向的三种方法

    ES5中提供了三种改变函数中this指针指向的方法,分别如下 1.call() var obj = {username:"孙悟空"}; //没有任何修饰的调用函数,函数中的this ...

  7. CSS相关,手画三角形,正方形,扇形

    三角形 实现一个三角形 <!DOCTYPE html> <html> <head> <title>三角形</title> <style ...

  8. 单链表的模板类(C++)

    /*header.h*/#pragma once #include<iostream> using namespace std; template<class T> struc ...

  9. sql技巧(增册改查)

    1 select * from wyl.t; 2 --将数据从t1导入t2 3 insert into t2(c1,c2) select c1,c2 from t1 where c1= xx and ...

  10. Oracle中IS TABLE OF的使用

    IS TABLE OF :指定是一个集合的表的数组类型,简单的来说就是一个可以存储一列多行的数据类型. INDEX BY BINARY_INTEGER:指索引组织类型 BULK COLLECT :指是 ...