【LeetCode】7.Reverse Integer 整數反轉
示例1:
Input: 123 Output: 321
注意:
假設我們的環境只能儲存得下 32 位的有符號整數,則其數值範圍為 [−231, 231 − 1]。請根據這個假設,如果反轉後整數溢位那麼就返回 0。
解題思路:
以後過於簡單的題不寫進部落格了。
if x < 0: res = - int(str(x)[: 0: -1]) if res < - 2 ** 31: return 0 return res else: res = int(str(x)[: : -1]) if res > 2 ** 31 - 1: return 0 return res
72 ms,打敗了44.30%的對手。
解題思路2:
排名靠前的前輩,使用了abs函式取絕對值。
相關推薦
【LeetCode】7.Reverse Integer 整數反轉
示例1: Input: 123 Output: 321 注意: 假設我們的環境只能儲存得下 32 位的有符號整數,則其數值範圍為 [−231, 231 − 1]。請根據這個假設,如果反轉後整數溢位那麼就返回 0。 解題思路: 以後過於簡單的題不寫進
【leetcode】7. Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321
【leetcode】7. Reverse Integer(C)
Description: Given a 32-bit signed integer, reverse digits of an integer. Example1: Input: 123 Output: 321 Example2: Input: -1
leetcode-7 reverse-integer(整數反轉)
我們先看一下題目描述: 就是將數字進行反轉,可以用數學方法將其解答。 public static int reverse(int x) { long a = 0; while (x != 0) { a = a * 10 + x
leetcode-7:Reverse Integer整數反轉
題目: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321
Leetcode 7. Reverse Integer 整數反轉
Leetcode 7. Reverse Integer 整數反轉 標籤: Leetcode 題目地址:https://leetcode-cn.com/problems/reverse-integer/ 題目描述 給出一個 32 位的有符號整數,你需要將這個整數中每位
【LeetCode】124.Reverse Integer
題目描述(Easy) Given a 32-bit signed integer, reverse digits of an integer. 題目連結 https://leetcode.com/problems/reverse-integer/description/ Exampl
【Leetcode】之Reverse Integer
題目描述: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 我的解題思路: 這道題就是一套數學題,給定一個10進位制
【LeetCode】#7整數反轉(Reverse Integer)
【LeetCode】#7整數反轉(Reverse Integer) 題目描述 給出一個 32 位的有符號整數,你需要將這個整數中每位上的數字進行反轉。 示例 示例 1: 輸入: 123 輸出: 321 示例 2: 輸入: -123 輸出: -321 示例 3: 輸
Leetcode解題 7. Reverse Integer 反轉整數
ret light leetcode on() def rev python 整數 pre 沒看清要求,提交錯誤一次。 要求是 "如果反轉後的整數溢出,則返回 0"。 class Solution(object): def reverse(self, x)
【LeetCode】7. 反轉整數
題目連結:https://leetcode-cn.com/problems/reverse-integer/description/ 題目描述 給定一個 32 位有符號整數,將整數中的數字進行反轉。 示例1 輸入: 123 輸出: 321 示例2 輸入: 120
【LeetCode】7. 反轉整數 結題報告 (C++)
題目描述:給定一個 32 位有符號整數,將整數中的數字進行反轉。示例 1:輸入: 123 輸出: 321 示例 2:輸入: -123 輸出: -321 示例 3:輸入: 120 輸出: 21 注意:假設我們的環境只能儲存 32 位有符號整數,其數值範圍是 [−231, 2
【leetcode】7 反轉整數 (c++)
題目連結給定一個 32 位有符號整數,將整數中的數字進行反轉。示例 1:輸入: 123 輸出: 321 示例 2:輸入: -123 輸出: -321 示例 3:輸入: 120 輸出: 21 注意:假設
【python】【leetcode】【演算法題目7—Reverse Integer】
一、題目描述 題目原文: Reverse digits of an integer. (將一個整數反轉輸出) 舉例: Example1: x = 123, return 321Exa
【LeetCode】7. 整數反轉
題目描述: 給出一個 32 位的有符號整數,你需要將這個整數中每位上的數字進行反轉。 示例 1: 輸入: 123 輸出: 321 示例 2: 輸入: -123 輸出: -321 示例 3: 輸入: 120 輸出: 21 注意: 假設我們
LeetCode 7. Reverse Integer(反轉整數)
Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the inte
【LeetCode】#13羅馬數字轉整數(Roman to Integer)
【LeetCode】#13羅馬數字轉整數(Roman to Integer) 題目描述 羅馬數字包含以下七種字元: I, V, X, L,C,D 和 M。 字元 數值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 羅馬數字 2 寫做 II ,即為兩
【LeetCode】#8字串轉換整數(String to Integer)
【LeetCode】#8字串轉換整數(String to Integer) 題目描述 請你來實現一個 atoi 函式,使其能將字串轉換成整數。 首先,該函式會根據需要丟棄無用的開頭空格字元,直到尋找到第一個非空格的字元為止。 當我們尋找到的第一個非空字元為正或者負號時,則將該符號與之
【LeetCode】345.Reverse Vowels of a String(反轉字串中的母音字母)-C++實現
本題為谷歌面試題。 問題描述: 一、第一種方法:對撞指標法 #include <iostream> #include <vector> #include <string> #include <cassert> #inc
7. Reverse Integer(反轉整數)
Description:Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123 Output: 321 Example 2:Input: -123 Output: -321 Example 3:Inpu