Java設計實踐課的LeetCode題目

來源:互聯網
上載者:User

標籤:ret   bool   min   類型   轉化   ber   ever   判斷   back   

7.Reverse Integer 

解題思路:使用強制類型轉換的代碼,先用long做,之後判斷值然後強制轉化成int

 1 public class Solution { 2     public int reverse(int x) { 3         long reverse_n = 0; 4         while (x != 0) { 5             reverse_n = reverse_n * 10 + x % 10;//關鍵代碼 6             x = x / 10; 7         } 8         if (reverse_n > Integer.MAX_VALUE || reverse_n < Integer.MIN_VALUE) { 9             return 0;10         }11         return (int)reverse_n;12     }13 }

 9. Palindrome Number 

 1 public class Solution {//解題思路:直接利用直接做的reverse題目,如果x == reverse_x則說明是迴文數。 2     public boolean isPalindrome(int x) { 3         if (x < 0) { 4             return false; 5         } 6         int y = x; 7         long reverse_x = 0; 8         while (x != 0) { 9             reverse_x = reverse_x * 10 + x % 10;10             x = x / 10;11         }12         // if (x > Integer.MAX_VALUE || x < Integer.MIN_VALUE) {13         //     return false;14         // }15         if (y == (int)reverse_x) {16             return true;17         }18         return false;19     }20 }

 

          

         

Java設計實踐課的LeetCode題目

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.