1. 程式人生 > >Reverse Integer(反轉整型數)

Reverse Integer(反轉整型數)

Reverse digits of an integer.(反轉一個整型數)

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

1.個人分析
思路一:整型數轉字串->反轉字串->字串轉整型
思路二:數學解法,不斷地進行整除和取餘運算。

2.個人解法
(1)

int reverse(int x) 
{
    int sign = 1;
    if(x < 0){
        sign = -1;
        x = -x;
    }

    //整型轉字串並反轉字串
    string str
= to_string((long long)x); string rstr; string::reverse_iterator rit=str.rbegin(); for(; rit != str.rend(); ++rit) rstr.push_back(*rit); //將字串轉換為整型 long long res; istringstream iss(rstr); iss >>res; //溢位處理 if(res > INT_MAX || res < INT_MIN) return
0; return res * sign; }

(2)

int reverse(int x)
{
    int sign = 1;
    if(x < 0){
        sign = -1;
        x = -x;
    }

    long long res = 0;
    string str = to_string((long long)x);       
    for(string::reverse_iterator rit=str.rbegin(); rit != str.rend(); ++rit){
        res = res * 10
+ (*rit - '0'); } if(res > INT_MAX || res < INT_MIN) return 0; return res * sign; }

3.參考解法
(1)

int reverse(int x)
{
    long long res = 0;
    while (x){
        res = res * 10 + x % 10;
        x /= 10;
    }

    return (res > INT_MAX || res < INT_MIN) ? 0 : (int)res;
}

(2)

class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """   
        s = cmp(x, 0)   #x > 0,s=1; x < 0,s=-1
        r = int(`s*x`[::-1])    #先將字串反轉,然後將字串轉換為整型
        return s*r * (r < 2**31)    #r < 2**31為真時返回1,否則返回0;2**31表示2的31次方

該解法使用Python實現,只用了三行程式碼,思路是和思路一類似的。

4.總結
該題比較簡單,但需注意處理溢位的情況。另外前兩種解法是基於字串操作實現的,沒有數學解法簡潔,但提供了一個比較常用的字串與整型之間相互轉換的方式。

PS:

  • 題目的中文翻譯是本人所作,如有偏差敬請指正。
  • 其中的“個人分析”和“個人解法”均是本人最初的想法和做法,不一定是對的,只是作為一個對照和記錄。

相關推薦

Reverse Integer反轉

Reverse digits of an integer.(反轉一個整型數) Example1: x = 123, return 321 Example2: x = -123, return -32

7. Reverse Integer反轉 —— Java

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Note: The input is assumed to be a 32-bit

LeetCode之Reverse Integer反轉整數 簡單 模擬

問題描述: 給定32位有符號整數,整數的反向數字。 例1: 輸入: 123 輸出: 321 例2: 輸入: -123 輸出: -321 例3: 輸入: 120 輸出: 21 注意: 假設我們正在處理一個只能在32位有符號整數範圍內儲存整數的環境:[ - 2

LeetCode之Reverse Integer反轉整數 簡單 模擬

問題描述: 給定32位有符號整數,整數的反向數字。 例1: 輸入: 123 輸出: 321 例2: 輸入: -123 輸出: -321 例3: 輸入: 120 輸出: 21 注意: 假設我們正在處理一個只能在32位有符號整數範圍內儲存整數的環境:[

LeetCode 7. Reverse Integer反轉整數

Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the inte

7. Reverse Integer反轉整數

Description: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:Inpu

Integer的自動拆箱和自動裝箱的陷阱-128到127的值比較問題

Integer的自動拆裝箱的陷阱(整型數-128到127的值比較問題): 1、先看下面的例子: package integerdemo;   publicclass IntegerDemo {       publicstaticvoid main(String[] args) {         

【LeetCode】- Reverse Integer將一個整數反轉

[ 問題: ] Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321

LeetCode7——Reverse Integer將一個整數反轉,注意溢位的處理

題目: 解法一: 注意long long型別,表示64bit數字。 解法二: class Solution { public: int reverse(int x) { int ans = 0; while (x) {

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

陣列處理演算法十四不用庫函式,用C語言實現將一轉換成字串

不用庫函式,用C語言實現將一整型數轉換成字串,如:int a=123456,轉換成"123456"。 如題,要求將一整型數轉換為字串。這裡要考慮的是整型數可能是負數、正數和0。 實現如下: char

事務的隔離級別-關系據庫

數據 並發訪問 範圍 serial 事務處理 serializa 之前 zab nco 一、事務具有以下ACID特性: 原子性(Atomictiy):原子性是指事務包含的所有操作要麽全部成功,要麽全部失敗回滾,因此事務的操作如果成功就必須要全部應用到數據庫,如果操作失敗則不

★ Python字串型別轉換為列表型別資料

我們會發現在使用Matplotlib庫畫圖的時候因為許多引數都是以元組/列表的形式儲存與呼叫, 那我們可不可以通過input()輸入我們需要的引數呢? 測試過後, 發現input()輸入的是字串型別的資料,在使用list()直接對其列表化之後, 每一個單獨的字元都會被轉化列表中的

7. Reverse Integerpython+cppint範圍的問題

題目: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Outp

leetcode-7 reverse-integer(整數反轉

我們先看一下題目描述:  就是將數字進行反轉,可以用數學方法將其解答。 public static int reverse(int x) { long a = 0; while (x != 0) { a = a * 10 + x

LeetCode 之Reverse IntegerC

Given a 32-bit signed integer, reverse digits of an integer. 給定32位有符號整數,整數的反向數字。 Example 1: Input: 123 Output: 321 Example 2: In

【leetcode】7. Reverse IntegerC

Description: Given a 32-bit signed integer, reverse digits of an integer. Example1: Input: 123 Output: 321 Example2: Input: -1

LeetCode7. Reverse Integer翻轉整數

Reverse digits of an integer.  Example1: x = 123, return 321  Example2: x = -123, return -321 給出一個 32 位的有符號整數,你需要將這個整數中每位上的數字進行反轉。 示例&nb

leetcode 7. Reverse IntegerC語言,翻轉一個整數,判斷是否溢位19

貼原題: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return

LEETCODE 7 Reverse Integer JAVA題解

https://leetcode.com/problems/reverse-integer/ 原題如上,越簡單的題目,越考功底。 一般的解題思路,相信大家已經有了。關鍵是邊界情況 關鍵是,在JA