數(shù)據(jù)結(jié)構(gòu)與算法的重要性就不多說了觅捆,幾乎沒有一個(gè)一線互聯(lián)網(wǎng)公司招聘任何類別的技術(shù)人員是不考算法的赦役,程序猿們都懂的,現(xiàn)在最權(quán)威流行的刷題平臺(tái)就是 LeetCode栅炒。閑話少講掂摔,步入正題。
Question:
Given a 32-bit signed integer, reverse digits of an integer.
Example:
Input: 123
Output: 321
Input: -123
Output: -321
Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [?231, 231 ? 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Solution
以下代碼皆是本人用 C++寫的赢赊,覺得還不錯(cuò)的話別忘了點(diǎn)個(gè)贊哦乙漓。各位同學(xué)們?nèi)绻衅渌咝Ы忸}思路還請(qǐng)不吝賜教,多多學(xué)習(xí)释移。
A1叭披、對(duì)數(shù)循環(huán)
算法時(shí)間復(fù)雜度 O(logn),Runtime: 25 ms
玩讳,代碼如下
class Solution {
public:
int reverse(int x) {
long res = 0;
while (x) {
res = res*10 + x%10;
x /= 10;
}
return (res < INT32_MIN || res > INT32_MAX) ? 0 : (int)res;
}
};