Algorithm--leetcode 728. Self dividing Numbers

Source: Internet
Author: User

Topic:

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

For example, 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 Possib Le.

Test instructions

Self-dividing number can be understood as a self divisor, and the third line shows that the divisor cannot contain numbers 0

Rules for judging self-divisor:

Separate a number num by bit (using the take-up operation%10 each time the single digit is taken out) to determine whether the number num can be divided by the number of separated, if all the bits can be divisible by the current number of NUM, then this number num is the self divisor

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

Enter the minimum and maximum values (left and right) to determine all the divisor within this range and return a List

Python:
classsolution (object):defselfdividingnumbers (self, left, right):""": Type Left:int:type right:int:rtype:list[int]"""Rat=[]         forIinchRange (left,right+1): Num=Len (str (i)) flag=1J=I while(i): t=i%10if  notTorJ%t! =0:flag=0 BreakI/=10ifFlag:rat.append (j)returnRat
Grammar Summary:

Rat=[] can declare an empty list, and if the current number is a self-divisor, use Rat.append () to add the current number to the list

When you use the left and right scopes, you need to right+1

If after must follow a colon

Num=len (str (i)) can find the number of digits of an int type

Algorithm--leetcode 728. Self dividing Numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.