1. 程式人生 > >leetcood學習筆記-7

leetcood學習筆記-7

三個參數 ins rip pretty ace rst max 描述 top


Python join()方法

join()方法語法:

str.join(sequence)

參數

  • sequence -- 要連接的元素序列。

返回值

返回通過指定字符連接序列中元素後生成的新字符串。

實例

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
str = "-";
seq = ("a", "b", "c"); # 字符串序列
print str.join( seq );

以上實例輸出結果如下:

a-b-c
故可用join將列表轉為字符串

python strip()函數 介紹

函數原型

聲明:s為字符串,rm為要刪除的字符序列

s.strip(rm) 刪除s字符串中開頭、結尾處,位於 rm刪除序列的字符
s.lstrip(rm) 刪除s字符串中開頭處,位於 rm刪除序列的字符
s.rstrip(rm) 刪除s字符串中結尾處,位於 rm刪除序列的字符

註意:

1. 當rm為空時,默認刪除空白符(包括‘\n‘, ‘\r‘, ‘\t‘, ‘ ‘)

Python3 replace()方法

描述
replace() 方法把字符串中的 old(舊字符串) 替換成 new(新字符串),如果指定第三個參數max,則替換不超過 max 次。

語法
replace()方法語法:

str.replace(old, new[, max])

參數
old -- 將被替換的子字符串。
new -- 新字符串,用於替換old子字符串。
max -- 可選字符串, 替換不超過 max 次
返回值
返回字符串中的 old(舊字符串) 替換成 new(新字符串)後生成的新字符串,如果指定第三個參數max,則替換不超過 max 次。

題目描述:

技術分享圖片技術分享圖片

第一次提交答案:

class Solution:
    def reverse(self, x: int) -> int:
        stx=str(x)
        listx=list(stx)
        if listx[0]==-:
            a
=listx.pop(0) listx.reverse() listx.insert(0,a) r="".join(listx) a=int(r) if a<(-2)**31: return 0 else : listx.reverse() r="".join(listx) a=int(r) if a>(2**31-1): return 0 return a










leetcood學習筆記-7