Skip to content

Commit 307b92d

Browse files
committed
commit solution 344 345
1 parent 90e540a commit 307b92d

File tree

10 files changed

+212
-6
lines changed

10 files changed

+212
-6
lines changed

_sidebar.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@
349349
- [341. 扁平化嵌套列表迭代器](solution/300-399/0341.flatten-nested-list-iterator/)
350350
- [342. 4的幂](solution/300-399/0342.power-of-four/)
351351
- [343. 整数拆分](solution/300-399/0343.integer-break/)
352-
- [344. 反转字符串](solution/300-399/0344.reverse-string/)
353-
- [345. 反转字符串中的元音字母](solution/300-399/0345.reverse-vowels-of-a-string/)
352+
- [344. 反转字符串](solution/300-399/0344.reverse-string/)
353+
- [345. 反转字符串中的元音字母](solution/300-399/0345.reverse-vowels-of-a-string/)
354354
- [346. 数据流中的移动平均值](solution/300-399/0346.moving-average-from-data-stream/)
355355
- [347. 前 k 个高频元素](solution/300-399/0347.top-k-frequent-elements/)
356356
- [348. 判定井字棋胜负](solution/300-399/0348.design-tic-tac-toe/)

index-tags.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
| [20](https://leetcode-cn.com/problems/valid-parentheses) | [有效的括号](/solution/1-99/0020.valid-parentheses/) | ``,`字符串` | <font color=green>简单</font> ||
3131
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> ||
3232
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
33+
| [344](https://leetcode-cn.com/problems/reverse-string) | [反转字符串](/solution/300-399/0344.reverse-string/) | `双指针`,`字符串` | <font color=green>简单</font> ||
34+
| [345](https://leetcode-cn.com/problems/reverse-vowels-of-a-string) | [反转字符串中的元音字母](/solution/300-399/0345.reverse-vowels-of-a-string/) | `双指针`,`字符串` | <font color=green>简单</font> ||
3335

3436
#### **数学**
3537

@@ -68,7 +70,8 @@
6870
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
6971
| [167](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted) | [两数之和 ii - 输入有序数组](/solution/100-199/0167.two-sum-ii---input-array-is-sorted/) | `数组`,`双指针`,`二分查找` | <font color=green>简单</font> ||
7072
| [283](https://leetcode-cn.com/problems/move-zeroes) | [移动零](/solution/200-299/0283.move-zeroes/) | `数组`,`双指针` | <font color=green>简单</font> ||
71-
73+
| [344](https://leetcode-cn.com/problems/reverse-string) | [反转字符串](/solution/300-399/0344.reverse-string/) | `双指针`,`字符串` | <font color=green>简单</font> ||
74+
| [345](https://leetcode-cn.com/problems/reverse-vowels-of-a-string) | [反转字符串中的元音字母](/solution/300-399/0345.reverse-vowels-of-a-string/) | `双指针`,`字符串` | <font color=green>简单</font> ||
7275

7376
#### **链表**
7477

index-type.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@
100100
| [326](https://leetcode-cn.com/problems/power-of-three) | [3的幂](/solution/300-399/0326.power-of-three/) | `数学` | <font color=green>简单</font> |
101101
| [339](https://leetcode-cn.com/problems/nested-list-weight-sum) | [嵌套列表权重和](/solution/300-399/0339.nested-list-weight-sum/) | `深度优先搜索` | <font color=green>简单</font> |
102102
| [342](https://leetcode-cn.com/problems/power-of-four) | [4的幂](/solution/300-399/0342.power-of-four/) | `位运算` | <font color=green>简单</font> |
103-
| [344](https://leetcode-cn.com/problems/reverse-string) | [反转字符串](/solution/300-399/0344.reverse-string/) | `双指针`,`字符串` | <font color=green>简单</font> |
104-
| [345](https://leetcode-cn.com/problems/reverse-vowels-of-a-string) | [反转字符串中的元音字母](/solution/300-399/0345.reverse-vowels-of-a-string/) | `双指针`,`字符串` | <font color=green>简单</font> |
103+
| [344](https://leetcode-cn.com/problems/reverse-string) | [反转字符串](/solution/300-399/0344.reverse-string/) | `双指针`,`字符串` | <font color=green>简单</font> ||
104+
| [345](https://leetcode-cn.com/problems/reverse-vowels-of-a-string) | [反转字符串中的元音字母](/solution/300-399/0345.reverse-vowels-of-a-string/) | `双指针`,`字符串` | <font color=green>简单</font> ||
105105
| [346](https://leetcode-cn.com/problems/moving-average-from-data-stream) | [数据流中的移动平均值](/solution/300-399/0346.moving-average-from-data-stream/) | `设计`,`队列` | <font color=green>简单</font> |
106106
| [349](https://leetcode-cn.com/problems/intersection-of-two-arrays) | [两个数组的交集](/solution/300-399/0349.intersection-of-two-arrays/) | `排序`,`哈希表`,`双指针`,`二分查找` | <font color=green>简单</font> |
107107
| [350](https://leetcode-cn.com/problems/intersection-of-two-arrays-ii) | [两个数组的交集 ii](/solution/300-399/0350.intersection-of-two-arrays-ii/) | `排序`,`哈希表`,`双指针`,`二分查找` | <font color=green>简单</font> |

solution/200-299/0268.missing-number/solution.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package leetcode
99
// @lc code=start
1010
func missingNumber(nums []int) int {
1111
sum1, sum2 := len(nums), 0
12-
for i, _ := range nums {
12+
for i := range nums {
1313
sum1 += i
1414
sum2 += nums[i]
1515
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# [344. 反转字符串](https://leetcode-cn.com/problems/reverse-string/description/)
2+
3+
4+
### 题目描述
5+
6+
<p>编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 <code>char[]</code> 的形式给出。</p>
7+
8+
<p>不要给另外的数组分配额外的空间,你必须<strong><a href="https://baike.baidu.com/item/原地算法" target="_blank">原地</a>修改输入数组</strong>、使用 O(1) 的额外空间解决这一问题。</p>
9+
10+
<p>你可以假设数组中的所有字符都是 <a href="https://baike.baidu.com/item/ASCII" target="_blank">ASCII</a> 码表中的可打印字符。</p>
11+
12+
<p>&nbsp;</p>
13+
14+
<p><strong>示例 1:</strong></p>
15+
16+
<pre><strong>输入:</strong>[&quot;h&quot;,&quot;e&quot;,&quot;l&quot;,&quot;l&quot;,&quot;o&quot;]
17+
<strong>输出:</strong>[&quot;o&quot;,&quot;l&quot;,&quot;l&quot;,&quot;e&quot;,&quot;h&quot;]
18+
</pre>
19+
20+
<p><strong>示例 2:</strong></p>
21+
22+
<pre><strong>输入:</strong>[&quot;H&quot;,&quot;a&quot;,&quot;n&quot;,&quot;n&quot;,&quot;a&quot;,&quot;h&quot;]
23+
<strong>输出:</strong>[&quot;h&quot;,&quot;a&quot;,&quot;n&quot;,&quot;n&quot;,&quot;a&quot;,&quot;H&quot;]</pre>
24+
25+
### 解题思路
26+
27+
1. 双指针操作
28+
29+
### 代码实现
30+
31+
<!-- tabs:start -->
32+
33+
#### **Golang**
34+
```go
35+
func reverseString(s []byte) {
36+
p, q := 0, len(s)-1
37+
for p < q {
38+
s[p], s[q] = s[q], s[p]
39+
p++
40+
q--
41+
}
42+
}
43+
```
44+
45+
46+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=344 lang=golang
5+
*
6+
* [344] 反转字符串
7+
*/
8+
9+
// @lc code=start
10+
func reverseString(s []byte) {
11+
p, q := 0, len(s)-1
12+
for p < q {
13+
s[p], s[q] = s[q], s[p]
14+
p++
15+
q--
16+
}
17+
}
18+
19+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestReverseString(t *testing.T) {
8+
var ret []byte
9+
var s []byte
10+
11+
s = []byte("hello")
12+
ret = []byte("olleh")
13+
reverseString(s)
14+
for i, v := range s {
15+
if v != ret[i] {
16+
t.Fatalf("case fails %v\n", ret)
17+
}
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# [345. 反转字符串中的元音字母](https://leetcode-cn.com/problems/reverse-vowels-of-a-string/description/)
2+
3+
4+
### 题目描述
5+
6+
<p>编写一个函数,以字符串作为输入,反转该字符串中的元音字母。</p>
7+
8+
<p><strong>示例 1:</strong></p>
9+
10+
<pre><strong>输入: </strong>&quot;hello&quot;
11+
<strong>输出: </strong>&quot;holle&quot;
12+
</pre>
13+
14+
<p><strong>示例 2:</strong></p>
15+
16+
<pre><strong>输入: </strong>&quot;leetcode&quot;
17+
<strong>输出: </strong>&quot;leotcede&quot;</pre>
18+
19+
<p><strong>说明:</strong><br>
20+
元音字母不包含字母&quot;y&quot;。</p>
21+
22+
### 解题思路
23+
24+
1. 双指针操作
25+
26+
### 代码实现
27+
28+
<!-- tabs:start -->
29+
30+
#### **Golang**
31+
```go
32+
func reverseVowels(s string) string {
33+
vowel := map[rune]bool{
34+
'a': true,
35+
'e': true,
36+
'i': true,
37+
'o': true,
38+
'u': true,
39+
}
40+
p, q := 0, len(s)-1
41+
ret := []byte(s)
42+
for p < q {
43+
_, ok1 := vowel[unicode.ToLower(rune(ret[p]))]
44+
if !ok1 {
45+
p++
46+
}
47+
_, ok2 := vowel[unicode.ToLower(rune(ret[q]))]
48+
if !ok2 {
49+
q--
50+
}
51+
if ok1 && ok2 {
52+
ret[p], ret[q] = ret[q], ret[p]
53+
p++
54+
q--
55+
}
56+
}
57+
return string(ret)
58+
}
59+
```
60+
61+
62+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package leetcode
2+
3+
import "unicode"
4+
5+
/*
6+
* @lc app=leetcode.cn id=345 lang=golang
7+
*
8+
* [345] 反转字符串中的元音字母
9+
*/
10+
11+
// @lc code=start
12+
func reverseVowels(s string) string {
13+
vowel := map[rune]bool{
14+
'a': true,
15+
'e': true,
16+
'i': true,
17+
'o': true,
18+
'u': true,
19+
}
20+
p, q := 0, len(s)-1
21+
ret := []byte(s)
22+
for p < q {
23+
_, ok1 := vowel[unicode.ToLower(rune(ret[p]))]
24+
if !ok1 {
25+
p++
26+
}
27+
_, ok2 := vowel[unicode.ToLower(rune(ret[q]))]
28+
if !ok2 {
29+
q--
30+
}
31+
if ok1 && ok2 {
32+
ret[p], ret[q] = ret[q], ret[p]
33+
p++
34+
q--
35+
}
36+
}
37+
return string(ret)
38+
}
39+
40+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestReverseVowels(t *testing.T) {
8+
var ret string
9+
var s string
10+
11+
s = "hello"
12+
ret = "holle"
13+
if ret != reverseVowels(s) {
14+
t.Fatalf("case fails %v\n", ret)
15+
}
16+
17+
}

0 commit comments

Comments
 (0)