问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3889 访问。

自除数 是指可以被它包含的每一位数除尽的数。

例如,128 是一个自除数,因为 128 % 1 == 0,128 % 2 == 0,128 % 8 == 0。

还有,自除数不允许包含 0 。

给定上边界和下边界数字,输出一个列表,列表的元素是边界(含边界)内所有的自除数。

输入: 上边界left = 1, 下边界right = 22

输出: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]

注意:每个输入参数的边界满足 1 <= left <= right <= 10000。


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.

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.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3889 访问。

public class Program {

    public static void Main(string[] args) {
var left = 1;
var right = 21; var res = SelfDividingNumbers(left, right);
ShowArray(res); left = 3;
right = 36; res = SelfDividingNumbers2(left, right);
ShowArray(res); Console.ReadKey();
} private static void ShowArray(IList<int> array) {
foreach(var num in array) {
Console.Write($"{num} ");
}
Console.WriteLine();
} private static IList<int> SelfDividingNumbers(int left, int right) {
var res = new List<int>();
for(var i = left; i <= right; i++) {
if(IsSelfDividingNumber(i)) res.Add(i);
}
return res;
} private static bool IsSelfDividingNumber(int num) {
//用字符串索引取位
var length = num.ToString().Length;
for(var i = 0; i < length; i++) {
var bit = int.Parse(num.ToString()[length - i - 1].ToString());
if(bit == 0 || num % bit != 0) return false;
}
return true;
} private static IList<int> SelfDividingNumbers2(int left, int right) {
var res = new List<int>();
for(var i = left; i <= right; i++) {
if(IsSelfDividingNumber2(i)) res.Add(i);
}
return res;
} private static bool IsSelfDividingNumber2(int num) {
//用传统取余式取位
var number = num;
while(number > 0) {
var bit = number % 10;
if(bit == 0 || num % bit != 0) return false;
number /= 10;
}
return true;
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3889 访问。

1 2 3 4 5 6 7 8 9 11 12 15
3 4 5 6 7 8 9 11 12 15 22 24 33 36

分析:

显而易见,以上2种算法的时间复杂度均为: 

C#LeetCode刷题之#728-自除数(Self Dividing Numbers)的更多相关文章

  1. 【leetcode刷题笔记】Sum Root to Leaf Numbers

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  2. C#LeetCode刷题-数学

    数学篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...

  3. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  4. leetcode 刷题进展

    最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多  前200的吃透了 足以应付非算法岗 ...

  5. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  6. leetcode刷题记录--js

    leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...

  7. LeetCode刷题总结之双指针法

    Leetcode刷题总结 目前已经刷了50道题,从零开始刷题学到了很多精妙的解法和深刻的思想,因此想按方法对写过的题做一个总结 双指针法 双指针法有时也叫快慢指针,在数组里是用两个整型值代表下标,在链 ...

  8. Leetcode刷题记录(python3)

    Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...

  9. LeetCode刷题总结-数组篇(上)

    数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...

  10. LeetCode刷题总结-数组篇(中)

    本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...

随机推荐

  1. 手写简易SpringMVC

    手写简易SpringMVC 手写系列框架代码基于普通Maven构建,因此在手写SpringMVC的过程中,需要手动的集成Tomcat容器 必备知识: Servlet相关理解和使用,Maven,Java ...

  2. node name配置错误,导致grid日志在报警

    [root@aipdb ContentsXML]# cat inventory.xml <?xml version="1.0" standalone="yes&qu ...

  3. NVIDIA GPU Volta架构简述

    NVIDIA GPU Volta架构简述 本文摘抄自英伟达Volta架构官方白皮书:https://www.nvidia.com/content/dam/en-zz/Solutions/Data-Ce ...

  4. JAVA面向对象:三大特征 封装讲解

    一.JAVA封装 1.封装的理解 封装是 JAVA 面向对象思想的 一 种特性,也是一种信息隐蔽的技术 2.封装的原则 将类中的某些信息隐藏起来,来防止外部程序直接访问,通过类中的方法实现对隐藏的信息 ...

  5. HDU - 1520 Anniversary party (树的最大独立集)

    Time limit :1000 ms :Memory limit :32768 kB: OS :Windows There is going to be a party to celebrate t ...

  6. AI面试之SVM推导

    SVM现在主流的有两个方法.一个是传统的推导,计算支持向量求解的方法,一个是近几年兴起的梯度下降的方法. 梯度下降方法的核心是使用了hinge loss作为损失函数,所以最近也有人提出的深度SVM其实 ...

  7. react native redux

    redux可以解决, 程序中所有组件的状态统一管理, 从而使我们可以更加动态的,灵活的控制程序 React:数据管理使用props.stateRedux的主要思想:提供一个数据存储中心,可以供外部访问 ...

  8. C# WebClient几种常用方法的用法

    1.UploadData方法(Content-Type:application/x-www-form-urlencoded) //创建WebClient 对象            WebClient ...

  9. NGINX 上的限流

    NGINX 上的限流(译) zlup YP小站  今天 前言 本文是对Rate Limiting with NGINX and NGINX Plus的主要内容(去掉了关于NGINX Plus相关内容) ...

  10. Android仿支付宝高顶部功能条伸缩动画

    参考:https://blog.csdn.net/aqi00/article/details/72621176