Skip to content

Commit dc07f3b

Browse files
authored
feat: add java solution to lc probolem: No.747.Largest Number At Least Twice of Others (doocs#542)
1 parent 1dc1b66 commit dc07f3b

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

solution/0700-0799/0747.Largest Number At Least Twice of Others/README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,20 @@
5858
<!-- 这里可写当前语言的特殊实现逻辑 -->
5959

6060
```java
61-
61+
class Solution {
62+
public int dominantIndex(int[] nums) {
63+
int maxIndex = 0;
64+
for (int i = 0; i < nums.length; i++) {
65+
if (nums[i] > nums[maxIndex])
66+
maxIndex = i;
67+
}
68+
for (int i = 0; i < nums.length; i++) {
69+
if (nums[i] * 2 > nums[maxIndex] && i != maxIndex)
70+
return -1;
71+
}
72+
return maxIndex;
73+
}
74+
}
6275
```
6376

6477
### **...**

solution/0700-0799/0747.Largest Number At Least Twice of Others/README_EN.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,20 @@ The index of value 6 is 1, so we return 1.
5050
### **Java**
5151

5252
```java
53-
53+
class Solution {
54+
public int dominantIndex(int[] nums) {
55+
int maxIndex = 0;
56+
for (int i = 0; i < nums.length; i++) {
57+
if (nums[i] > nums[maxIndex])
58+
maxIndex = i;
59+
}
60+
for (int i = 0; i < nums.length; i++) {
61+
if (nums[i] * 2 > nums[maxIndex] && i != maxIndex)
62+
return -1;
63+
}
64+
return maxIndex;
65+
}
66+
}
5467
```
5568

5669
### **...**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int dominantIndex(int[] nums) {
3+
int maxIndex = 0;
4+
for (int i = 0; i < nums.length; i++) {
5+
if (nums[i] > nums[maxIndex])
6+
maxIndex = i;
7+
}
8+
for (int i = 0; i < nums.length; i++) {
9+
if (nums[i] * 2 > nums[maxIndex] && i != maxIndex)
10+
return -1;
11+
}
12+
return maxIndex;
13+
}
14+
}

0 commit comments

Comments
 (0)