iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 13
0
自我挑戰組

有志者,事竟成。系列 第 13

Day13 LeetCode #7 Reverse Integer

題目描述

給你一個整數,請反序輸出。

思路

.......直接寫?

程式碼

class Solution {
public:
    int reverse(int x) {
        int ans=0;
        while(x!=0)
        {
            if(ans>=214748365||ans<=-214748365)
                return 0;
            ans=ans*10+x%10;
            x/=10;
        }
        return ans;        
    }
};

心路歷程

其實本身這題根本沒什麼難度,但其實也是碰過error的。
runtime error: signed integer overflow: 925935840 * 10 cannot be represented in type 'int' (solution.cpp)
簡單來說就是當 ansx10 的時候我們也不能讓她溢位,剩下就沒有問題了。


上一篇
Day12 LeetCode #6 ZigZag Conversion
下一篇
Day14 LeetCode #8 String to Integer (atoi)
系列文
有志者,事竟成。19
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言