(Easy) Self Dividing Numbers LeetCode

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.

Accepted

89,675

Submissions

126,086

Solution:

class Solution {
    public List<Integer> selfDividingNumbers(int left, int right) {

        List<Integer> arr  = new ArrayList<Integer> ();

        for(int i = left; i<=right; i++){

            if(Check(i)){

                arr.add(i);
            }
        }

        return arr;

    }

    boolean Check(int n){

        String a = Integer.toString(n);

        for(int i = 0; i < a.length(); i++ ){

            if(a.charAt(i)==‘0‘){
                return false;
            }
        }

        int digit =0;
        int tmp = n;
        do{

            digit = tmp%10;

            if(n%digit!= 0){
                return false;
            }

            tmp = tmp/10;

        }
        while(tmp>0);

        return true;
    }
}

原文地址:https://www.cnblogs.com/codingyangmao/p/11396034.html

时间: 2024-08-30 18:01:32

(Easy) Self Dividing Numbers LeetCode的相关文章

Add Two Numbers leetcode java

题目: 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) + (4 -> 6 -&

Sum Root to Leaf Numbers leetcode java

题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2

[LeetCode] 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 == 0, 128 % 2 == 0, and 128 % 8 == 0. Also, a self-dividing number is not allowed to contain the digit zero. G

[LeetCode&amp;Python] Problem 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 == 0, 128 % 2 == 0, and 128 % 8 == 0. Also, a self-dividing number is not allowed to contain the digit zero. G

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 == 0, 128 % 2 == 0, and 128 % 8 == 0. Also, a self-dividing number is not allowed to contain the digit ze

Add Two Numbers | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/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 i

Sum Root to Leaf Numbers——LeetCode

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

Add Two Numbers Leetcode

You are given two non-empty linked lists representing two non-negative integers. 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. You may assume the two numbers

165. Compare Version Numbers Leetcode Python

Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character. The . charac