41
41
42
42
<!-- 这里可写通用的实现逻辑 -->
43
43
44
- 计数器实现。
44
+ ** 方法一:计数 + 倒序遍历**
45
+
46
+ 注意到题目的数据范围,我们可以使用一个长度为 $1001$ 的数组来统计每个数字出现的次数,然后倒序遍历数组,找到第一个出现次数为 $1$ 的数字即可。如果没有找到,则返回 $-1$。
47
+
48
+ 时间复杂度 $O(n + M)$,空间复杂度 $O(M)$。其中 $n$ 为数组长度;而 $M$ 为数组中出现的最大数字,本题中 $M \leq 1000$。
45
49
46
50
<!-- tabs:start -->
47
51
51
55
52
56
``` python
53
57
class Solution :
54
- def largestUniqueNumber (self , A : List[int ]) -> int :
55
- counter = Counter(A)
56
- for i in range (1000 , - 1 , - 1 ):
57
- if counter[i] == 1 :
58
- return i
59
- return - 1
58
+ def largestUniqueNumber (self , nums : List[int ]) -> int :
59
+ cnt = Counter(nums)
60
+ return next ((x for x in range (1000 , - 1 , - 1 ) if cnt[x] == 1 ), - 1 )
61
+ ```
62
+
63
+ ``` python
64
+ class Solution :
65
+ def largestUniqueNumber (self , nums : List[int ]) -> int :
66
+ cnt = Counter(nums)
67
+ return max ((x for x, v in cnt.items() if v == 1 ), default = - 1 )
60
68
```
61
69
62
70
### ** Java**
@@ -65,42 +73,96 @@ class Solution:
65
73
66
74
``` java
67
75
class Solution {
68
- public int largestUniqueNumber (int [] A ) {
69
- int [] counter = new int [1001 ];
70
- for (int a : A ) {
71
- ++ counter[a ];
76
+ public int largestUniqueNumber (int [] nums ) {
77
+ int [] cnt = new int [1001 ];
78
+ for (int x : nums ) {
79
+ ++ cnt[x ];
72
80
}
73
- for (int i = 1000 ; i >= 0 ; -- i ) {
74
- if (counter[i ] == 1 ) {
75
- return i ;
81
+ for (int x = 1000 ; x >= 0 ; -- x ) {
82
+ if (cnt[x ] == 1 ) {
83
+ return x ;
76
84
}
77
85
}
78
86
return - 1 ;
79
87
}
80
88
}
81
89
```
82
90
91
+ ### ** C++**
92
+
93
+ ``` cpp
94
+ class Solution {
95
+ public:
96
+ int largestUniqueNumber(vector<int >& nums) {
97
+ int cnt[ 1001] {};
98
+ for (int& x : nums) {
99
+ ++cnt[ x] ;
100
+ }
101
+ for (int x = 1000; ~ x; --x) {
102
+ if (cnt[ x] == 1) {
103
+ return x;
104
+ }
105
+ }
106
+ return -1;
107
+ }
108
+ };
109
+ ```
110
+
111
+ ### **Go**
112
+
113
+ ```go
114
+ func largestUniqueNumber(nums []int) int {
115
+ cnt := [1001]int{}
116
+ for _, x := range nums {
117
+ cnt[x]++
118
+ }
119
+ for x := 1000; x >= 0; x-- {
120
+ if cnt[x] == 1 {
121
+ return x
122
+ }
123
+ }
124
+ return -1
125
+ }
126
+ ```
127
+
83
128
### ** JavaScript**
84
129
85
130
``` js
86
131
/**
87
- * @param {number[]} A
132
+ * @param {number[]} nums
88
133
* @return {number}
89
134
*/
90
- var largestUniqueNumber = function (A ) {
91
- let counter = {} ;
92
- for (const a of A ) {
93
- counter[a] = (counter[a] || 0 ) + 1 ;
135
+ var largestUniqueNumber = function (nums ) {
136
+ const cnt = new Array ( 1001 ). fill ( 0 ) ;
137
+ for (const x of nums ) {
138
+ ++ cnt[x] ;
94
139
}
95
- for (let i = 1000 ; i >= 0 ; -- i ) {
96
- if (counter[i ] == 1 ) {
97
- return i ;
140
+ for (let x = 1000 ; x >= 0 ; -- x ) {
141
+ if (cnt[x ] == 1 ) {
142
+ return x ;
98
143
}
99
144
}
100
145
return - 1 ;
101
146
};
102
147
```
103
148
149
+ ### ** TypeScript**
150
+
151
+ ``` ts
152
+ function largestUniqueNumber(nums : number []): number {
153
+ const cnt = new Array (1001 ).fill (0 );
154
+ for (const x of nums ) {
155
+ ++ cnt [x ];
156
+ }
157
+ for (let x = 1000 ; x >= 0 ; -- x ) {
158
+ if (cnt [x ] == 1 ) {
159
+ return x ;
160
+ }
161
+ }
162
+ return - 1 ;
163
+ }
164
+ ```
165
+
104
166
### ** ...**
105
167
106
168
```
0 commit comments