LeetCode728 Self Dividing Numbers

限时加码!20+主流AI编程工具免费用 购周边加赠Coding Plan Lite,Claude Code、Cursor等即刻畅享,学习进阶更高效! 阅读详情

题目:

       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.

public class SelfDividingNumbers {
    public static List<Integer> selfDividingNumbers(int left, int right) {
        List<Integer> resultList = new ArrayList<>();

        if (left < 1){
            resultList.add(0);
            return resultList;
        }

        for (int num = left; num <= right ; num++) {
            if (selfDividing(num))
                resultList.add(num);
        }
        return resultList;
    }

    public static boolean selfDividing(int num){
        int x = num;

        while (x > 0){
            int y = x % 10;

            if (y == 0){
                return false;
            }
            if (num % y != 0){
                return false;
            }

            x = x / 10;
        }

        return true;
    }

    public static void main(String[] args) {
        System.out.println(selfDividingNumbers(1,22).toString());
    }
}
LeetCode 分割整组,分割为两部分的和相等 /*! * @file 组分割.cpp * @Date: 2018/03/22 15:03 * @author: sicaolong * @Contact: sicaolong@163.com * @brief: 将组分割成两部分,两部分的和相等的部分;判断是否存在这样的分割 思路1:递归:1、先求出这个组的和, 2、看这个和能不能被2整除,可以的话再进行... 阅读详情

相关推荐

Leetcode 1013:将组分成和相等的三个部分(超详细的解法!!!)

给定一个整组 A,只有我们可以将其划分为三个和相等的非空部分时才返回 true,否则返回 false。 形式上,如果我们可以找出索引 i+1 < j 且满足 (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1]) 就可以将组三等分。 示例 ...

coordinate的博客 2437

[LeetCode] 728.Self Dividing Numbers

[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,

user_name_is_so_long的博客 538

leetcode-728. Self Dividing Numbers

728. Self Dividing Numbersself-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, a

link98 1777

Leetcode 728 Self Dividing Numbers

class Solution { public List<Integer> selfDividingNumbers(int left, int right) { List<Integer> ans=new ArrayList<Integer>(); for(int i=left;i<=right;++i){ ...

Polanwind 157

LeetCode //C - 728. Self Dividing Numbers

Self-dividing numbers are numbers divisible by every digit they contain, excluding those with the digit zero. Given a range [left, right], the task is to identify all such numbers within this range. The solution involves iterating through each number in th

Made in Code 1201

LeetCode728. Self Dividing Numbers

题目:自除思路:利用除以10的商和余,依次获取每一位上的字,然后做出相应的判断即可【需要注意的是求商和取余的顺序问题】#include &lt;iostream&gt; #include &lt;vector&gt; using namespace std; class Solution { public: vector&lt;int&gt; selfDividingNumber...

Mint_r的博客 398

leetcode728. 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-dividin...

ghscarecrow的博客 363

LeetCode 728. Self Dividing Numbers

LeetCode 728. Self Dividing NumbersDescriptionA 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 ==

Vimer.Bai的博客 273

[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-dividi...

a342500329a的专栏 228

Leetcode728. Self Dividing Numbers

题目地址: https://leetcode.com/problems/self-dividing-numbers/ 给定一个正整区间范围[l,r][l, r][l,r],挑出所有能被其各个位上的整除的字(不允许其中某一位为000)。直接逐个判断即可。代码如下: import java.util.ArrayList; import java.util.List; public class Solution { public List<Integer> selfDividingNu

数学、算法爱好者的博客 161

Leetcode学习笔记:#728. Self Dividing Numbers

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,...

ccystewart的博客 201

leetCode刷题记录之728Self Dividing Numbers

leetCode刷题记录之728 原题 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. Al...

gunsmoke的专栏 169

[leetcode]728. Self Dividing Numbers

[leetcode]728. Self Dividing Numbers Analysis ummmm~—— [ummmm~] A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is a self-dividing number b...

weixin_32135877的博客 169

[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-div...

dfojc86648的专栏 92
上一篇: LeetCode33 Search in Rotated Sorted Array
下一篇: LeetCode747 Largest Number At Least Twice of Others
zhangjun62
博客等级 码龄11年 39粉丝 112原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值