当前位置:网站首页>力扣 自除数 C语言 题解
力扣 自除数 C语言 题解
2022-06-10 00:06:00 【6映辰】
力扣 728.自除数 解法
一、完整题目
自除数 是指可以被它包含的每一位数整除的数。
例如,128 是一个 自除数 ,因为 128 % 1 == 0,128 % 2 == 0,128 % 8 == 0。
自除数 不允许包含 0 。
给定两个整数 left 和 right ,返回一个列表,列表的元素是范围 [left, right] 内所有的自除数。
示例 1:
输入:left = 1, right = 22
输出:[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
示例 2:
输入:left = 47, right = 85
输出:[48,55,66,77]
提示:
1 <= left <= right <= 104
二、解题思路
- 首先,要开辟一定的空间(Right-Left+1)。
- 其次,将指针 * returnSize 置 0 ,指向空间的首地址。
- 再次,构造一个外循环 Left<=i<=Right ,将此范围内的数据都判断一遍。
- 然后,构造内循环,用来判断是否满足自除数条件,从最低位开始判断,即做取余于 10 的操作,直至取余结果为 0 退出循环(此时退出,表明此轮循环的数满足条件)。如果,此轮的数取余于此位的结果不为 0 ,则不满足条件,退出循环。除去以上两种情况,便将数取整于10,继续循环,直至结束。
- 最后,如果此轮数退出内循环时,已经变成 0 ,说明所有位都满足自除数的条件,即为我们寻找的数,将其记录在开辟的空间内,并将指针向后移动一位。
三、编写代码
/** * Note: The returned array must be malloced, assume caller calls free(). */
int* selfDividingNumbers(int left, int right, int* returnSize){
int *results=(int *)malloc(sizeof(int)*(right-left+1));
(*returnSize)=0;
int i,tmp,base;
for(i=left;i<=right;i++)
{
tmp=i;
while(tmp)
{
base=tmp%10;
if(base==0) break;//结束条件
if(i%base!=0) break;//不符合条件
tmp=tmp/10;
}
if(tmp==0) results[(*returnSize)++]=i;
}
return results;
}
四、测评结果
五、总结评价
这是力扣目前通过率最高的一道题,是最简单的题,希望可以通过这道题,熟悉答题环境与操作,规范书写,即为所谓的 “上道” ,希望大家努力刷题吧!
有问题欢迎各位大佬指出
力扣系列将持续更新,欢迎关注,一起学习
边栏推荐
- Please ask a question. PG is similar to MySQL server_ Is the instance with the same ID unique?
- On chip variation (OCV) concept learning
- 929. 独特的电子邮件地址
- wps怎么合并大小不一致的单元格
- Basic and introductory knowledge for PHP learning
- 约翰霍普金斯、加州伯克利等 | 利用序列和结构特征的机器学习实现抗体热稳定性的泛化预测
- Enterprise worry free | apipost privatization deployment activity is about to start
- Several syntax for adding events to elements in a page
- adb shell wm 命令使用
- MySQL执行计划
猜你喜欢

10个常见触发IO瓶颈的高频业务场景

On chip variation (OCV) concept learning

数字大时代来临,360携手创业黑马助力中小企业抓住关键未来

SIGIR 2022 | 港大、武大提出KGCL:基于知识图谱对比学习的推荐系统

Who says redis can't save big keys

JVM explanation

AQUANEE将在近期登陆Gate以及BitMart,低位布局的良机

MySQL development practice summary (I)

SPSS principal component analysis

Basic and introductory knowledge for PHP learning
随机推荐
嵌入式商业智能软件Wyn Enterprise正式步入V5.0时代!
Introduction to bias and variance
MySQL开发实战总结(一)
可重复读隔离级别的基石MVCC机制
Performance optimization scheme
低边驱动和高边驱动
10个常见触发IO瓶颈的高频业务场景
Masao Kudo: how to publish 5 SCI articles a year
Is Huishang futures regular? Is it safe to open an account?
Retrofit2.0 method summary of adding header
Gartner 2021年全球IaaS报告:AWS蛋糕被蚕食,中国云厂商稳步进击
numpy基础操作
Spingboot+quartrz cluster version realizes dynamic timing tasks (using reflection to realize custom services)
請問徽商期貨是正規的嗎?開戶交易安全嗎?
Sarsa
Blue Bridge Cup_ Split cube_ Combinatorial mathematics_ Addition principle
Sparksql source code series | to understand the distribution source code system (spark3.2)
DDD driven domain design learning notes
Dynamic reading of protobuf data
Process test supports batch parameter import, and the test efficiency is directly full!