LeetCode第7題_Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2 31 , 2 31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. 思路:先嘗試取到各個字的digit,在反序去進行打印 改寫為題目對應的函式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public static int reverse ( int x){ ArrayList revRes = new ArrayList(); int src = x; if (x< 0 ){ x = x*(- 1 ); } while (x> 0 ){ int digit = x% 10 ; revRes. add (digit); x/= 10 ; } int res = Integer. parseInt (revRes. toString (). replace ( "[" , "" ). replace ( "]" , "" ). replace ( ", " , &qu