题目链接:https://leetcode-cn.com/problems/self-dividing-numbers/
给定上边界和下边界数字,输出一个列表,列表的元素是边界(含边界)内所有的自除数。
示例 1:
输入:
上边界left = 1, 下边界right = 22
输出: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
注意:
每个输入参数的边界满足 1 <= left <= right <= 10000。
1 /** 2 * Note: The returned array must be malloced, assume caller calls free(). 3 */ 4 int* selfDividingNumbers(int left, int right, int* returnSize){ 5 int* a=calloc(right-left+1,sizeof(int)); 6 int i=left; 7 int t=0; 8 while(i<=right){ 9 if(i<10){ 10 a[t++]=i; 11 }else{ 12 int flag=1; 13 int x=i; 14 while(x){ 15 if(x%10==0){ 16 flag=0; 17 break; 18 }else if(i%(x%10)!=0){ 19 flag=0; 20 break; 21 } 22 x/=10; 23 } 24 if(flag) a[t++]=i; 25 } 26 i++; 27 } 28 *returnSize=t; 29 return a; 30 }