<p>A <strong>self-dividing number</strong> is a number that is divisible by every digit it contains.</p>

<ul>
	<li>For example, <code>128</code> is <strong>a self-dividing number</strong> because <code>128 % 1 == 0</code>, <code>128 % 2 == 0</code>, and <code>128 % 8 == 0</code>.</li>
</ul>

<p>A <strong>self-dividing number</strong> is not allowed to contain the digit zero.</p>

<p>Given two integers <code>left</code> and <code>right</code>, return <em>a list of all the <strong>self-dividing numbers</strong> in the range</em> <code>[left, right]</code> (both <strong>inclusive</strong>).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> left = 1, right = 22
<strong>Output:</strong> [1,2,3,4,5,6,7,8,9,11,12,15,22]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> left = 47, right = 85
<strong>Output:</strong> [48,55,66,77]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= left &lt;= right &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution {
public:
    vector<int> selfDividingNumbers(int left, int right) {
        using entry = std::pair<int, int>;
        std::vector<entry> result;
        std::vector<int> fin;
        
        for (int start = left; start <= right; ++start) {
            int lastDigit = start%10;
            if (!lastDigit) {
                continue;
            }
            
            if (start%lastDigit) {
                continue;
            }
            result.emplace_back(std::make_pair(start, start/10));
        }
        
        while(true)
        {
            std::vector<entry> tmp;
            int cnt = 0;
            for (auto [val, remainder] : result) {
                if (remainder == 0) {
                    fin.emplace_back(val);
                    cnt++;
                    continue;
                }
                int lastDigit = remainder%10;
                if (!lastDigit) {
                    continue;
                }
            
                if (val%lastDigit) {
                    continue;
                }
                tmp.emplace_back(std::make_pair(val, remainder/10));
            }
            result.swap(tmp);
            if (cnt == result.size()) {
                break;
            }
        }
        
        return fin;
    }
};
8700