# 力扣题目解答自我总结（反转类题目）

## 一.反转字符串

### 1.题目描述

```输入：["h","e","l","l","o"]

```输入：["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.题目描述

```输入: 123

```输入: -123

```输入: 120

### 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.题目描述

```给定 matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],

[
[7,4,1],
[8,5,2],
[9,6,3]
]```

```给定 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.题目描述

```输入: "Let's take LeetCode contest"

### 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.题目描述

1. 数字 `1-9` 在每一行只能出现一次。
2. 数字 `1-9` 在每一列只能出现一次。
3. 数字 `1-9` 在每一个以粗实线分隔的 `3x3` 宫内只能出现一次。

```输入:
[
["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"]
]

```输入:
[
["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"]
]

但由于位于左上角的 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.```

0 条评论

• ### 力扣题目汇总（最长连续递增序列，旋转图像（中等），宝石与石头）

你必须在原地旋转图像，这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。

• ### python词云生成-wordcloud库

全文转载于'https://www.cnblogs.com/nickchen121/p/11208274.html#autoid-0-0-0'

• ### MIT 发明“雾中看车”新成像系统，雾天自动驾驶表现比人类更好

Given a non-negative integer num, repeatedly add all its digits until the result...

• ### h5软键盘挡住输入框问题解决（android）

在部分android机型上测试点击靠下的输入框时遇到弹出的软键盘挡住输入框问题，ios可自身弹起（ios自身的调整偶尔也会出问题，例如第三方键盘会遮挡，原因是第...

• ### 版本问题 Java:Unsupported major.minor version 51.0 (unable to load class . . .

导入别人的项目时报错 Java:Unsupported major.minor version 51.0 (unable to load class . . ...

• ### 每日一刷：最长公共前缀

输入: ["dog","racecar","car"] 输出: "" 解释: 输入不存在公共前缀。