【LeetCode OJ】Best Time to Buy and Sell Stock II

Reverse digits of an integer.java

Example1: x = 123, return 321
Example2: x = -123, return -321git

public class Solution {
    public int reverse(int x) {
        if(x > 0)
			return doReverse(x);
		else
			return -1*doReverse(-1*x);
    }
	public static int doReverse(int x){
		String s = x+"";
		char[] array = s.toCharArray();
		StringBuffer sb = new StringBuffer();
		for(int i = array.length - 1; i >= 0; --i)
			sb.append(array[i]);
		return Integer.parseInt(sb.toString());
	}
}
相關文章
相關標籤/搜索