https://leetcode.com/problems/self-dividing-numbers/description/
A 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 == 0, 128 % 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.

bool checkIsSelfDividingNumber(int num)
{
	if (num < 10)
		return true;
	int tmp = num;
	while (tmp > 1)
	{
		int ge = tmp % 10;
		if (ge == 0)
		{
			return false;
		}
		else if (num % ge != 0)
		{
			return false;
		}
		tmp = tmp / 10;
		
	}
	return true;
}

// 728. Self Dividing Numbers
// For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. 
// ac
vector<int> solution::selfDividingNumbers(int left, int right)
{
	vector<int> res ;

	for (int i = left; i <= right; ++i)
	{
		if (checkIsSelfDividingNumber(i))
		{
			res.push_back(i);
		}
	}
	return res;
}
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