[LeetCode] 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]
Note:
- The boundaries of each input argument are
1 <= left <= right <= 10000
.
这道题让我们找一个给定范围内的所有的自整除数字,所谓的自整除数字就是该数字可以整除其每一个位上的数字。既然这道题是Easy类,那么一般来说不需要用tricky的方法,直接暴力搜索就行了,遍历区间内的所有数字,然后调用子函数判断其是否是自整除数,是的话就加入结果res中。在子函数中,我们先把数字转为字符串,然后遍历每个字符,只要其为0,或者num无法整除该位上的数字,就返回false,循环结束后返回true,参见代码如下:
解法一:
- class Solution {
- public:
- vector<int> selfDividingNumbers(int left, int right) {
- vector<int> res;
- for (int i = left; i <= right; ++i) {
- if (check(i)) res.push_back(i);
- }
- return res;
- }
- bool check(int num) {
- string str = to_string(num);
- for (char c : str) {
- if (c == '' || num % (c - '')) return false;
- }
- return true;
- }
- };
我们可以不用子函数,直接在大的for循环中加上一个for循环进行判断即可,参见代码如下:
解法二:
- class Solution {
- public:
- vector<int> selfDividingNumbers(int left, int right) {
- vector<int> res;
- for (int i = left, n = ; i <= right; ++i) {
- for (n = i; n > ; n /= ) {
- if (n % == || i % (n % ) != ) break;
- }
- if (n == ) res.push_back(i);
- }
- return res;
- }
- };
类似题目:
参考资料:
https://discuss.leetcode.com/topic/111201/java-c-clean-code
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Self Dividing Numbers 自整除数字的更多相关文章
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [LeetCode] 829. Consecutive Numbers Sum 连续数字之和
Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- [CareerCup] 2.5 Add Two Numbers 两个数字相加
2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...
- 【Leetcode_easy】728. Self Dividing Numbers
problem 728. Self Dividing Numbers solution1: 使用string类型来表示每位上的数字: class Solution { public: vector&l ...
- LeetCode——Find All Numbers Disappeared in an Array
LeetCode--Find All Numbers Disappeared in an Array Question Given an array of integers where 1 ≤ a[i ...
- LeetCode:移除K位数字【402】
LeetCode:移除K位数字[402] 题目描述 给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小. 注意: num 的长度小于 10002 且 ≥ k. nu ...
- Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)
Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower) 我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你 ...
随机推荐
- postman简单教程-环境变量,全局变量的设置及作用
讲postman环境变量设置之前,先讲一个小插曲,环境变量.全局变量的区别在于Globals,只能用一组,而Environmen可以设置多组,所以我更喜欢设置环境变量 1.环境变量-Environme ...
- sql操作知识点个人笔记(SQLServer篇)
实际工作中,总会遇到一些常用的或不常用的sql,这些sql可能并没多少技术含量,但对我们本身而言,一个最大的问题就是很容易忘记.对我个人而言,以前常用的,过阵子之后再用到,发现不记得了.由此得出结论, ...
- [学习笔记] 模拟退火 (Simulated Annealing)
真没想到这东西真的在考场上用到了...顺便水篇blog以示诈尸好了(逃 模拟退火算法 模拟退火是一种随机化算法, 用于求函数的极值qwq 比如给出一个问题, 我们要求最优解的值, 但是可能的方案数量极 ...
- MySQL之数据的简单查询
我直接把我的mysql学习笔记以图片的形式粘贴在这里了,供自己回顾(都是一些简单的语句)
- C++之异常捕获和处理
一.简介 在C++语言中,异常处理包括:throw表达式,try语句块,一套异常类.其中,异常类用于在throw表达式和相关的catch子句之间传递异常的具体信息.exception头文件定义了最 ...
- defaultdict使用及__missing__理解
import collections import re WORD_RE = re.compile(r'\w+') index = collections.defaultdict(list) #用li ...
- JAVA线程概念
一.操作系统中线程和进程的概念 现在的操作系统是多任务操作系统.多线程是实现多任务的一种方式. 进程是指一个内存中运行的应用程序,每个进程都有自己独立的一块内存空间,一个进程中可以启动多个线程.比如在 ...
- 基于协程的Python网络库gevent
import gevent def test1(): print 12 gevent.sleep(0) print 34 def test2(): print 56 gevent.sleep(0) p ...
- border 三角形 有边框的 东西南北的 气泡三角形
链接地址:http://www.cnblogs.com/blosaa/p/3823695.html
- 【Fungus入门】10分钟快速构建Unity中的万能对话系统 / 叙事系统 / 剧情系统
我真的很久没有写过一个完整的攻略了(笑),咸鱼了很久之后还是想来写一个好玩的.这次主要是梳理一下Unity的小众插件Fungus的核心功能,并且快速掌握其使用方法. 官方文档:http://fungu ...