Leetcodes Solutions 7 Reverse Integer

匯總

雪之下雪乃:leetcode解題總匯?

zhuanlan.zhihu.com圖標

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123Output: 321

Example 2:

Input: -123Output: -321

Example 3:

Input: 120Output: 21

Note:

Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

思路1

long long make res a 64 bit number, the overflow is checked..

求膜得到最後一位,再除10去掉原數最後一位

class Solution{public: int reverse(int x){ long long res = 0; while(x){ res = res * 10 + x % 10; x /= 10; } return (res < INT_MIN || res > INT_MAX) ? 0 : res; }};

推薦閱讀:

從零開始手敲次世代遊戲引擎(四十八)
LintCode/LeetCode 概括總結全集
模擬退火演算法學習筆記
九章演算法 | Snapchat 面試題 : Palindrome Data Stream
正則表達式匹配

TAG:編程 | 演算法 | 數據結構 |