LeetCode-7. 反轉整數
阿新 • • 發佈:2019-01-08
給定一個 32 位有符號整數,將整數中的數字進行反轉。
我的程式碼:
public class Solution2 { public int reverse(int x) { Integer in = new Integer(x); String s = in.toString(); String result = null; Integer result1 = null; if (s.charAt(0) != '-') { StringBuilder sb = new StringBuilder(s); result = sb.reverse().toString(); System.out.println(result); try { result1 = new Integer(result); } catch (NumberFormatException e) { e.printStackTrace(); return 0; } } else { String s2 = s.substring(1,s.length()); StringBuilder sb = new StringBuilder(s2); result = "-".concat(sb.reverse().toString()); System.out.println(result); try { result1 = new Integer(result); } catch (NumberFormatException e) { e.printStackTrace(); return 0; } } return result1; } public static void main(String[] args) { int x = 1534236469; new Solution2().reverse(x); } }