LeetCode728. 自除数
自除数 是指可以被它包含的每一位数除尽的数。
例如,128 是一个自除数,因为 128 % 1 == 0,128 % 2 == 0,128 % 8 == 0。
还有,自除数不允许包含 0 。
给定上边界和下边界数字,输出一个列表,列表的元素是边界(含边界)内所有的自除数。
示例 1:
输入:
上边界left = 1, 下边界right = 22
输出: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
class Solution {
public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> list = new ArrayList<Integer>();
for(int i = left;i<right+1;i++){
if(0<i && i<10){
list.add(i);
}
if(10<=i && i<100){
int a = i % 10;
int b = i /10 % 10;
if(a != 0 && b!=0){
if(i % a ==0 && i%b ==0){
list.add(i);
}
}
}
if(99<i && i<1000){
int a = i % 10;
int b = i /10 % 10;
int c = i /100 % 10;
if(a != 0 && b!=0 &&c!=0){
if(i % a ==0 && i % b ==0 && i% c ==0){
list.add(i);
}
}
}
if(999<i && i<=10000){
int a = i % 10;
int b = i/10 % 10;
int c = i/100 % 10;
int d = i/1000 % 10;
if(a != 0 && b!=0 &&c!=0 &&d!=0){
if(i % a ==0 && i % b ==0 && i% c ==0 && i%d==0){
list.add(i);
}
}
}
}
return list;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
解法二
class Solution {
private boolean isDividing(int num){
int tmp=num;
while(tmp!=0){
if(tmp%10==0) return false;
if(num%(tmp%10)!=0) return false;
else tmp/=10;
}
return true;
}
public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> intlist=new ArrayList<Integer>();
for(int i=left;i<=right;i++){
if(isDividing(i)==true) intlist.add(i);
}
return intlist;
}
}
LeetCode728. 自除数的更多相关文章
- [Swift]LeetCode728. 自除数 | Self Dividing Numbers
A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is ...
- Leetcode728.Self Dividing Numbers自除数
自除数 是指可以被它包含的每一位数除尽的数. 例如,128 是一个自除数,因为 128 % 1 == 0,128 % 2 == 0,128 % 8 == 0. 还有,自除数不允许包含 0 . 给定上边 ...
- C# 刷遍 Leetcode 面试题系列连载(3): No.728 - 自除数
前文传送门: C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介 C#刷遍Leetcode面试题系列连载(2): No.38 - 报数 系列教程索引 传送门:https://enjoy2 ...
- java语言中除数为零问题
在以下几个例子中,输出结果如何? float aa=0; System.out.println(aa/0); System.out.println(1/aa); System.out.println( ...
- sql server中除数为零的处理技巧
在sql server中做除法处理的时候,我们经常需要处理除数为零的情况,因为如果遇到这种情况的时候,sqlserver会抛出遇到以零作除数错误的异常,我们总不希望把这个异常显示给用户吧. 做个会报这 ...
- Hdu 1452 Happy 2004(除数和函数,快速幂乘(模),乘法逆元)
Problem Description Considera positive integer X,and let S be the sum of all positive integer diviso ...
- 成本卷积报错:CSTPSCEX.explode_sc_cost_flags():40:ORA-01476: 除数为 0
成本卷积请求:供应链成本累计 - 打印报表 运行后报一下错误: MSG-00000: Rollup ID = 236403MSG-00000: Before CSTPSCEX.supply_chain ...
- js使用栈来实现10进制转8进制 js取除数 余数
function ten2eight(x){ var s=[]; var r=''; while(x>0){ s.push(x%8); x=parseInt(x/8); } while(s.le ...
- nest exception is java.sql.SQLException:ORA-01476:除数为0
1.错误描述 nest exception is java.sql.SQLException:ORA-01476:除数为0 2.错误原因 3.解决办法
随机推荐
- iOS 让部分ViewController支持屏幕旋转
首先,在Xcode里设置整个项目支持的屏幕显示方向: 然后创建一个UINavigationController的子类,然后重载以下属性: 对于需要自定义屏幕方向的ViewController,重载这个 ...
- mybaits 连接数据库汉字保存乱码??
查看数据库连接地址: jdbc.url=jdbc:mysql://localhost:3306/az?useUnicode=true&characterEncoding=utf-8 多了一个a ...
- HDOJ 5475 An easy problem
题目传送门 题意:一个计算器,两种操作,乘上x,或者除掉之前的某个x,结果取模输出 分析:因为取模不支持除法,然后比赛时想到用逆元,结果发现MOD需要与b互质,结果一直苦苦寻找求逆元的其它方法.后来队 ...
- 513 Find Bottom Left Tree Value 找树左下角的值
给定一个二叉树,在树的最后一行找到最左边的值. 详见:https://leetcode.com/problems/find-bottom-left-tree-value/description/ C+ ...
- css中常见中文字体的英文名称
曾经看过一些文章,建议CSS中字体应用英文来替代,但一直未引起我重视.最近官网改版,今天同事测试发现Mac的Safari总是显示宋体 → → 修改font-family:"微软雅黑" ...
- [转]利用telnet进行SMTP的验证
本文转自:http://www.cnblogs.com/rootq/articles/1320266.html [crazywill@localhost crazywill]$ telnet #tel ...
- 使用mysql作为配置文件的地址
server端配置 POM文件 <dependency> <groupId>org.springframework.boot</groupId> <artif ...
- CSS层叠的问题、标准文档流、伪类选择器
一.层叠的问题 CSS有两个性质: 1.继承性 2.层叠性:选择器的一种选择能力,谁的权重大就选谁 层叠性又分为: 1).选不中:走继承性 (font.color.text.) 继承性的权重是0 若 ...
- python中的get函数
>>> a={1:'a',2:'b'}>>> print a.get(1)a>>> print a.get(3)None
- 原生js的容易忽略的相似点(二)
1.new Object 和字面量 {}测试; <script type="text/javascript"> //1.new出来对象 console.log(obj, ...