Skip to content

Commit cd1ff77

Browse files
committed
feat: add solutions to leetcode problem: No.1133. Largest Unique Number
1 parent 18a3c55 commit cd1ff77

File tree

5 files changed

+121
-23
lines changed

5 files changed

+121
-23
lines changed

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

+43-2
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,68 @@
3737
<li><code>0 &lt;= A[i] &lt;= 1000</code></li>
3838
</ol>
3939

40-
4140
## 解法
4241

4342
<!-- 这里可写通用的实现逻辑 -->
4443

44+
计数器实现。
45+
4546
<!-- tabs:start -->
4647

4748
### **Python3**
4849

4950
<!-- 这里可写当前语言的特殊实现逻辑 -->
5051

5152
```python
52-
53+
class Solution:
54+
def largestUniqueNumber(self, A: List[int]) -> int:
55+
counter = collections.Counter(A)
56+
for i in range(1000, -1, -1):
57+
if counter[i] == 1:
58+
return i
59+
return -1
5360
```
5461

5562
### **Java**
5663

5764
<!-- 这里可写当前语言的特殊实现逻辑 -->
5865

5966
```java
67+
class Solution {
68+
public int largestUniqueNumber(int[] A) {
69+
int[] counter = new int[1001];
70+
for (int a : A) {
71+
++counter[a];
72+
}
73+
for (int i = 1000; i >= 0; --i) {
74+
if (counter[i] == 1) {
75+
return i;
76+
}
77+
}
78+
return -1;
79+
}
80+
}
81+
```
6082

83+
### **JavaScript**
84+
85+
```js
86+
/**
87+
* @param {number[]} A
88+
* @return {number}
89+
*/
90+
var largestUniqueNumber = function (A) {
91+
let counter = {};
92+
for (const a of A) {
93+
counter[a] = (counter[a] || 0) + 1;
94+
}
95+
for (let i = 1000; i >= 0; --i) {
96+
if (counter[i] == 1) {
97+
return i;
98+
}
99+
}
100+
return -1;
101+
};
61102
```
62103

63104
### **...**

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

+41-21
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,12 @@
66

77
<p>Given an array of integers <code>A</code>, return the largest integer that only occurs once.</p>
88

9-
10-
119
<p>If no integer occurs once, return -1.</p>
1210

13-
14-
1511
<p>&nbsp;</p>
1612

17-
18-
1913
<p><strong>Example 1:</strong></p>
2014

21-
22-
2315
<pre>
2416

2517
<strong>Input: </strong><span id="example-input-1-1">[5,7,3,9,4,9,8,3,1]</span>
@@ -32,12 +24,8 @@ The maximum integer in the array is 9 but it is repeated. The number 8 occurs on
3224

3325
</pre>
3426

35-
36-
3727
<p><strong>Example 2:</strong></p>
3828

39-
40-
4129
<pre>
4230

4331
<strong>Input: </strong><span id="example-input-1-1">[9,9,8,8]</span>
@@ -50,37 +38,69 @@ There is no number that occurs only once.
5038

5139
</pre>
5240

53-
54-
5541
<p>&nbsp;</p>
5642

57-
58-
5943
<p><strong>Note:</strong></p>
6044

61-
62-
6345
<ol>
6446
<li><code>1 &lt;= A.length &lt;= 2000</code></li>
6547
<li><code>0 &lt;= A[i] &lt;= 1000</code></li>
6648
</ol>
6749

68-
69-
7050
## Solutions
7151

7252
<!-- tabs:start -->
7353

7454
### **Python3**
7555

7656
```python
77-
57+
class Solution:
58+
def largestUniqueNumber(self, A: List[int]) -> int:
59+
counter = collections.Counter(A)
60+
for i in range(1000, -1, -1):
61+
if counter[i] == 1:
62+
return i
63+
return -1
7864
```
7965

8066
### **Java**
8167

8268
```java
69+
class Solution {
70+
public int largestUniqueNumber(int[] A) {
71+
int[] counter = new int[1001];
72+
for (int a : A) {
73+
++counter[a];
74+
}
75+
for (int i = 1000; i >= 0; --i) {
76+
if (counter[i] == 1) {
77+
return i;
78+
}
79+
}
80+
return -1;
81+
}
82+
}
83+
```
8384

85+
### **JavaScript**
86+
87+
```js
88+
/**
89+
* @param {number[]} A
90+
* @return {number}
91+
*/
92+
var largestUniqueNumber = function (A) {
93+
let counter = {};
94+
for (const a of A) {
95+
counter[a] = (counter[a] || 0) + 1;
96+
}
97+
for (let i = 1000; i >= 0; --i) {
98+
if (counter[i] == 1) {
99+
return i;
100+
}
101+
}
102+
return -1;
103+
};
84104
```
85105

86106
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int largestUniqueNumber(int[] A) {
3+
int[] counter = new int[1001];
4+
for (int a : A) {
5+
++counter[a];
6+
}
7+
for (int i = 1000; i >= 0; --i) {
8+
if (counter[i] == 1) {
9+
return i;
10+
}
11+
}
12+
return -1;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} A
3+
* @return {number}
4+
*/
5+
var largestUniqueNumber = function (A) {
6+
let counter = {};
7+
for (const a of A) {
8+
counter[a] = (counter[a] || 0) + 1;
9+
}
10+
for (let i = 1000; i >= 0; --i) {
11+
if (counter[i] == 1) {
12+
return i;
13+
}
14+
}
15+
return -1;
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def largestUniqueNumber(self, A: List[int]) -> int:
3+
counter = collections.Counter(A)
4+
for i in range(1000, -1, -1):
5+
if counter[i] == 1:
6+
return i
7+
return -1

0 commit comments

Comments
 (0)