Reverse digits of an integer.
class Solution {public: int reverse(int x) { int result = 0; while(x != 0){ result = result * 10 + x % 10; x = x / 10; } return result; }}; |
while(x != 0){ int element = x % 10; x = x / 10; } |
No comments:
Post a Comment