diff --git a/solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/README.md b/solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/README.md index c42f804c43b85..d45cbd055af25 100644 --- a/solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/README.md +++ b/solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/README.md @@ -62,7 +62,7 @@ tags: 我们发现,如果一个数 $n$ 可以表示成若干个“不同的”三的幂之和,那么 $n$ 的三进制表示中,每一位上的数字只能是 $0$ 或者 $1$。 -因此,我们将 $n$ 转换成三进制,然后判断每一位上的数字是否是 $0$ 或者 $1$。如果不是,那么 $n$ 就不可以表示成若干个三的幂之和,直接返回 `false`;否则 $n$ 可以表示成若干个三的幂之和,返回 `true`。 +因此,我们将 $n$ 转换成三进制,然后判断每一位上的数字是否是 $0$ 或者 $1$。如果不是,那么 $n$ 就不可以表示成若干个三的幂之和,直接返回 $\textit{false}$;否则 $n$ 可以表示成若干个三的幂之和,返回 $\textit{true}$。 时间复杂度 $O(\log_3 n)$,空间复杂度 $O(1)$。 @@ -137,6 +137,39 @@ function checkPowersOfThree(n: number): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn check_powers_of_three(n: i32) -> bool { + let mut n = n; + while n > 0 { + if n % 3 > 1 { + return false; + } + n /= 3; + } + true + } +} +``` + +#### C# + +```cs +public class Solution { + public bool CheckPowersOfThree(int n) { + while (n > 0) { + if (n % 3 > 1) { + return false; + } + n /= 3; + } + return true; + } +} +``` + diff --git a/solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/README_EN.md b/solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/README_EN.md index ae217d7ac7430..30a5170542969 100644 --- a/solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/README_EN.md +++ b/solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/README_EN.md @@ -63,7 +63,7 @@ tags: We find that if a number $n$ can be expressed as the sum of several "different" powers of three, then in the ternary representation of $n$, each digit can only be $0$ or $1$. -Therefore, we convert $n$ to ternary and then check whether each digit is $0$ or $1$. If not, then $n$ cannot be expressed as the sum of several powers of three, and we directly return `false`; otherwise, $n$ can be expressed as the sum of several powers of three, and we return `true`. +Therefore, we convert $n$ to ternary and then check whether each digit is $0$ or $1$. If not, then $n$ cannot be expressed as the sum of several powers of three, and we directly return $\textit{false}$; otherwise, $n$ can be expressed as the sum of several powers of three, and we return $\textit{true}$. The time complexity is $O(\log_3 n)$, and the space complexity is $O(1)$. @@ -138,6 +138,39 @@ function checkPowersOfThree(n: number): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn check_powers_of_three(n: i32) -> bool { + let mut n = n; + while n > 0 { + if n % 3 > 1 { + return false; + } + n /= 3; + } + true + } +} +``` + +#### C# + +```cs +public class Solution { + public bool CheckPowersOfThree(int n) { + while (n > 0) { + if (n % 3 > 1) { + return false; + } + n /= 3; + } + return true; + } +} +``` + diff --git a/solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/Solution.cs b/solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/Solution.cs new file mode 100644 index 0000000000000..48041049880fc --- /dev/null +++ b/solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/Solution.cs @@ -0,0 +1,11 @@ +public class Solution { + public bool CheckPowersOfThree(int n) { + while (n > 0) { + if (n % 3 > 1) { + return false; + } + n /= 3; + } + return true; + } +} diff --git a/solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/Solution.rs b/solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/Solution.rs new file mode 100644 index 0000000000000..c84373b018628 --- /dev/null +++ b/solution/1700-1799/1780.Check if Number is a Sum of Powers of Three/Solution.rs @@ -0,0 +1,12 @@ +impl Solution { + pub fn check_powers_of_three(n: i32) -> bool { + let mut n = n; + while n > 0 { + if n % 3 > 1 { + return false; + } + n /= 3; + } + true + } +}