Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 813b5e7

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents 0a9653d + 64e0752 commit 813b5e7

6 files changed

+319
-124
lines changed

‎problems/0005.最长回文子串.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,34 @@ class Solution:
363363
Go:
364364

365365
```go
366+
func longestPalindrome(s string) string {
367+
maxLen := 0
368+
left := 0
369+
length := 0
370+
dp := make([][]bool, len(s))
371+
for i := 0; i < len(s); i++ {
372+
dp[i] = make([]bool,len(s))
373+
}
374+
for i := len(s)-1; i >= 0; i-- {
375+
for j := i; j < len(s); j++ {
376+
if s[i] == s[j]{
377+
if j-i <= 1{ // 情况一和情况二
378+
length = j-i
379+
dp[i][j]=true
380+
}else if dp[i+1][j-1]{ // 情况三
381+
length = j-i
382+
dp[i][j] = true
383+
}
384+
}
385+
}
386+
if length > maxLen {
387+
maxLen = length
388+
left = i
389+
}
390+
}
391+
return s[left: left+maxLen+1]
392+
}
393+
366394

367395
```
368396

‎problems/0054.螺旋矩阵.md‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,73 @@ public:
133133

134134
## 其他语言版本
135135

136+
### Java
137+
138+
```java
139+
class Solution {
140+
public List<Integer> spiralOrder(int[][] matrix) {
141+
//存放数组的数
142+
List<Integer> ans = new ArrayList<>();
143+
//列数
144+
int columns = matrix[0].length;
145+
//行数
146+
int rows = matrix.length;
147+
//遍历起点
148+
int start = 0;
149+
//循环的次数 行数和列数中的最小值除以二
150+
int loop = Math.min(rows,columns) / 2;
151+
//未遍历的中间列(行)的列(行)下标
152+
int mid = loop;
153+
//终止条件
154+
int offSet = 1;
155+
int i,j;
156+
while(loop-- > 0) {
157+
//初始化起点
158+
i = j = start;
159+
160+
//从左往右
161+
for(; j < columns - offSet; j++)
162+
ans.add(matrix[i][j]);
163+
164+
//从上往下
165+
for(; i < rows - offSet; i++)
166+
ans.add(matrix[i][j]);
167+
168+
//从右往左
169+
for(; j > start; j--)
170+
ans.add(matrix[i][j]);
171+
172+
//从下往上
173+
for(; i > start; i--)
174+
ans.add(matrix[i][j]);
175+
176+
//每循环一次 改变起点位置
177+
start++;
178+
//终止条件改变
179+
offSet++;
180+
}
181+
182+
//如果行和列中的最小值是奇数 则会产生中间行或者中间列没有遍历
183+
if(Math.min(rows,columns) % 2 != 0) {
184+
//行大于列则产生中间列
185+
if(rows > columns) {
186+
//中间列的行的最大下标的下一位的下标为mid + rows - columns + 1
187+
for(int k = mid; k < mid + rows - columns + 1; k++) {
188+
ans.add(matrix[k][mid]);
189+
}
190+
}else {//列大于等于行则产生中间行
191+
//中间行的列的最大下标的下一位的下标为mid + columns - rows + 1
192+
for(int k = mid; k < mid + columns - rows + 1; k++) {
193+
ans.add(matrix[mid][k]);
194+
}
195+
}
196+
}
197+
return ans;
198+
}
199+
}
200+
```
201+
202+
203+
136204
-----------------------
137205
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

‎problems/0100.相同的树.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,26 @@ class Solution:
237237
return True
238238
```
239239
Go:
240+
> 递归法
241+
```go
242+
func isSameTree(p *TreeNode, q *TreeNode) bool {
243+
if p != nil && q == nil {
244+
return false
245+
}
246+
if p == nil && q != nil {
247+
return false
248+
}
249+
if p == nil && q == nil {
250+
return true
251+
}
252+
if p.Val != q.Val {
253+
return false
254+
}
255+
Left := isSameTree(p.Left, q.Left)
256+
Right := isSameTree(p.Right, q.Right)
257+
return Left && Right
258+
}
259+
```
240260

241261
JavaScript:
242262

@@ -253,6 +273,28 @@ var isSameTree = function (p, q) {
253273
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
254274
};
255275
```
276+
> 迭代法
277+
278+
```javascript
279+
var isSameTree = (p, q) => {
280+
const queue = [{ p, q }];
281+
// 这是用{ } 解决了null的问题!
282+
while (queue.length) {
283+
const cur = queue.shift();
284+
if (cur.p == null && cur.q == null) continue;
285+
if (cur.p == null || cur.q == null) return false;
286+
if (cur.p.val != cur.q.val) return false;
287+
queue.push({
288+
p: cur.p.left,
289+
q: cur.q.left
290+
}, {
291+
p: cur.p.right,
292+
q: cur.q.right
293+
});
294+
}
295+
return true;
296+
};
297+
```
256298

257299
TypeScript:
258300

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /