Skip to content

Commit 119dccc

Browse files
committed
feat: commit solution 169
1 parent dc268b3 commit 119dccc

File tree

6 files changed

+141
-17
lines changed

6 files changed

+141
-17
lines changed

_sidebar.md

+17-16
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,24 @@
77
- [贡献者列表](contributor.md)
88

99
- 📚  题目列表
10-
- [1.两数之和 ✅](solution/1-99/0001.two-sum/)
10+
- [1.两数之和](solution/1-99/0001.two-sum/)
1111
- [2.两数相加](solution/1-99/0002.add-two-numbers/)
12-
- [7.整数反转 ✅](solution/1-99/0007.reverse-integer/)
13-
- [9.回文数 ✅](solution/1-99/0009.palindrome-number/)
14-
- [13.罗马数字转整数 ✅](solution/1-99/0013.roman-to-integer/)
15-
- [20.有效的括号 ✅](solution/1-99/0020.valid-parentheses/)
16-
- [21.合并两个有序链表 ✅](solution/1-99/0021.merge-two-sorted-lists/)
17-
- [26.删除排序数组中的重复项 ✅](solution/1-99/0026.remove-duplicates-from-sorted-array/)
18-
- [27.移除元素 ✅](solution/1-99/0027.remove-element/)
19-
- [28.实现 strStr() ✅](solution/1-99/0028.implement-strstr/)
20-
- [35.搜索插入位置 ✅](solution/1-99/0035.search-insert-position/)
21-
- [66.加一 ✅](solution/1-99/0066.plus-one/)
22-
- [83.删除排序链表中的重复元素 ✅](solution/1-99/0083.remove-duplicates-from-sorted-list/)
23-
- [88.合并两个有序数组 ✅](solution/1-99/0088.merge-sorted-array/)
24-
- [125.验证回文串 ✅](solution/100-199/0125.valid-palindrome/)
25-
- [217.存在重复元素 ✅](solution/200-299/0217.contains-duplicate/)
26-
- [219.存在重复元素 II ✅](solution/200-299/0219.contains-duplicate-ii/)
12+
- [7.整数反转](solution/1-99/0007.reverse-integer/)
13+
- [9.回文数](solution/1-99/0009.palindrome-number/)
14+
- [13.罗马数字转整数](solution/1-99/0013.roman-to-integer/)
15+
- [20.有效的括号](solution/1-99/0020.valid-parentheses/)
16+
- [21.合并两个有序链表](solution/1-99/0021.merge-two-sorted-lists/)
17+
- [26.删除排序数组中的重复项](solution/1-99/0026.remove-duplicates-from-sorted-array/)
18+
- [27.移除元素](solution/1-99/0027.remove-element/)
19+
- [28.实现 strStr()](solution/1-99/0028.implement-strstr/)
20+
- [35.搜索插入位置](solution/1-99/0035.search-insert-position/)
21+
- [66.加一](solution/1-99/0066.plus-one/)
22+
- [83.删除排序链表中的重复元素](solution/1-99/0083.remove-duplicates-from-sorted-list/)
23+
- [88.合并两个有序数组](solution/1-99/0088.merge-sorted-array/)
24+
- [125.验证回文串](solution/100-199/0125.valid-palindrome/)
25+
- [169.多数元素](solution/100-199/0169.majority-element/)
26+
- [217.存在重复元素](solution/200-299/0217.contains-duplicate/)
27+
- [219.存在重复元素 II](solution/200-299/0219.contains-duplicate-ii/)
2728

2829

2930

index-tags.md

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
3333
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
3434
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||
35+
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
3536

3637
#### **哈希表**
3738

@@ -79,6 +80,10 @@
7980

8081
#### **位运算**
8182

83+
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
84+
| --- | --- | --- | --- | --- |
85+
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
86+
8287
#### ****
8388

8489
#### **递归**
@@ -95,6 +100,10 @@
95100

96101
#### **分治算法**
97102

103+
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
104+
| --- | --- | --- | --- | --- |
105+
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
106+
98107
#### **回溯算法**
99108

100109
#### **脑筋急转弯**

