Solution in Java


  • 0
    V

    class Solution {
    public int reverse(int x) {
    boolean flag = false;
    if(x < 0){
    x *= -1;
    flag = true;
    }
    String s = String.valueOf(x);
    String result = "";
    for (int i = (s.length() - 1); i >= 0; i--) {
    result += s.charAt(i);
    }
    int rev = Integer.parseInt(result);
    if(flag == true) { rev *= -1;}
    return rev;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.