LeetCode 728 Self Dividing Numbers 解题报告

题目要求

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 == 0128 % 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.

题目分析及思路

题目给出一个整数范围,要求输出该范围中的所有self-dividing number(包含边界数字)。该数字的特点是能被它所包含的所有整数整除且不包含零。可以遍历范围内的所有数字,对每个数字进行判断,符合要求的推进返回的列表中。判断条件的设置使用循环的方式,循环终止的条件应当是每次取余得一位数,那一位数为零则判断停止,循环内部拿到那一位数后对整体数字进行取余,若不为零同样跳出循环。出循环后通过判断剩下的位数的数字是否空了,若空了则说明符合条件,推入返回列表,否则不满足。

python代码?

class Solution:

def selfDividingNumbers(self, left, right):

"""

:type left: int

:type right: int

:rtype: List[int]

"""

res = []

for i in range(left,right + 1):

temp = i

while(temp % 10):

if i % (temp % 10) != 0:

break

temp //= 10

if temp == 0:

res.append(i)

return res

原文地址:https://www.cnblogs.com/yao1996/p/10326036.html

时间: 2024-09-28 17:38:55

LeetCode 728 Self Dividing Numbers 解题报告的相关文章

【LeetCode】Add Two Numbers 解题报告

[题目] You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -

LeetCode 2. Add Two Numbers 解题报告

题意: 有两个链表,它们表示逆序的两个非负数.例 (2 -> 4 -> 3)表示342,求两个数字的和,并用同样的方式逆序输出.如342+465 = 807,你需要把结果表达为(7 ->0 ->8). 思路: 模拟一下加法的运算过程,从个位开始加,进位保存下来,十位运算的时候把个位的进位加上,依次类推. C++ Code /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNod

LeetCode: Pascal's Triangle II 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to us

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa

【LeetCode】Course Schedule II 解题报告

[题目] There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses an

【LeetCode】Jump Game II 解题报告

[题目] Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of

【LeetCode】Unique Paths II 解题报告

[题目] Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the

USACO Section2.2 Runaround Numbers 解题报告 【icedream61】

runround解题报告------------------------------------------------------------------------------------------------------------------------------------------------[题目] 给你一个数M,找出第一个比它大的循环数. 循环数:不包括0.没有重复数字,并且有循环性质的正整数. 循环性质:以81362为例 1.找到最高位,是8,那么往下数8位,依次是1,3

【LeetCode】Word Break II 解题报告

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat", "cats&quo