| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.SelfDividingNumbers; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class SelfDividingNumbersMath : ISelfDividingNumbers |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n * d), where n is range size and d is digit count |
| | | 19 | | /// Space complexity - O(1) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="left"></param> |
| | | 22 | | /// <param name="right"></param> |
| | | 23 | | /// <returns></returns> |
| | | 24 | | public IList<int> SelfDividingNumbers(int left, int right) |
| | 2 | 25 | | { |
| | 2 | 26 | | var result = new List<int>(); |
| | | 27 | | |
| | 126 | 28 | | for (var number = left; number <= right; number++) |
| | 61 | 29 | | { |
| | 61 | 30 | | if (IsSelfDividing(number)) |
| | 17 | 31 | | { |
| | 17 | 32 | | result.Add(number); |
| | 17 | 33 | | } |
| | 61 | 34 | | } |
| | | 35 | | |
| | 2 | 36 | | return result; |
| | 2 | 37 | | } |
| | | 38 | | |
| | | 39 | | private static bool IsSelfDividing(int number) |
| | 61 | 40 | | { |
| | 61 | 41 | | var temp = number; |
| | | 42 | | |
| | 101 | 43 | | while (temp > 0) |
| | 84 | 44 | | { |
| | 84 | 45 | | var digit = temp % 10; |
| | | 46 | | |
| | 84 | 47 | | if (digit == 0) |
| | 6 | 48 | | { |
| | 6 | 49 | | return false; |
| | | 50 | | } |
| | | 51 | | |
| | 78 | 52 | | if (number % digit != 0) |
| | 38 | 53 | | { |
| | 38 | 54 | | return false; |
| | | 55 | | } |
| | | 56 | | |
| | 40 | 57 | | temp /= 10; |
| | 40 | 58 | | } |
| | | 59 | | |
| | 17 | 60 | | return true; |
| | 61 | 61 | | } |
| | | 62 | | } |