博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode讲解--728. Self Dividing Numbers
阅读量:6927 次
发布时间:2019-06-27

本文共 1205 字,大约阅读时间需要 4 分钟。

728. Self Dividing Numbers

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 = 22Output: [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.

这道题需要注意几点:

  1. 不要直接对i进行操作,而是复制一份,对复制品进行操作
  2. 对0的过滤,0不能做除数
  3. while的控制条件应该是num!=0,而非num/10!=0,否则会过滤掉个位数

Java代码

class Solution {    public List
selfDividingNumbers(int left, int right) { List
result = new ArrayList<>(); for(int i=left;i<=right;i++){ boolean flag = true; int num=i; while(num!=0){ int n = num%10; if(n==0 || i%n!=0){ flag = false; break; } num=num/10; } if(flag){ result.add(i); } } return result; }}

转载地址:http://kfujl.baihongyu.com/

你可能感兴趣的文章
我们工作到底为了什么
查看>>
关于“VisualSVN-2.5.2”的破解
查看>>
用jQuery判断两个元素是否有重叠部分
查看>>
windows 下安装Simplejson方法
查看>>
IE并发连接限制(as)AS队列加载类(as3和as2)
查看>>
转:Android View.post(Runnable )
查看>>
ChinaTest第二天
查看>>
图灵等价和图灵完备
查看>>
CSS中position的absolute和relative的应用
查看>>
对 makefile 中 二次扩展的一点理解
查看>>
SET XACT_ABORT on
查看>>
记录mysql性能查询过程
查看>>
数据连接 DataDirectory 中的作用
查看>>
持续更新 iText in Action 2nd Edition中文版 个人翻译
查看>>
树、森林和二叉树的转换
查看>>
SSH重新登录的问题
查看>>
sys.path.insert(0, os.path.join('..', '..', '..', '..','...')) 解释
查看>>
开启mysql慢查询日志
查看>>
WEB项目的分层结构
查看>>
如果通过key获取dictionary里面的value
查看>>