LeetCode Entry
1317. Convert Integer to the Sum of Two No-Zero Integers
Find a+b=n without zeros in digits
1317. Convert Integer to the Sum of Two No-Zero Integers easy blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1106
Problem TLDR
Find a+b=n without zeros in digits #easy
Intuition
Brute force a=1..<n, b = n-a
Approach
- any faster solution?
- any shorter solution?
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
// 55ms
fun getNoZeroIntegers(n: Int) =
(0..n).map { listOf(it, n-it) }.find { '0' !in "$it" }
// 0ms
pub fn get_no_zero_integers(n: i32) -> Vec<i32> {
(0..n).map(|a|vec![a,n-a]).find(|v|!format!("{:?}",v).contains('0')).unwrap()
}
// 0ms
vector<int> getNoZeroIntegers(int n) {
int a=0,b=0,s=1;
while (n) {
int d = n%10; n/=10;
if (n&&d<2) a += s*(8+d), b += s<<1, --n;
else a += s, b += s*(d-1);
s *= 10;
}
return {a,b};
}