1. 程式人生 > >整數實現翻轉---java實現

整數實現翻轉---java實現

當時面試的時候,給出了一道題目,就是讓整數進行翻轉,比如給出整數123,然後翻轉成321,下面是程式碼實現

<span style="font-size:18px;">public class Solution {  
public intreverse(int x) {  
  long tmp = x; // 防止結果溢位  
  long result = 0;  
  while (tmp != 0) {  
     result = result *10 + tmp % 10;  
     tmp = tmp /10; }  
    // 溢位判斷  
    if (result < Integer.MIN_VALUE || result > Integer.MAX_VALUE)
    {  
     result =0; }  
     return (int) result;   
   }
}
</span>