Leetcode:728. Self Dividing Numbers
来源:互联网 发布:java中的方法 编辑:程序博客网 时间:2024/05/29 10:08
问题重述:
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.
考虑到时间复杂度的优化应该少用for循环,利用python中参数类型强制转换,且str类型是可迭代类型:
for digit in str(num))
遍历digit寻找全部符合要求的非零数字,采用filter()函数来代替fo,filter的用法见:http://blog.csdn.net/SeeTheWorld518/article/details/46975897,简化代码:
def selfDividingNumbers(left, right): dividing = lambda num: '0' not in str(num) and all(num % int(digit) == 0 for digit in str(num)) return filter(dividing, range(left, right + 1))
阅读全文
0 0
- 【LeetCode】728. Self Dividing Numbers
- 【LeetCode】728. Self Dividing Numbers
- leetcode-728. Self Dividing Numbers
- leetcode 728. Self Dividing Numbers
- Leetcode:728. Self Dividing Numbers
- LeetCode 728. Self Dividing Numbers
- [LeetCode] 728.Self Dividing Numbers
- LeetCode 728. Self Dividing Numbers
- [LeetCode]728. Self Dividing Numbers
- LeetCode | 728. Self Dividing Numbers
- [LeetCode] 728. Self Dividing Numbers
- Self Dividing Numbers(leetcode)
- LeetCode Self Dividing Numbers
- Leetcode——728. Self Dividing Numbers
- LeetCode 728. Self Dividing Numbers with Java
- [Leetcode从零开刷]728. Self Dividing Numbers
- 728. Self Dividing Numbers
- 728. Self Dividing Numbers
- 判断字符串中是否包含特定字符的方法总结
- 100种数据恢复的方法?!
- Android spinner点击相同选项处理无法响应事件问题,自定义spinner
- controller跳转controller
- Spring framework(1):框架构建&配置文件
- Leetcode:728. Self Dividing Numbers
- poj2965 The Pilots Brothers' refrigerator(枚举)
- cursor取值
- 【Struts2】8.OGNL
- 软阈值(Soft Thresholding)函数解读
- 如何在安装Linux之后自己修改Swap分区大小
- 【Tensorflow】tf.concat函数
- java泛型详解
- 代码收集_jQuery省份城市菜单二级下拉列表联动功能