leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

reverse-integer.cpp (700B)


      1 #include <iostream>
      2 
      3 
      4 //Leet Ratings
      5 //       Speed  Memory
      6 //Total  0ms    6.4MB    
      7 //Beats  100%   5.61%
      8 //
      9 
     10 using namespace std;
     11 
     12 int reverse(int z)
     13 {
     14     bool negative = false;
     15     if(z < 0){
     16         negative = true;
     17     }
     18     string str = to_string(abs(z));
     19     string temp;
     20     for(int i = str.length() - 1; i >= 0; --i){
     21         temp += str[i];
     22     }
     23     str = temp;
     24     try{
     25         if(negative){
     26             return(stoi(str) * -1);
     27         }
     28         else{
     29             return(stoi(str));
     30         }
     31     }
     32     catch(exception& error){
     33         return 0;
     34     }
     35 }
     36 
     37 int main(){
     38     cout << "Enter a number: ";
     39     int input;
     40     cin >> input;
     41     cout << reverse(input) << endl;
     42     return 0;
     43 }
     44 
     45