Leetcode

Convert Integer to the Sum of Two No-Zero Integers

  • Time:O(n)
  • Space:O(h)

C++

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;
  }
};

JAVA

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();
  }
}

Python

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