Skip to content

1317. Convert Integer to the Sum of Two No-Zero Integers

  • Time: $O(n)$
  • Space: $O(h)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
 public:
  vector<int> getNoZeroIntegers(int n) {
    for (int A = 1; A < n; ++A) {
      int B = n - A;
      if (to_string(A).find('0') == string::npos &&
          to_string(B).find('0') == string::npos)
        return {A, B};
    }

    throw;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
  public int[] getNoZeroIntegers(int n) {
    for (int A = 1; A < n; ++A) {
      int B = n - A;
      if (!String.valueOf(A).contains("0") && !String.valueOf(B).contains("0"))
        return new int[] {A, B};
    }

    throw new IllegalArgumentException();
  }
}
1
2
3
4
5
6
class Solution:
  def getNoZeroIntegers(self, n: int) -> list[int]:
    for A in range(n):
      B = n - A
      if '0' not in str(A) and '0' not in str(B):
        return A, B