index-type.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
| [160](https://leetcode-cn.com/problems/intersection-of-two-linked-lists) | [相交链表](/solution/100-199/0160.intersection-of-two-linked-lists/) | `链表` | <font color=green>简单</font> |
4848
| [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> |
4949
| [168](https://leetcode-cn.com/problems/excel-sheet-column-title) | [excel表列名称](/solution/100-199/0168.excel-sheet-column-title/) | `数学` | <font color=green>简单</font> |
50-
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> |
50+
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
5151
| [170](https://leetcode-cn.com/problems/two-sum-iii-data-structure-design) | [两数之和 iii - 数据结构设计](/solution/100-199/0170.two-sum-iii---data-structure-design/) | `设计`,`哈希表` | <font color=green>简单</font> |
5252
| [171](https://leetcode-cn.com/problems/excel-sheet-column-number) | [excel表列序号](/solution/100-199/0171.excel-sheet-column-number/) | `数学` | <font color=green>简单</font> |
5353
| [172](https://leetcode-cn.com/problems/factorial-trailing-zeroes) | [阶乘后的零](/solution/100-199/0172.factorial-trailing-zeroes/) | `数学` | <font color=green>简单</font> |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# [169.多数元素](https://leetcode-cn.com/problems/majority-element/description/)
2+
3+
4+
### 题目描述
5+
6+
<div class="notranslate"><p>给定一个大小为 <em>n </em>的数组,找到其中的多数元素。多数元素是指在数组中出现次数<strong>大于</strong>&nbsp;<code>⌊ n/2 ⌋</code>&nbsp;的元素。</p>
7+
8+
<p>你可以假设数组是非空的,并且给定的数组总是存在多数元素。</p>
9+
10+
<p>&nbsp;</p>
11+
12+
<p><strong>示例&nbsp;1:</strong></p>
13+
14+
<pre><strong>输入:</strong> [3,2,3]
15+
<strong>输出:</strong> 3</pre>
16+
17+
<p><strong>示例&nbsp;2:</strong></p>
18+
19+
<pre><strong>输入:</strong> [2,2,1,1,1,2,2]
20+
<strong>输出:</strong> 2
21+
</pre>
22+
</div>
23+
24+
### 解题思路
25+
26+
1. 利用hash表来实现
27+
28+
### 代码实现
29+
30+
<!-- tabs:start -->
31+
32+
#### **Golang**
33+
```go
34+
func majorityElement(nums []int) int {
35+
l := len(nums)
36+
if l == 0 {
37+
return 0
38+
}
39+
n := l/2 + l%2 // l%2为额外加个奇偶数的判断
40+
numMap := make(map[int]int)
41+
for _, v := range nums {
42+
if _, ok := numMap[v]; ok {
43+
numMap[v]++
44+
} else {
45+
numMap[v] = 1
46+
}
47+
if numMap[v] >= n {
48+
return v
49+
}
50+
}
51+
return -1
52+
}
53+
```
54+
55+
56+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=169 lang=golang
5+
*
6+
* [169] 多数元素
7+
*/
8+
9+
// @lc code=start
10+
func majorityElement(nums []int) int {
11+
l := len(nums)
12+
if l == 0 {
13+
return 0
14+
}
15+
n := l/2 + l%2 // l%2为额外加个奇偶数的判断
16+
numMap := make(map[int]int)
17+
for _, v := range nums {
18+
if _, ok := numMap[v]; ok {
19+
numMap[v]++
20+
} else {
21+
numMap[v] = 1
22+
}
23+
if numMap[v] >= n {
24+
return v
25+
}
26+
}
27+
return -1
28+
}
29+
30+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMajorityElement(t *testing.T) {
8+
var nums []int
9+
var ret int
10+
11+
nums = []int{3, 2, 3}
12+
ret = 3
13+
if ret != majorityElement(nums) {
14+
t.Fatalf("case fails: %v\n", ret)
15+
}
16+
17+
nums = []int{2, 2, 1, 1, 1, 2, 2}
18+
ret = 2
19+
if ret != majorityElement(nums) {
20+
t.Fatalf("case fails: %v\n", ret)
21+
}
22+
23+
nums = []int{}
24+
ret = 0
25+
if ret != majorityElement(nums) {
26+
t.Fatalf("case fails: %v\n", ret)
27+
}
28+
}

0 commit comments

Comments
 (0)