@@ -51,6 +51,16 @@ target = 2 是紧跟着 key 之后出现次数最多的数字,所以我们返
51
51
52
52
<!-- 这里可写通用的实现逻辑 -->
53
53
54
+ ** 方法一:遍历计数**
55
+
56
+ 我们用一个哈希表或数组 $cnt$ 记录每个 $target$ 出现的次数,用一个变量 $mx$ 维护 $target$ 出现的最大次数,初始时 $mx = 0$。
57
+
58
+ 遍历数组 $nums$,如果 $nums[ i] = key$,则 $nums[ i + 1] $ 出现的次数 $cnt[ nums[ i + 1]] $ 加一,如果此时 $mx \lt cnt[ nums[ i + 1]] $,则更新 $mx = cnt[ nums[ i + 1]] $,并更新答案 $ans = nums[ i + 1] $。
59
+
60
+ 遍历结束后,返回答案 $ans$。
61
+
62
+ 时间复杂度 $O(n)$,空间复杂度 $O(M)$。其中 $n$ 和 $M$ 分别为数组 $nums$ 的长度和数组 $nums$ 中元素的最大值。
63
+
54
64
<!-- tabs:start -->
55
65
56
66
### ** Python3**
@@ -61,14 +71,13 @@ target = 2 是紧跟着 key 之后出现次数最多的数字,所以我们返
61
71
class Solution :
62
72
def mostFrequent (self , nums : List[int ], key : int ) -> int :
63
73
cnt = Counter()
64
- mx = ans = 0
65
- for i, v in enumerate (nums[:- 1 ]):
66
- if v == key:
67
- target = nums[i + 1 ]
68
- cnt[target] += 1
69
- if mx < cnt[target]:
70
- mx = cnt[target]
71
- ans = nums[i + 1 ]
74
+ ans = mx = 0
75
+ for a, b in pairwise(nums):
76
+ if a == key:
77
+ cnt[b] += 1
78
+ if mx < cnt[b]:
79
+ mx = cnt[b]
80
+ ans = b
72
81
return ans
73
82
```
74
83
@@ -79,14 +88,12 @@ class Solution:
79
88
``` java
80
89
class Solution {
81
90
public int mostFrequent (int [] nums , int key ) {
82
- int [] cnt = new int [1010 ];
83
- int mx = 0 , ans = 0 ;
91
+ int [] cnt = new int [1001 ];
92
+ int ans = 0 , mx = 0 ;
84
93
for (int i = 0 ; i < nums. length - 1 ; ++ i) {
85
94
if (nums[i] == key) {
86
- int target = nums[i + 1 ];
87
- ++ cnt[target];
88
- if (mx < cnt[target]) {
89
- mx = cnt[target];
95
+ if (mx < ++ cnt[nums[i + 1 ]]) {
96
+ mx = cnt[nums[i + 1 ]];
90
97
ans = nums[i + 1 ];
91
98
}
92
99
}
@@ -102,14 +109,12 @@ class Solution {
102
109
class Solution {
103
110
public:
104
111
int mostFrequent(vector<int >& nums, int key) {
105
- vector< int > cnt(1010) ;
106
- int mx = 0, ans = 0;
112
+ int cnt[ 1001 ] {} ;
113
+ int ans = 0, mx = 0;
107
114
for (int i = 0; i < nums.size() - 1; ++i) {
108
115
if (nums[ i] == key) {
109
- int target = nums[ i + 1] ;
110
- ++cnt[ target] ;
111
- if (mx < cnt[ target] ) {
112
- mx = cnt[ target] ;
116
+ if (mx < ++cnt[ nums[ i + 1]] ) {
117
+ mx = cnt[ nums[ i + 1]] ;
113
118
ans = nums[ i + 1] ;
114
119
}
115
120
}
@@ -122,27 +127,39 @@ public:
122
127
### **Go**
123
128
124
129
```go
125
- func mostFrequent(nums []int, key int) int {
126
- cnt := make([]int, 1010)
127
- mx, ans := 0, 0
128
- for i, v := range nums[:len(nums)-1] {
129
- if v == key {
130
- target := nums[i+1]
131
- cnt[target]++
132
- if mx < cnt[target] {
133
- mx = cnt[target]
134
- ans = nums[i+1]
130
+ func mostFrequent(nums []int, key int) (ans int) {
131
+ cnt := [1001]int{}
132
+ mx := 0
133
+ for i, x := range nums[1:] {
134
+ if nums[i] == key {
135
+ cnt[x]++
136
+ if mx < cnt[x] {
137
+ mx = cnt[x]
138
+ ans = x
135
139
}
136
140
}
137
141
}
138
- return ans
142
+ return
139
143
}
140
144
```
141
145
142
146
### ** TypeScript**
143
147
144
148
``` ts
145
-
149
+ function mostFrequent(nums : number [], key : number ): number {
150
+ const cnt: number [] = new Array (1001 ).fill (0 );
151
+ let ans = 0 ;
152
+ let mx = 0 ;
153
+ for (let i = 0 ; i < nums .length - 1 ; ++ i ) {
154
+ if (nums [i ] === key ) {
155
+ if (mx < ++ cnt [nums [i + 1 ]]) {
156
+ mx = cnt [nums [i + 1 ]];
157
+ ans = nums [i + 1 ];
158
+ }
159
+ }
160
+ }
161
+ return ans ;
162
+ }
146
163
```
147
164
148
165
### ** PHP**
0 commit comments