I try to convert integer to String,but don't know how to deal with the minus sign

  • 0
        public int reverse(int x) {
            String str =String.valueOf(x);
            char ch[]=str.toCharArray();
            char ch1[]=new char [ch.length];
            int m=0;
            for(int i=ch.length-1;i>=0;i--)
            String str1=String.copyValueOf(ch1);
            int y = Integer.parseInt(str1);
            return y;

  • 0

    You can check if the input is negative at the very beginning.

    If it's negative, multiply by -1 so that it becomes positive. And multiply by -1 again at last.

    If it's positive, you know what to do already.

Log in to reply

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