Python 實(shí)現(xiàn)反轉(zhuǎn)整數(shù)的案例(很容易懂的那種)
題目:
給出一個(gè) 32 位的有符號整數(shù),你需要將這個(gè)整數(shù)中每位上的數(shù)字進(jìn)行反轉(zhuǎn)。
示例 1:
輸入: 123
輸出: 321
示例 2:
輸入: -123
輸出: -321
示例 3:
輸入: 120
輸出: 21
注意:
假設(shè)我們的環(huán)境只能存儲得下 32 位的有符號整數(shù),則其數(shù)值范圍為 。請根據(jù)這個(gè)假設(shè),如果反轉(zhuǎn)后整數(shù)溢出那么就返回 0。
解題思路:
1.實(shí)現(xiàn)數(shù)據(jù)的反轉(zhuǎn)
如果是正數(shù):
tra = 0 while x != 0: n2 = x%10 x = x //10 tra = tra*10 + n2
如果是負(fù)數(shù)就abs()一下這個(gè)數(shù)
2.溢出判定
給出范圍[−2^31, 2^31 − 1]
則輸出的結(jié)果tra就必須滿足這個(gè)范圍.
代碼:
class Solution(object): def reverse(self, x): base = 1 for i in range(31): base = base * 2 two_Max = base - 1 two_Min = -base tra = 0 if x < 0: x = abs(x) while x != 0: n2 = x % 10 if tra > abs(two_Min) // 10 or (tra == abs(two_Min) // 10 and n2 < -8): return 0 x = x // 10 tra = tra * 10 + n2 return -tra else: while x != 0: n2 = x % 10 if tra > two_Max//10 or (tra == two_Max and n2 > 7 ): return 0 x = x // 10 tra = tra * 10 + n2 return tra
補(bǔ)充:python實(shí)現(xiàn)數(shù)字反轉(zhuǎn)_python 數(shù)字怎么反轉(zhuǎn)
每次寫 Python 都會(huì)忘記該怎么寫,最后只能去 Stack Overflow 查?我也一樣。時(shí)間一長,這讓人厭倦。
這15個(gè) Python 技巧和竅門,可以幫你提高效率
1. 交換值
x, y = 1, 2 print(x, y) x, y = y, x print(x, y)
2. 字符串列表合并為一個(gè)字符串
sentence_list = ["my", "name", "is", "George"] sentence_string = " ".join(sentence_list) print(sentence_string)
3. 將字符串拆分為子字符串列表
sentence_string = "my name is George" sentence_string.split() print(sentence_string)
4. 通過數(shù)字填充初始化列表
[0]*1000 # List of 1000 zeros [8.2]*1000 # List of 1000 8.2's
5. 字典合并
x = {'a': 1, 'b': 2} y = {'b': 3, 'c': 4} z = {**x, **y}
6. 反轉(zhuǎn)字符串
name = "George" name[::-1]
7. 從函數(shù)返回多個(gè)值
def get_a_string(): a = "George" b = "is" c = "cool" return a, b, c sentence = get_a_string() (a, b, c) = sentence
8. 列表解析式
a = [1, 2, 3] b = [num*2 for num in a] # Create a new list by multiplying each element in a by 2
9. 遍歷字典
m = {'a': 1, 'b': 2, 'c': 3, 'd': 4} for key, value in m.items(): print('{0}: {1}'.format(key, value))
10. 同時(shí)遍歷列表的索引和值
m = ['a', 'b', 'c', 'd'] for index, value in enumerate(m): print('{0}: {1}'.format(index, value))
11. 初始化空容器
a_list = list() a_dict = dict() a_map = map() a_set = set()
12. 刪除字符串兩端的無用字符
name = " George " name_2 = "George///" name.strip() # prints "George" name_2.strip("/") # prints "George"
13. 列表中出現(xiàn)最多的元素
test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4] print(max(set(test), key = test.count))
14. 檢查對象的內(nèi)存使用情況
import sys x = 1 print(sys.getsizeof(x))
15. 將 dict 轉(zhuǎn)換為 XML
from xml.etree.ElementTree import Element def dict_to_xml(tag, d): ''' Turn a simple dict of key/value pairs into XML ''' elem = Element(tag) for key, val in d.items(): child = Element(key) child.text = str(val) elem.append(child) return elem
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
python獲取當(dāng)前時(shí)間對應(yīng)unix時(shí)間戳的方法
這篇文章主要介紹了python獲取當(dāng)前時(shí)間對應(yīng)unix時(shí)間戳的方法,涉及Python時(shí)間操作的相關(guān)技巧,非常簡單實(shí)用,需要的朋友可以參考下2015-05-05Python中rapidjson參數(shù)校驗(yàn)實(shí)現(xiàn)
通常需要對前端傳遞過來的參數(shù)進(jìn)行校驗(yàn),校驗(yàn)的方式有多種,本文主要介紹了Python中rapidjson參數(shù)校驗(yàn)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07python數(shù)據(jù)類型bytes?和?bytearray的使用與區(qū)別
本文主要介紹了python數(shù)據(jù)類型bytes?和?bytearray的使用與區(qū)別,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02python連接clickhouse數(shù)據(jù)庫的兩種方式小結(jié)
這篇文章主要介紹了python連接clickhouse數(shù)據(jù)庫的兩種方式小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05Qt5 實(shí)現(xiàn)主窗口狀態(tài)欄顯示時(shí)間
這篇文章主要介紹了Qt5 實(shí)現(xiàn)主窗口狀態(tài)欄顯示時(shí)間,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03Django的HttpRequest和HttpResponse對象詳解
這篇文章主要介紹了Django的HttpRequest和HttpResponse對象,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01詳解sklearn?Preprocessing?數(shù)據(jù)預(yù)處理功能
這篇文章主要介紹了sklearn?Preprocessing?數(shù)據(jù)預(yù)處理功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08