[면접 문제] LeetCode: Reverse Integer

3154 단어 LeetCode
Reverse digits of an integer.
Example1: x = 123, return 321Example2: x = -123, return -321
 1 class Solution {
 2 public:
 3     int reverse(int x) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         int sig = 1;
 7         if(x<0){
 8             sig = -1;
 9             x = -x;
10         }
11         int rtn = 0;
12         while(x>0){
13             rtn = rtn * 10 + x%10;
14             x /= 10;
15         }
16         if(sig == -1){
17             return -rtn;
18         }
19         return rtn;
20     }
21 };

Run Status: Accepted!Program Runtime: 56 milli secs
Progress: 1020/1020 test cases passed.

좋은 웹페이지 즐겨찾기