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 = 22
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道怎么取出数字中的每一位数:mod%取余,然后每次除10就可以了
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 有数字不变的要求:原来的数要固定住,才能自己除以自己
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
不知道怎么取出数字中的每一位数:mod%取余,然后每次除10就可以了
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
取余、除10,很方便
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> res = new LinkedList<Integer>(); for (int i = left; i <= right; i++) {
if (isSelfDividingNumbers(i)) res.add(i);
} return res;
} public boolean isSelfDividingNumbers(int n) {
int original = n;
while (n != 0) {
int res = n % 10;
if (n % 10 == 0) return false;
if (original % res != 0) return false;
n /= 10;
} return true;
}
}
最新文章
- 学习 opencv---(6)玩转opencv源代码:生成opencv 工程解决方案与opencv 源码编译
- sharepoint2013的审核日志的时间区域设置
- 27 GroupSock概述(一)——live555源码阅读(四)网络
- matlab函数bwareaopen的详解
- [清理页面缓存]asp.net、html
- 网络爬虫urllib2 tornado
- 关于APP自动化工程的一点小想法
- 4 Values whose Sum is 0
- Hadoop集群(第10期副刊)_常用MySQL数据库命令
- 对于WIFI版ipad(无GPS芯片)定位功能的释疑
- [React] React Router: Nested Routes
- python接口自动化(十六)--参数关联接口后传(详解)
- CentOS7安装MySQL8.0图文教程
- 计算pi的位数
- jQuery EasyUI 选项卡面板tabs使用实例精讲
- MySQL数据库视图(view),视图定义、创建视图、修改视图
- 【转】LoadRunner压力测试:测试报告结果分析
- SSKeychain
- 微信公众号 JSSDK 提示:invalid signature
- Docker 技巧:删除 Docker 所有镜像