首页 > 其他 > 详细

#Leetcode# 728. Self Dividing Numbers

时间:2019-04-12 19:33:13      阅读:99      评论:0      收藏:0      [点我收藏+]

https://leetcode.com/problems/self-dividing-numbers/

 

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.

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.

代码:

class Solution {
public:
    vector<int> selfDividingNumbers(int left, int right) {
        vector<int> ans;
        for(int i = left; i <= right; i ++) {
            if(iself(i)) ans.push_back(i);
        }
        return ans;
    }
    bool iself(int x) {
        int y = x;
        while(x) {
            if(x % 10 == 0) return false;
            if(y % (x % 10)) return false;
            x /= 10;
        }
        return true;
    }
};

  今天 LeetCode 五题打卡 杭州阴天三天之后天气终于放晴 准备提前回去跑步啦 被 extgcd 搞得一天没有好心情了 晚上回去多学一会吧

@fh

#Leetcode# 728. Self Dividing Numbers

原文:https://www.cnblogs.com/zlrrrr/p/10697978.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!