diff --git a/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README.md b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README.md index 4c6df94597775..ef328dc20121b 100644 --- a/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README.md +++ b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README.md @@ -96,9 +96,9 @@ tags: ```python class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: - for a in range(1, n): + for a in count(1): b = n - a - if "0" not in str(a) + str(b): + if "0" not in f"{a}{b}": return [a, b] ``` @@ -159,6 +159,22 @@ function getNoZeroIntegers(n: number): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn get_no_zero_integers(n: i32) -> Vec { + for a in 1..n { + let b = n - a; + if !a.to_string().contains('0') && !b.to_string().contains('0') { + return vec![a, b]; + } + } + vec![] + } +} +``` + @@ -178,14 +194,14 @@ function getNoZeroIntegers(n: number): number[] { ```python class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: - def f(x): + def f(x: int) -> bool: while x: if x % 10 == 0: return False x //= 10 return True - for a in range(1, n): + for a in count(1): b = n - a if f(a) and f(b): return [a, b] @@ -281,6 +297,32 @@ function getNoZeroIntegers(n: number): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn get_no_zero_integers(n: i32) -> Vec { + fn f(mut x: i32) -> bool { + while x > 0 { + if x % 10 == 0 { + return false; + } + x /= 10; + } + true + } + + for a in 1..n { + let b = n - a; + if f(a) && f(b) { + return vec![a, b]; + } + } + vec![] + } +} +``` + diff --git a/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README_EN.md b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README_EN.md index 5a4cf6eb73ffa..e4acfbf10e204 100644 --- a/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README_EN.md +++ b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README_EN.md @@ -75,9 +75,9 @@ The time complexity is $O(n \times \log n)$, where $n$ is the integer given in t ```python class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: - for a in range(1, n): + for a in count(1): b = n - a - if "0" not in str(a) + str(b): + if "0" not in f"{a}{b}": return [a, b] ``` @@ -138,6 +138,22 @@ function getNoZeroIntegers(n: number): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn get_no_zero_integers(n: i32) -> Vec { + for a in 1..n { + let b = n - a; + if !a.to_string().contains('0') && !b.to_string().contains('0') { + return vec![a, b]; + } + } + vec![] + } +} +``` + @@ -157,14 +173,14 @@ The time complexity is $O(n \times \log n)$, where $n$ is the integer given in t ```python class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: - def f(x): + def f(x: int) -> bool: while x: if x % 10 == 0: return False x //= 10 return True - for a in range(1, n): + for a in count(1): b = n - a if f(a) and f(b): return [a, b] @@ -260,6 +276,32 @@ function getNoZeroIntegers(n: number): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn get_no_zero_integers(n: i32) -> Vec { + fn f(mut x: i32) -> bool { + while x > 0 { + if x % 10 == 0 { + return false; + } + x /= 10; + } + true + } + + for a in 1..n { + let b = n - a; + if f(a) && f(b) { + return vec![a, b]; + } + } + vec![] + } +} +``` + diff --git a/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution.py b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution.py index bacbb31c42bfb..21096508897d5 100644 --- a/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution.py +++ b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution.py @@ -1,6 +1,6 @@ class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: - for a in range(1, n): + for a in count(1): b = n - a - if "0" not in str(a) + str(b): + if "0" not in f"{a}{b}": return [a, b] diff --git a/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution.rs b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution.rs new file mode 100644 index 0000000000000..6ceb156a6be73 --- /dev/null +++ b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution.rs @@ -0,0 +1,11 @@ +impl Solution { + pub fn get_no_zero_integers(n: i32) -> Vec { + for a in 1..n { + let b = n - a; + if !a.to_string().contains('0') && !b.to_string().contains('0') { + return vec![a, b]; + } + } + vec![] + } +} diff --git a/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution2.py b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution2.py index 0cb73e647f401..719c50a803688 100644 --- a/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution2.py +++ b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution2.py @@ -1,13 +1,13 @@ class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: - def f(x): + def f(x: int) -> bool: while x: if x % 10 == 0: return False x //= 10 return True - for a in range(1, n): + for a in count(1): b = n - a if f(a) and f(b): return [a, b] diff --git a/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution2.rs b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution2.rs new file mode 100644 index 0000000000000..80870a5556c00 --- /dev/null +++ b/solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/Solution2.rs @@ -0,0 +1,21 @@ +impl Solution { + pub fn get_no_zero_integers(n: i32) -> Vec { + fn f(mut x: i32) -> bool { + while x > 0 { + if x % 10 == 0 { + return false; + } + x /= 10; + } + true + } + + for a in 1..n { + let b = n - a; + if f(a) && f(b) { + return vec![a, b]; + } + } + vec![] + } +}