728. Self Dividing Numbers

Posted gsz-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了728. 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.

解题思路:

一直思考有没有什么技巧,思考半天,没有头绪,最后使用暴力枚举。

代码:

 1 class Solution {
 2 public:
 3     vector<int> selfDividingNumbers(int left, int right) {
 4         vector<int> ret;
 5         for (int i = left; i <= right; ++i) {
 6             if (check(i))
 7                 ret.push_back(i);
 8         }
 9         return ret;
10     }
11     bool check(int num) {
12         int tmp = num;
13         while (tmp != 0) {
14             int div = tmp % 10;
15             if (!div)
16                 return false;
17             if (num % div != 0)
18                 return false;
19             tmp /= 10;
20         }
21         return true;
22     }
23 };

 

以上是关于728. Self Dividing Numbers的主要内容,如果未能解决你的问题,请参考以下文章

728. Self Dividing Numbers

728. Self Dividing Numbers

728. Self Dividing Numbers

[leetcode-728-Self Dividing Numbers]

LeetCode 728. Self Dividing Numbers

LeetCode 728. Self Dividing Numbers