#Leetcode# 728. Self Dividing Numbers

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 搞得一天没有好心情了 晚上回去多学一会吧

 

posted @ 2019-04-12 19:19  丧心病狂工科女  阅读(191)  评论(0编辑  收藏  举报