Skip to content

Commit c2e0c1d

Browse files
committed
feat: add solutions to lc problem: No.1133
No.1133.Largest Unique Number
1 parent 131c30e commit c2e0c1d

File tree

8 files changed

+220
-64
lines changed

8 files changed

+220
-64
lines changed

solution/1100-1199/1133.Largest Unique Number/README.md

+84-22
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44-
计数器实现。
44+
**方法一:计数 + 倒序遍历**
45+
46+
注意到题目的数据范围,我们可以使用一个长度为 $1001$ 的数组来统计每个数字出现的次数,然后倒序遍历数组,找到第一个出现次数为 $1$ 的数字即可。如果没有找到,则返回 $-1$。
47+
48+
时间复杂度 $O(n + M)$,空间复杂度 $O(M)$。其中 $n$ 为数组长度;而 $M$ 为数组中出现的最大数字,本题中 $M \leq 1000$。
4549

4650
<!-- tabs:start -->
4751

@@ -51,12 +55,16 @@
5155

5256
```python
5357
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)
6068
```
6169

6270
### **Java**
@@ -65,42 +73,96 @@ class Solution:
6573

6674
```java
6775
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];
7280
}
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;
7684
}
7785
}
7886
return -1;
7987
}
8088
}
8189
```
8290

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+
83128
### **JavaScript**
84129

85130
```js
86131
/**
87-
* @param {number[]} A
132+
* @param {number[]} nums
88133
* @return {number}
89134
*/
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];
94139
}
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;
98143
}
99144
}
100145
return -1;
101146
};
102147
```
103148

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+
104166
### **...**
105167

