本系列博客为个人刷题思路分享,有需要借鉴即可。
1.目录大纲:
2.题目链接:
T1:LINK
T2:LINK
3.详解思路:
T1:
思路:
/*** Note: The returned array must be malloced, assume caller calls free().*/#include<stdlib.h>int* selfDividingNumbers(int left, int right, int* returnSize) {int j = 0;int count = right - left + 1;//用来计数,看看几个合法数字//开辟空间int* arr = (int*)malloc(count * sizeof(int));//生成left到right的所有数字int i = left;for (i = left; i <= right; i++){//每生成一个数字得检验一下是不是自除数字//得想办法得到该数字的每一位int a = i;int flag = 0;//标记是否为自除数,默认为是while (a){if (a % 10 == 0){flag = 1;count--;break;}if (i % (a % 10) != 0){flag = 1;//标记一下不是,直接下一个count--;break;}a /= 10;}//存入合法数字if (flag == 0){arr[j++] = i;}}//返回有效个数*returnSize = count;return arr;
}
T2:
思路:
/*** Note: The returned array must be malloced, assume caller calls free().*/
int* productExceptSelf(int* nums, int numsSize, int* returnSize) {//开辟新的数组int i = 0;//下标作用int* answer = (int*)malloc(sizeof(int)*numsSize);//新数组//先把新数组所有数值置为1for(i = 0;i<numsSize;i++){answer[i] = 1;}//乘上前半部分int x = nums[0];for(i = 1;i<numsSize;i++){answer[i] *= x;x*=nums[i];}//乘上后半部分x = nums[numsSize-1];for(i = numsSize-2;i>=0;i--){answer[i]*=x;x*=nums[i];}//返回*returnSize = numsSize;return answer;
}
完。