C++ Straightforward, 3ms


  • 0
    M
    class Solution {
    public:
        vector<int> selfDividingNumbers(int left, int right) {
            vector<int> result;
            int t = 0, r = 0;
            bool is_sd = false;
            for (int n = left; n <= right; ++n) {
                t = n;
                is_sd = true;
                while (t) {
                    r = t % 10;
                    if (r == 0 || n % r != 0) {
                        is_sd = false;
                        break;
                    }
                    t /= 10;
                }
                if (is_sd) result.push_back(n);
            }
            return result;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.