106168
```

solution/1100-1199/1133.Largest Unique Number/README_EN.md

+79-21
Original file line numberDiff line numberDiff line change
@@ -38,54 +38,112 @@
3838

3939
```python
4040
class Solution:
41-
def largestUniqueNumber(self, A: List[int]) -> int:
42-
counter = Counter(A)
43-
for i in range(1000, -1, -1):
44-
if counter[i] == 1:
45-
return i
46-
return -1
41+
def largestUniqueNumber(self, nums: List[int]) -> int:
42+
cnt = Counter(nums)
43+
return next((x for x in range(1000, -1, -1) if cnt[x] == 1), -1)
44+
```
45+
46+
```python
47+
class Solution:
48+
def largestUniqueNumber(self, nums: List[int]) -> int:
49+
cnt = Counter(nums)
50+
return max((x for x, v in cnt.items() if v == 1), default=-1)
4751
```
4852

4953
### **Java**
5054

5155
```java
5256
class Solution {
53-
public int largestUniqueNumber(int[] A) {
54-
int[] counter = new int[1001];
55-
for (int a : A) {
56-
++counter[a];
57+
public int largestUniqueNumber(int[] nums) {
58+
int[] cnt = new int[1001];
59+
for (int x : nums) {
60+
++cnt[x];
5761
}
58-
for (int i = 1000; i >= 0; --i) {
59-
if (counter[i] == 1) {
60-
return i;
62+
for (int x = 1000; x >= 0; --x) {
63+
if (cnt[x] == 1) {
64+
return x;
6165
}
6266
}
6367
return -1;
6468
}
6569
}
6670
```
6771

72+
### **C++**
73+
74+
```cpp
75+
class Solution {
76+
public:
77+
int largestUniqueNumber(vector<int>& nums) {
78+
int cnt[1001]{};
79+
for (int& x : nums) {
80+
++cnt[x];
81+
}
82+
for (int x = 1000; ~x; --x) {
83+
if (cnt[x] == 1) {
84+
return x;
85+
}
86+
}
87+
return -1;
88+
}
89+
};
90+
```
91+
92+
### **Go**
93+
94+
```go
95+
func largestUniqueNumber(nums []int) int {
96+
cnt := [1001]int{}
97+
for _, x := range nums {
98+
cnt[x]++
99+
}
100+
for x := 1000; x >= 0; x-- {
101+
if cnt[x] == 1 {
102+
return x
103+
}
104+
}
105+
return -1
106+
}
107+
```
108+
68109
### **JavaScript**
69110

70111
```js
71112
/**
72-
* @param {number[]} A
113+
* @param {number[]} nums
73114
* @return {number}
74115
*/
75-
var largestUniqueNumber = function (A) {
76-
let counter = {};
77-
for (const a of A) {
78-
counter[a] = (counter[a] || 0) + 1;
116+
var largestUniqueNumber = function (nums) {
117+
const cnt = new Array(1001).fill(0);
118+
for (const x of nums) {
119+
++cnt[x];
79120
}
80-
for (let i = 1000; i >= 0; --i) {
81-
if (counter[i] == 1) {
82-
return i;
121+
for (let x = 1000; x >= 0; --x) {
122+
if (cnt[x] == 1) {
123+
return x;
83124
}
84125
}
85126
return -1;
86127
};
87128
```
88129

130+
### **TypeScript**
131+
132+
```ts
133+
function largestUniqueNumber(nums: number[]): number {
134+
const cnt = new Array(1001).fill(0);
135+
for (const x of nums) {
136+
++cnt[x];
137+
}
138+
for (let x = 1000; x >= 0; --x) {
139+
if (cnt[x] == 1) {
140+
return x;
141+
}
142+
}
143+
return -1;
144+
}
145+
```
146+
89147
### **...**
90148

91149
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int largestUniqueNumber(vector<int>& nums) {
4+
int cnt[1001]{};
5+
for (int& x : nums) {
6+
++cnt[x];
7+
}
8+
for (int x = 1000; ~x; --x) {
9+
if (cnt[x] == 1) {
10+
return x;
11+
}
12+
}
13+
return -1;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func largestUniqueNumber(nums []int) int {
2+
cnt := [1001]int{}
3+
for _, x := range nums {
4+
cnt[x]++
5+
}
6+
for x := 1000; x >= 0; x-- {
7+
if cnt[x] == 1 {
8+
return x
9+
}
10+
}
11+
return -1
12+
}

solution/1100-1199/1133.Largest Unique Number/Solution.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
2-
public int largestUniqueNumber(int[] A) {
3-
int[] counter = new int[1001];
4-
for (int a : A) {
5-
++counter[a];
2+
public int largestUniqueNumber(int[] nums) {
3+
int[] cnt = new int[1001];
4+
for (int x : nums) {
5+
++cnt[x];
66
}
7-
for (int i = 1000; i >= 0; --i) {
8-
if (counter[i] == 1) {
9-
return i;
7+
for (int x = 1000; x >= 0; --x) {
8+
if (cnt[x] == 1) {
9+
return x;
1010
}
1111
}
1212
return -1;

solution/1100-1199/1133.Largest Unique Number/Solution.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/**
2-
* @param {number[]} A
2+
* @param {number[]} nums
33
* @return {number}
44
*/
5-
var largestUniqueNumber = function (A) {
6-
let counter = {};
7-
for (const a of A) {
8-
counter[a] = (counter[a] || 0) + 1;
5+
var largestUniqueNumber = function (nums) {
6+
const cnt = new Array(1001).fill(0);
7+
for (const x of nums) {
8+
++cnt[x];
99
}
10-
for (let i = 1000; i >= 0; --i) {
11-
if (counter[i] == 1) {
12-
return i;
10+
for (let x = 1000; x >= 0; --x) {
11+
if (cnt[x] == 1) {
12+
return x;
1313
}
1414
}
1515
return -1;
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
class Solution:
2-
def largestUniqueNumber(self, A: List[int]) -> int:
3-
counter = Counter(A)
4-
for i in range(1000, -1, -1):
5-
if counter[i] == 1:
6-
return i
7-
return -1
2+
def largestUniqueNumber(self, nums: List[int]) -> int:
3+
cnt = Counter(nums)
4+
return next((x for x in range(1000, -1, -1) if cnt[x] == 1), -1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function largestUniqueNumber(nums: number[]): number {
2+
const cnt = new Array(1001).fill(0);
3+
for (const x of nums) {
4+
++cnt[x];
5+
}
6+
for (let x = 1000; x >= 0; --x) {
7+
if (cnt[x] == 1) {
8+
return x;
9+
}
10+
}
11+
return -1;
12+
}

0 commit comments

Comments
 (0)