Leetcode

Remove 9

  • Time:O(1)
  • Space:O(1)

C++

class Solution {
 public:
  int newInteger(int n) {
    string ans;
    while (n) {
      ans = to_string(n % 9) + ans;
      n /= 9;
    }
    return stoi(ans);
  }
};

JAVA

class Solution {
  public int newInteger(int n) {
    return Integer.parseInt(Integer.toString(n, 9));
  }
}

Python

class Solution:
  def newInteger(self, n: int) -> int:
    ans = []
    while n:
      ans.append(str(n % 9))
      n //= 9
    return ''.join(reversed(ans))