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 == 0128 % 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;
}
};

类似题目:

Perfect Number

参考资料:

https://discuss.leetcode.com/topic/111201/java-c-clean-code

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Self Dividing Numbers 自整除数字的更多相关文章

  1. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  2. [LeetCode] 829. Consecutive Numbers Sum 连续数字之和

    Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...

  3. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

  4. [LeetCode] 2. Add Two Numbers 两个数字相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  5. [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 ...

  6. 【Leetcode_easy】728. Self Dividing Numbers

    problem 728. Self Dividing Numbers solution1: 使用string类型来表示每位上的数字: class Solution { public: vector&l ...

  7. 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 ...

  8. LeetCode:移除K位数字【402】

    LeetCode:移除K位数字[402] 题目描述 给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小. 注意: num 的长度小于 10002 且 ≥ k. nu ...

  9. Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)

    Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower) 我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你 ...

随机推荐

  1. C语言的文件读写操作函数小结

    一.文件打开 使用 fopen( ) 函数来创建一个新的文件或者打开一个已有的文件,这个调用会初始化类型 FILE 的一个对象,类型 FILE包含了所有用来控制流的必要的信息.函数原型为: FILE ...

  2. (转)SQLite内置函数

    一.聚合函数: SQLite中支持的聚合函数在很多其他的关系型数据库中也同样支持,因此我们这里将只是给出每个聚集函数的简要说明,而不在给出更多的示例了.这里还需要进一步说明的是,对于所有聚合函数而言, ...

  3. 基于FPGA的Cordic算法实现

    CORDIC(Coordinate Rotation Digital Computer)算法即坐标旋转数字计算方法,是J.D.Volder1于1959年首次提出,主要用于三角函数.双曲线.指数.对数的 ...

  4. Vue中的v-cloak用法

    v-cloak 的作用和用法 用法: 这个指令保持在元素上直到关联实例结束编译.和 CSS 规则如 [v-cloak] { display: none } 一起用时,这个指令可以隐藏未编译的 Must ...

  5. JavaScript(第二十天)【DOM操作表格及样式】

    DOM在操作生成HTML上,还是比较简明的.不过,由于浏览器总是存在兼容和陷阱,导致最终的操作就不是那么简单方便了.本章主要了解一下DOM操作表格和样式的一些知识. 一.操作表格 <table& ...

  6. C语言--第二周作业

    ****学习内容总结**** 1.Git和编辑器截图 2.MOOC截图 3.阅读<提问的智慧>感想 读完<提问的智慧>之后,我认为在提问时,要根据以下步骤: 谨慎明确的描述症状 ...

  7. Django 分类标签查找

    from django.conf.urls import url from django.contrib import admin from blog.views import index,stude ...

  8. java8-Stream之数值流

    在Stream里元素都是对象,那么,当我们操作一个数字流的时候就不得不考虑一个问题,拆箱和装箱.虽然自动拆箱不需要我们处理,但依旧有隐含的成本在里面.Java8引入了3个原始类型特化流接口来解决这个问 ...

  9. display属性

    display 属性规定元素应该生成的框的类型. 值 描述 none 此元素不会被显示. block 此元素将显示为块级元素,此元素前后会带有换行符. inline 默认.此元素会被显示为内联元素,元 ...

  10. ORA-00379 缓冲池 DEFAULT 中无法提供 32K 块大小的空闲缓冲区

    (一)问题 今天在使用Pl/sql developer查看表空间大小的时候,报错误:ORA-00379 缓冲池 DEFAULT 中无法提供 32K 块大小的空闲缓冲区,具体如下图: SQL> s ...