Skip to content

2443. Sum of Number and Its Reverse 👎

  • Time: $O(\texttt{num})$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
 public:
  bool sumOfNumberAndReverse(int num) {
    for (int i = num / 2; i <= num; ++i)
      if (num == i + reversed(i))
        return true;
    return false;
  }

 private:
  int reversed(int num) {
    int ans = 0;
    while (num > 0) {
      ans = ans * 10 + num % 10;
      num /= 10;
    }
    return ans;
  }
};