# Leetcode 7. Reverse Integer ###### tags: `Leetcode(C++)` 題目 : https://leetcode.com/problems/reverse-integer/ 。 想法 : 先轉成字串,翻轉後再轉回去int判斷有沒有溢位。 時間複雜度 : O(lgn)。 程式碼 : ``` using namespace std; class Solution { public: int reverse(int x) { string s=to_string(x); if(s[0] == '-') std::reverse(s.begin()+1,s.end()); else ::reverse(s.begin(),s.end()); try{ return stoi(s); } catch(::out_of_range& flow){ return 0; } } }; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up