zoukankan      html  css  js  c++  java
  • 728. Self Dividing Numbers

    题目描述:

    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.

    解题思路:

    一直思考有没有什么技巧,思考半天,没有头绪,最后使用暴力枚举。

    代码:

     1 class Solution {
     2 public:
     3     vector<int> selfDividingNumbers(int left, int right) {
     4         vector<int> ret;
     5         for (int i = left; i <= right; ++i) {
     6             if (check(i))
     7                 ret.push_back(i);
     8         }
     9         return ret;
    10     }
    11     bool check(int num) {
    12         int tmp = num;
    13         while (tmp != 0) {
    14             int div = tmp % 10;
    15             if (!div)
    16                 return false;
    17             if (num % div != 0)
    18                 return false;
    19             tmp /= 10;
    20         }
    21         return true;
    22     }
    23 };
  • 相关阅读:
    103. 二叉树的锯齿形层次遍历
    102. 二叉树的层次遍历
    94. 二叉树的中序遍历
    Redis和数据库 数据同步问题
    203. 移除链表元素
    19. 删除链表的倒数第N个节点
    237. 删除链表中的节点
    141. 环形链表
    2. 两数相加
    143. 重排链表
  • 原文地址:https://www.cnblogs.com/gsz-/p/9397663.html
Copyright © 2011-2022 走看看