diff --git a/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/README.md b/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/README.md index 4f35eda0d8e98..98dbde22d1c43 100644 --- a/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/README.md +++ b/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/README.md @@ -67,32 +67,95 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3622.Ch -### 方法一 +### 方法一:模拟 + +我们可以遍历整数 $n$ 的每一位数字,计算出数字和 $s$ 和数字积 $p$。最后判断 $n$ 是否能被 $s + p$ 整除。 + +时间复杂度 $O(\log n)$,其中 $n$ 为整数 $n$ 的值。空间复杂度 $O(1)$。 #### Python3 ```python - +class Solution: + def checkDivisibility(self, n: int) -> bool: + s, p = 0, 1 + x = n + while x: + x, v = divmod(x, 10) + s += v + p *= v + return n % (s + p) == 0 ``` #### Java ```java - +class Solution { + public boolean checkDivisibility(int n) { + int s = 0, p = 1; + int x = n; + while (x != 0) { + int v = x % 10; + x /= 10; + s += v; + p *= v; + } + return n % (s + p) == 0; + } +} ``` #### C++ ```cpp - +class Solution { +public: + bool checkDivisibility(int n) { + int s = 0, p = 1; + int x = n; + while (x != 0) { + int v = x % 10; + x /= 10; + s += v; + p *= v; + } + return n % (s + p) == 0; + } +}; ``` #### Go ```go +func checkDivisibility(n int) bool { + s, p := 0, 1 + x := n + for x != 0 { + v := x % 10 + x /= 10 + s += v + p *= v + } + return n%(s+p) == 0 +} +``` +#### TypeScript + +```ts +function checkDivisibility(n: number): boolean { + let [s, p] = [0, 1]; + let x = n; + while (x !== 0) { + const v = x % 10; + x = Math.floor(x / 10); + s += v; + p *= v; + } + return n % (s + p) === 0; +} ``` diff --git a/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/README_EN.md b/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/README_EN.md index cca63a88cf9a5..928493fa38be8 100644 --- a/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/README_EN.md +++ b/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/README_EN.md @@ -65,32 +65,95 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3622.Ch -### Solution 1 +### Solution 1: Simulation + +We can iterate through each digit of the integer $n$, calculating the digit sum $s$ and digit product $p$. Finally, we check whether $n$ is divisible by $s + p$. + +The time complexity is $O(\log n)$, where $n$ is the value of the integer $n$. The space complexity is $O(1)$. #### Python3 ```python - +class Solution: + def checkDivisibility(self, n: int) -> bool: + s, p = 0, 1 + x = n + while x: + x, v = divmod(x, 10) + s += v + p *= v + return n % (s + p) == 0 ``` #### Java ```java - +class Solution { + public boolean checkDivisibility(int n) { + int s = 0, p = 1; + int x = n; + while (x != 0) { + int v = x % 10; + x /= 10; + s += v; + p *= v; + } + return n % (s + p) == 0; + } +} ``` #### C++ ```cpp - +class Solution { +public: + bool checkDivisibility(int n) { + int s = 0, p = 1; + int x = n; + while (x != 0) { + int v = x % 10; + x /= 10; + s += v; + p *= v; + } + return n % (s + p) == 0; + } +}; ``` #### Go ```go +func checkDivisibility(n int) bool { + s, p := 0, 1 + x := n + for x != 0 { + v := x % 10 + x /= 10 + s += v + p *= v + } + return n%(s+p) == 0 +} +``` +#### TypeScript + +```ts +function checkDivisibility(n: number): boolean { + let [s, p] = [0, 1]; + let x = n; + while (x !== 0) { + const v = x % 10; + x = Math.floor(x / 10); + s += v; + p *= v; + } + return n % (s + p) === 0; +} ``` diff --git a/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/Solution.cpp b/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/Solution.cpp new file mode 100644 index 0000000000000..c4f769ebb092c --- /dev/null +++ b/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/Solution.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + bool checkDivisibility(int n) { + int s = 0, p = 1; + int x = n; + while (x != 0) { + int v = x % 10; + x /= 10; + s += v; + p *= v; + } + return n % (s + p) == 0; + } +}; \ No newline at end of file diff --git a/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/Solution.go b/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/Solution.go new file mode 100644 index 0000000000000..0ced64416d7b2 --- /dev/null +++ b/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/Solution.go @@ -0,0 +1,11 @@ +func checkDivisibility(n int) bool { + s, p := 0, 1 + x := n + for x != 0 { + v := x % 10 + x /= 10 + s += v + p *= v + } + return n%(s+p) == 0 +} \ No newline at end of file diff --git a/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/Solution.java b/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/Solution.java new file mode 100644 index 0000000000000..9f042b0143d03 --- /dev/null +++ b/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/Solution.java @@ -0,0 +1,13 @@ +class Solution { + public boolean checkDivisibility(int n) { + int s = 0, p = 1; + int x = n; + while (x != 0) { + int v = x % 10; + x /= 10; + s += v; + p *= v; + } + return n % (s + p) == 0; + } +} \ No newline at end of file diff --git a/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/Solution.py b/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/Solution.py new file mode 100644 index 0000000000000..8a5cc00ae0c19 --- /dev/null +++ b/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/Solution.py @@ -0,0 +1,9 @@ +class Solution: + def checkDivisibility(self, n: int) -> bool: + s, p = 0, 1 + x = n + while x: + x, v = divmod(x, 10) + s += v + p *= v + return n % (s + p) == 0 diff --git a/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/Solution.ts b/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/Solution.ts new file mode 100644 index 0000000000000..2dae083e791b2 --- /dev/null +++ b/solution/3600-3699/3622.Check Divisibility by Digit Sum and Product/Solution.ts @@ -0,0 +1,11 @@ +function checkDivisibility(n: number): boolean { + let [s, p] = [0, 1]; + let x = n; + while (x !== 0) { + const v = x % 10; + x = Math.floor(x / 10); + s += v; + p *= v; + } + return n % (s + p) === 0; +}