Java StringBuffer.reverse()


  • 1
    C
    public class Solution {
        public int reverse(int x) {
    int result = 0;
    		if (x > Integer.MAX_VALUE)
    			return 0;
    		if (x >= 0) {
    			StringBuffer sBuffer = new StringBuffer(Integer.toString(x));
    			String reString = sBuffer.reverse().toString().trim();
    			try {
    				result = Integer.parseInt(reString);
    			} catch (Exception ex) {
    				System.out.println("error");
    				return 0;
    			}
    
    		}
    		if (x < 0) {
    			StringBuffer sBuffer = new StringBuffer(Integer.toString(x)
    					.replace("-", ""));
    			String reString = "-" + sBuffer.reverse().toString();
    			try {
    				result = Integer.parseInt(reString);
    			} catch (Exception ex) {
    				System.out.println("error");
    				return 0;
    			}
    
    		}
    		System.out.println(result);
    
    		return result;
    }
    }
    

Log in to reply
 

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