LeetCode Algorithm 7. 整数反转
生活随笔
收集整理的這篇文章主要介紹了
LeetCode Algorithm 7. 整数反转
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
7. 整數反轉
Ideas
很經典的一道練習題,數字翻轉、字符串翻轉都是需要掌握的內容。
數字翻轉相對比較簡單,基本思路就是依次取數字的最后一位拼接起來。
Code
C++
#include <iostream>using namespace std;class Solution { public:int reverse(int x) {int res = 0;while (x != 0) {if (res < INT_MIN / 10 || res > INT_MAX / 10) return 0;res = res * 10 + x % 10;x /= 10;}return res;} };Python
class Solution:def reverse(self, x: int) -> int:negative = True if x < 0 else Falsex = int("".join(list(reversed(list(str(abs(x)))))))if not (-(1 << 31) < x < (1 << 31) - 1):return 0return -x if negative else x 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的LeetCode Algorithm 7. 整数反转的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 6. Z 字形变换
- 下一篇: 面试题64. 求1+2+…+n