python刷LeetCode 之 【reverse數字】
給定一個 32 位有符號整數,將整數中的數字進行反轉。
示例 1: 輸入: 123 輸出: 321
示例 2: 輸入: -123 輸出: -321
示例 3: 輸入: 120 輸出: 21
注意:
假設我們的環境只能儲存 32 位有符號整數,其數值範圍是 [−231, 231 − 1]。根據這個假設,如果反轉後的整數溢位,則返回 0。
class Solution:
def reverse(x):
"""
:type x: int
:rtype: int
"""
if x >= 0:
rNum = int(str(x)[::-1])
else:
rNum = int("-" + str(-x)[::-1])
# 判斷返回引數是否超出整數範圍
if rNum > (pow(2, 31) - 1) or rNum < pow(-2, 31):
return 0
else:
return rNum
相關推薦
python刷LeetCode 之 【reverse數字】
給定一個 32 位有符號整數,將整數中的數字進行反轉。 示例 1: 輸入: 123 輸出: 321 示例 2: 輸入: -123 輸出: -321 示例 3: 輸入: 120 輸出: 21 注意: 假設我們的環境只能儲存 32 位有符號整數,其數值範圍是 [
python刷LeetCode 之 【reverse數字】
給定一個 32 位有符號整數,將整數中的數字進行反轉。 示例 1: 輸入: 123 輸出: 321 示例 2: 輸入: -123 輸出: -321 示例 3: 輸入: 120 輸出: 21 注意: 假設我們的環
python 刷LeetCode 之 【數字的補數】
class Solution: def findComplement(num): """ :type num: int :rtype: int """ i = 1 #從第一位開始一位一位按
python 刷LeetCode 之 【漢明距離】
class Solution: def hammingDistance(x, y): """ :type x: int :type y: int :rtype: int """ # 相當於
Python學習筆記之【高階特性】
切片 選取list或str的一部分 >>> L[0:3] >>> L[:3] >>> L[1:3] >>> L[-2:] >>> L[-2:-1] 前10個數 >>&g
【一】Leetcode之Python刷題之路
申請完了github帳號,又在Leetcode上發現了新大陸,從昨天開始刷題了,感覺很有意思。看視訊學習Python動手不太多,現在開始刷題練習自己的程式碼水平,leetcode上還有很多大神,快哉美哉! 程式碼我都放到github裡了https://
用Python刷LeetCode【1.TwoSum】
class Solution: def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype:
python標準庫之【socket】
yun lock .cn 函數返回 targe ddr 是個 進程間的通信 log socket通常也稱作”套接字“。網絡上的兩個程序通過一個雙向的通信連接實現數據的交換,這個連接的一端稱為一個socket。socket 是網絡連接端點。例如當你的W
C#LeetCode刷題之#374-猜數字大小(Guess Number Higher or Lower)
問題 我們正在玩一個猜數字遊戲。 遊戲規則如下: 我從 1 到 n 選擇一個數字。 你需要猜我選擇了哪個數字。 每次你猜錯了,我會告訴你這個數字是大了還是小了。 你呼叫一個預先定義好的介面 guess(int num),它會返回 3 個可能的結果(-1,1 或 0): -1 :
C#LeetCode刷題之#788-旋轉數字(Rotated Digits)
問題 我們稱一個數 X 為好數, 如果它的每位數字逐個地被旋轉 180 度後,我們仍可以得到一個有效的,且和 X 不同的數。要求每位數字都要被旋轉。 如果一個數的每位數字被旋轉以後仍然還是一個數字, 則這個數是有效的。0, 1, 和 8 被旋轉後仍然是它們自己;2 和 5 可以互相旋轉成
Python 基本資料型別之【編碼問題】
content 字符集 常見的字符集舉例 位元組和字串之間的轉換 編碼問題 1. 字符集 a="A" a=b"A" # 計算機中儲存只是0101二進位制程式碼 # 字符集: 一堆字元的集合,用來制定當前的字元對映成計算機中儲存的ascii規則
python基礎之【元組 】(04)
Python 的元組與列表類似,不同之處在於元組的元素不能修改。 元組使用小括號,列表使用方括號。 元組建立很簡單,只需要在括號中新增元素,並使用逗號隔開即可。 tup1 = ('Google', 'Runoob', 1997, 2000); 元組
Python 基本資料型別之【格式化問題】
content **%**進行格式化 format進行格式化 f常量進行格式化 1. %進行格式化 # 有幾個%佔位,後面就跟著幾個變數或者值,按照順序對應 # 在python2的時候使用的比較多 # %s-----str(變數) # %d %f
Python學習筆記之【輸入和輸出】
文章內容基於廖雪峰Python教程,參考慕課網相關課程,並進行了整理。文章僅供本人學習使用,若侵權將立刻刪除。 若無特別說明,文章內容都針對於Python2.7版本。 輸出 print加上字串、整數或變數,即可實現輸出。 多個輸出項需要用逗號隔開,輸出時會自動以空格隔
Python學習筆記之【字串和編碼】
字元編碼 最早的ASCII,只有127個字母被編到計算機中。中國製定了GB2312編碼,用來把中文編進去。其它語言也出現了各自的編碼。 為了統一編碼,產生了Unicode,它把所有語言都統一到一套編碼裡,解決了亂碼問題,Unicode編碼通常是2個位元組。 為了節約,出
劍指offer之【醜數】☆
下一個 一個數 ice amp spa public cto interview 但我 題目: 醜數 鏈接: https://www.nowcoder.com/practice/6aa9e04fc3794f68acf8778237ba065b?tpId=13&
ArcGIS 網絡分析[8.1] 資料1 使用AO打開或創建網絡數據集之【打開】
find 重要 database red color play ace ext set 為了創建或打開一個網絡數據集,你必須使用NetworkDatasetFDExtension對象(文件地理數據庫中的數據集)或NetworkDatasetWorkspaceExtensi
用戶空間和內核空間通訊之【Netlink 上】
提取 意思 數據報 multi 主動 聯合 興趣 ora 指示 原文地址:用戶空間和內核空間通訊之【Netlink 上】 作者:wjlkoorey258 引言 Alan Cox在內核1.3版本的開發階段最先引入了Netlink,剛開始時Netlink是以
刷leetcode之路
strong class -1 pac == lag ace style dig 寫的不是很好,僅記錄自己所寫的,僅供參考。 第七題: Reverse digits of an integer. Example1: x = 123, return 321 Example2
“一切都是消息”--MSF(消息服務框架)之【請求-響應】模式
手動 emp void syn 封裝 none 必須 服務端 req 在前一篇, “一切都是消息”--MSF(消息服務框架)入門簡介, 我們介紹了MSF基於異步通信,支持請求-響應通信模式和發布-訂閱通信模式,並且介紹了如何獲取MSF。今天,我們來看