力扣題目解答自我總結(反轉類題目) 一.反轉字元串 1.題目描述 編寫一個函數,其作用是將輸入的字元串反轉過來。輸入字元串以字元數組 的形式給出。 不要給另外的數組分配額外的空間,你必須 原地修改輸入數組 、使用 O(1) 的額外空間解決這一問題。 你可以假設數組中的所有字元都是 "ASCII" 碼 ...
力扣題目解答自我總結(反轉類題目)
一.反轉字元串
1.題目描述
編寫一個函數,其作用是將輸入的字元串反轉過來。輸入字元串以字元數組 char[]
的形式給出。
不要給另外的數組分配額外的空間,你必須原地修改輸入數組、使用 O(1) 的額外空間解決這一問題。
你可以假設數組中的所有字元都是 ASCII 碼表中的可列印字元。
示例 1:
輸入:["h","e","l","l","o"]
輸出:["o","l","l","e","h"]
示例 2:
輸入:["H","a","n","n","a","h"]
輸出:["h","a","n","n","a","H"]
2.解答
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
st_num = 0
e_num = len(s)-1
while e_num >st_num:
s[st_num],s[e_num] = s[e_num],s[st_num]
st_num += 1
e_num -= 1
#難點就是在O(1) 下運行
二.反轉整數
1.題目描述
給出一個 32 位的有符號整數,你需要將這個整數中每位上的數字進行反轉。
示例 1:
輸入: 123
輸出: 321
示例 2:
輸入: -123
輸出: -321
示例 3:
輸入: 120
輸出: 21
2.解答
class Solution:
def reverse(self, x: int) -> int:
new_x = '-'
x = str(x)
x = x[::-1]
while x[-1] == 0: #刪除最後一位的 0
x = x[:-1]
if x[-1] == '-': #刪除有括弧的
x = x[:-1]
while x[-1] == 0:
x = x[:-1]
new_x += x
x = new_x
if int(x) < -2**31 or int(x) >2**31-1:
return 0
else:
return int(x)
三.旋轉圖像
1.題目描述
給定一個 n × n 的二維矩陣表示一個圖像。
將圖像順時針旋轉 90 度。
說明:
你必須在原地旋轉圖像,這意味著你需要直接修改輸入的二維矩陣。請不要使用另一個矩陣來旋轉圖像。
示例 1:
給定 matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],
原地旋轉輸入矩陣,使其變為:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
示例 2:
給定 matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],
原地旋轉輸入矩陣,使其變為:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
2.解答
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
import copy
new_list = []
time = 0
conter = 0
matrix.reverse()
matrix_1 = copy.copy(matrix)
print(matrix)
if len(matrix) != 0:
for a in range(len(matrix)):
matrix[a] = []
while len(matrix) > len(matrix_1[0]):
matrix.pop()
while len (matrix) < len(matrix_1[0]):
matrix.append([])
for b in range(len(matrix_1[0])):
for c in range(len(matrix_1)):
if time != len(matrix_1):
time += 1
matrix[conter].append(matrix_1[c][b])
elif time == len(matrix_1):
time = 1
conter += 1
matrix[conter].append(matrix_1[c][b])
#這是我的思路比較low,先水平翻轉,再按照子列表的長度,把他變成有擁有子列表長度一樣的空的列表,再裡面填寫值進去,寫完了頭有點昏,等下次有空再優化下代碼
四.反轉字元串中的單詞 Ⅲ
1.題目描述
給定一個字元串,你需要反轉字元串中每個單詞的字元順序,同時仍保留空格和單詞的初始順序。
示例 1:
輸入: "Let's take LeetCode contest"
輸出: "s'teL ekat edoCteeL tsetnoc"
註意:在字元串中,每個單詞由單個空格分隔,並且字元串中不會有任何額外的空格。
2.解答
class Solution:
def reverseWords(self, s: str) -> str:
s_1 = ''
for i in s.split():
s_1 += i[::-1]+' '
s_1 = s_1[:-1]
return s_1
#這個寫法比較LOW
五.有效數獨
1.題目描述
判斷一個 9x9 的數獨是否有效。只需要根據以下規則,驗證已經填入的數字是否有效即可。
- 數字
1-9
在每一行只能出現一次。 - 數字
1-9
在每一列只能出現一次。 - 數字
1-9
在每一個以粗實線分隔的3x3
宮內只能出現一次。
上圖是一個部分填充的有效的數獨。
數獨部分空格內已填入了數字,空白格用 '.'
表示。
示例 1:
輸入:
[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
輸出: true
示例 2:
輸入:
[
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
輸出: false
解釋: 除了第一行的第一個數字從 5 改為 8 以外,空格內其他數字均與 示例1 相同。
但由於位於左上角的 3x3 宮內有兩個 8 存在, 因此這個數獨是無效的。
說明:
- 一個有效的數獨(部分已被填充)不一定是可解的。
- 只需要根據以上規則,驗證已經填入的數字是否有效即可。
- 給定數獨序列只包含數字
1-9
和字元'.'
。 - 給定數獨永遠是
9x9
形式的。
2.解答
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
import copy
x = True
s_1 = ''
s_2 = ''
new_list = copy.deepcopy(List)
list_1 = copy.copy(List)
for num_1 in range(9):
for num_2 in range(9):
new_list[num_1][num_2] = List[num_2][num_1]
new_list_1 =copy.copy(new_list)
for a_1 in range(9):
for a_2 in range(9):
s_1 += str(List[a_1][a_2])
list_1[a_1] = s_1
s_2 += str(new_list[a_1][a_2])
new_list_1[a_1] = s_2
if len(list_1[a_1]) == 9:
list_1[a_1] = list_1[a_1].replace('.','')
new_list_1[a_1] = new_list_1[a_1].replace('.', '')
s_1 = ''
s_2 = ''
num_1 = len(list_1[a_1])-len(set(list_1[a_1]))
num_2 = len(new_list_1[a_1])-len(set(new_list_1[a_1]))
if num_1 != 0 or num_2 != 0:
x = False
return x
#pycharm能運行,力扣里就不行,很悶逼大神路過留個言
#下麵是力扣里報錯內容
#Line 11: TypeError: Parameters to generic types must be types. Got 0.
如果你有更加弔炸天的解題方法留言,讓我這渣渣學學```