Skip to content

Commit 2170a1e

Browse files
committed
feat: commit solution
1 parent 39d37d2 commit 2170a1e

File tree

9 files changed

+256
-2
lines changed

9 files changed

+256
-2
lines changed

_sidebar.md

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
- [21.合并两个有序链表 ✅](solution/1-99/0021.merge-two-sorted-lists/)
1717
- [26.删除排序数组中的重复项 ✅](solution/1-99/0026.remove-duplicates-from-sorted-array/)
1818
- [27.移除元素 ✅](solution/1-99/0027.remove-element/)
19+
- [88.合并两个有序数组](solution/1-99/0088.merge-sorted-array/)
20+
- [125.验证回文串 ✅](solution/100-199/0125.valid-palindrome/)
21+
22+
1923

2024

2125

index-tags.md

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
| --- | --- | --- | --- | --- |
1717
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> ||
1818
| [20](https://leetcode-cn.com/problems/valid-parentheses) | [有效的括号](/solution/1-99/0020.valid-parentheses/) | ``,`字符串` | <font color=green>简单</font> ||
19+
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
1920

2021
#### **数组**
2122

@@ -24,6 +25,7 @@
2425
| [1](https://leetcode-cn.com/problems/two-sum) | [两数之和](/solution/1-99/0001.two-sum/) | `数组`,`哈希表` | <font color=green>简单</font> ||
2526
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
2627
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
28+
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
2729

2830
#### **哈希表**
2931

@@ -61,6 +63,8 @@
6163
| --- | --- | --- | --- | --- |
6264
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
6365
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
66+
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
67+
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
6468

6569
#### **递归**
6670

index-type.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
| [69](https://leetcode-cn.com/problems/sqrtx) | [x 的平方根](/solution/1-99/0069.sqrt%28x%29/) | `数学`,`二分查找` | <font color=green>简单</font> |
2727
| [70](https://leetcode-cn.com/problems/climbing-stairs) | [爬楼梯](/solution/1-99/0070.climbing-stairs/) | `动态规划` | <font color=green>简单</font> |
2828
| [83](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list) | [删除排序链表中的重复元素](/solution/1-99/0083.remove-duplicates-from-sorted-list/) | `链表` | <font color=green>简单</font> |
29-
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> |
29+
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
3030
| [100](https://leetcode-cn.com/problems/same-tree) | [相同的树](/solution/100-199/0100.same-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> |
3131
| [101](https://leetcode-cn.com/problems/symmetric-tree) | [对称二叉树](/solution/100-199/0101.symmetric-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> |
3232
| [104](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree) | [二叉树的最大深度](/solution/100-199/0104.maximum-depth-of-binary-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> |
@@ -39,7 +39,7 @@
3939
| [119](https://leetcode-cn.com/problems/pascals-triangle-ii) | [杨辉三角 ii](/solution/100-199/0119.pascal%27s-triangle-ii/) | `数组` | <font color=green>简单</font> |
4040
| [121](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock) | [买卖股票的最佳时机](/solution/100-199/0121.best-time-to-buy-and-sell-stock/) | `数组`,`动态规划` | <font color=green>简单</font> |
4141
| [122](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii) | [买卖股票的最佳时机 ii](/solution/100-199/0122.best-time-to-buy-and-sell-stock-ii/) | `贪心算法`,`数组` | <font color=green>简单</font> |
42-
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> |
42+
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
4343
| [136](https://leetcode-cn.com/problems/single-number) | [只出现一次的数字](/solution/100-199/0136.single-number/) | `位运算`,`哈希表` | <font color=green>简单</font> |
4444
| [141](https://leetcode-cn.com/problems/linked-list-cycle) | [环形链表](/solution/100-199/0141.linked-list-cycle/) | `链表`,`双指针` | <font color=green>简单</font> |
4545
| [155](https://leetcode-cn.com/problems/min-stack) | [最小栈](/solution/100-199/0155.min-stack/) | ``,`设计` | <font color=green>简单</font> |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# [88.合并两个有序数组](https://leetcode-cn.com/problems/merge-sorted-array)
2+
3+
4+
### 题目描述
5+
6+
<div class="notranslate"><p>给你两个有序整数数组&nbsp;<em>nums1 </em>和 <em>nums2</em>,请你将 <em>nums2 </em>合并到&nbsp;<em>nums1&nbsp;</em>中<em>,</em>使 <em>nums1 </em>成为一个有序数组。</p>
7+
8+
<p>&nbsp;</p>
9+
10+
<p><strong>说明:</strong></p>
11+
12+
<ul>
13+
<li>初始化&nbsp;<em>nums1</em> 和 <em>nums2</em> 的元素数量分别为&nbsp;<em>m</em> 和 <em>n </em>。</li>
14+
<li>你可以假设&nbsp;<em>nums1&nbsp;</em>有足够的空间(空间大小大于或等于&nbsp;<em>m + n</em>)来保存 <em>nums2</em> 中的元素。</li>
15+
</ul>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong>示例:</strong></p>
20+
21+
<pre><strong>输入:</strong>
22+
nums1 = [1,2,3,0,0,0], m = 3
23+
nums2 = [2,5,6], n = 3
24+
25+
<strong>输出:</strong>&nbsp;[1,2,2,3,5,6]</pre>
26+
</div>
27+
28+
### 解题思路
29+
30+
1. 采用双指针分别将两个数组 `nums1` 和数组 `nums2` 从后向前插入到数组 `nums1`
31+
2. 指针 `i` 标记数组 nums1 当前比较位置,开始位数组元素最后位
32+
3. 指针 `j` 标记数组 nums2 当前比较位置,开始位数组元素最后位
33+
4. 指针 `k` 表示结果从后向前当前可插入位置,默认 `nums1``n` 位都是可插入位,将 `nums1` 中元素插入当前可插入位后, 从 指针 `i``k` 都是可插入位
34+
5. 循环将数组 `nums2` 插入到数组 `nums1`
35+
6. 当数组 `nums1` 当前可比较数据位 `i` 大于等于 0 并且数组 `nums1` 当前位置值比 `nums2` 当前位置值大时,将数组 `nums1` 当前位置数组 插入到 `k` 位置 ,`i` 位指针向前挪一位
36+
7. 否则将 `nums2` 当前位置数据插入到 `k` 位置,`j` 位指针向前挪一位
37+
8. 插入完成后将当前插入位置指针 `k` 向前挪一位,进行下一次插入
38+
39+
![](http://lc-photo.xwlin.com/88.png)
40+
41+
### 代码实现
42+
43+
<!-- tabs:start -->
44+
45+
#### **Golang**
46+
```go
47+
func merge(nums1 []int, m int, nums2 []int, n int) {
48+
var i, j = m - 1, n - 1
49+
50+
for k := m + n - 1; k >= 0; k-- {
51+
if j < 0 || (i >= 0 && nums1[i] > nums2[j]) {
52+
nums1[k] = nums1[i]
53+
i--
54+
} else {
55+
nums1[k] = nums2[j]
56+
j--
57+
}
58+
}
59+
}
60+
```
61+
62+
63+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=88 lang=golang
5+
*
6+
* [88] 合并两个有序数组
7+
*/
8+
func merge(nums1 []int, m int, nums2 []int, n int) {
9+
var i, j = m - 1, n - 1
10+
11+
for k := m + n - 1; k >= 0; k-- {
12+
if j < 0 || (i >= 0 && nums1[i] > nums2[j]) {
13+
nums1[k] = nums1[i]
14+
i--
15+
} else {
16+
nums1[k] = nums2[j]
17+
j--
18+
}
19+
}
20+
}
21+
22+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMerge(t *testing.T) {
8+
m, n := 3, 3
9+
nums1 := []int{1, 2, 3, 0, 0, 0}
10+
nums2 := []int{2, 5, 6}
11+
ret := []int{1, 2, 2, 3, 5, 6}
12+
merge(nums1, m, nums2, n)
13+
for k, v := range ret {
14+
if nums1[k] != v {
15+
t.Fatalf("case fails: %v\n", nums1)
16+
}
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# [125.验证回文串](https://leetcode-cn.com/problems/valid-palindrome)
2+
3+
4+
### 题目描述
5+
6+
<div class="notranslate"><p>给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。</p>
7+
8+
<p><strong>说明:</strong>本题中,我们将空字符串定义为有效的回文串。</p>
9+
10+
<p><strong>示例 1:</strong></p>
11+
12+
<pre><strong>输入:</strong> "A man, a plan, a canal: Panama"
13+
<strong>输出:</strong> true
14+
</pre>
15+
16+
<p><strong>示例 2:</strong></p>
17+
18+
<pre><strong>输入:</strong> "race a car"
19+
<strong>输出:</strong> false
20+
</pre>
21+
</div>
22+
23+
### 解题思路
24+
25+
![](http://lc-photo.xwlin.com/125.png)
26+
27+
### 代码实现
28+
29+
<!-- tabs:start -->
30+
31+
#### **Golang**
32+
```go
33+
34+
import (
35+
"strings"
36+
)
37+
38+
func isPalindrome(s string) bool {
39+
s = strings.ToLower(s)
40+
l, r := 0, len(s)-1
41+
for l < r {
42+
for l < r && (s[l] < 'a' || s[l] > 'z') && (s[l] < '0' || s[l] > '9') {
43+
l++
44+
}
45+
for l < r && (s[r] < 'a' || s[r] > 'z') && (s[r] < '0' || s[r] > '9') {
46+
r--
47+
}
48+
if l < r {
49+
if s[l] != s[r] {
50+
return false
51+
}
52+
l++
53+
r--
54+
}
55+
}
56+
return true
57+
}
58+
```
59+
60+
61+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package leetcode
2+
3+
import (
4+
"strings"
5+
)
6+
7+
/*
8+
* @lc app=leetcode.cn id=125 lang=golang
9+
*
10+
* [125] 验证回文串
11+
*/
12+
13+
// @lc code=start
14+
func isPalindrome(s string) bool {
15+
s = strings.ToLower(s)
16+
l, r := 0, len(s)-1
17+
for l < r {
18+
for l < r && (s[l] < 'a' || s[l] > 'z') && (s[l] < '0' || s[l] > '9') {
19+
l++
20+
}
21+
for l < r && (s[r] < 'a' || s[r] > 'z') && (s[r] < '0' || s[r] > '9') {
22+
r--
23+
}
24+
if l < r {
25+
if s[l] != s[r] {
26+
return false
27+
}
28+
l++
29+
r--
30+
}
31+
}
32+
return true
33+
}
34+
35+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestIsPalindrome(t *testing.T) {
9+
10+
var x string
11+
var ret bool
12+
13+
ret = false
14+
x = "race a car"
15+
fmt.Printf("x = %v ret = %v\n", x, isPalindrome(x))
16+
if isPalindrome(x) != ret {
17+
t.Fatalf("case fails: %v\n", ret)
18+
}
19+
20+
ret = true
21+
x = ".,"
22+
fmt.Printf("x = %v ret = %v\n", x, isPalindrome(x))
23+
if isPalindrome(x) != ret {
24+
t.Fatalf("case fails: %v\n", ret)
25+
}
26+
27+
ret = true
28+
x = "A man, a plan, a canal: Panama"
29+
fmt.Printf("x = %v ret = %v\n", x, isPalindrome(x))
30+
if isPalindrome(x) != ret {
31+
t.Fatalf("case fails: %v\n", ret)
32+
}
33+
34+
ret = true
35+
x = "......a....."
36+
fmt.Printf("x = %v ret = %v\n", x, isPalindrome(x))
37+
if isPalindrome(x) != ret {
38+
t.Fatalf("case fails: %v\n", ret)
39+
}
40+
41+
ret = false
42+
x = "a.b,."
43+
fmt.Printf("x = %v ret = %v\n", x, isPalindrome(x))
44+
if isPalindrome(x) != ret {
45+
t.Fatalf("case fails: %v\n", ret)
46+
}
47+
}

0 commit comments

Comments
 (0)