File tree 5 files changed +131
-2
lines changed
solution/0700-0799/0747.Largest Number At Least Twice of Others
5 files changed +131
-2
lines changed Original file line number Diff line number Diff line change 50
50
<!-- 这里可写当前语言的特殊实现逻辑 -->
51
51
52
52
``` python
53
-
53
+ class Solution :
54
+ def dominantIndex (self , nums : List[int ]) -> int :
55
+ max_idx, n = 0 , len (nums)
56
+ for i in range (1 , n):
57
+ if nums[i] > nums[max_idx]:
58
+ max_idx = i
59
+ for i in range (n):
60
+ if i != max_idx and nums[i] * 2 > nums[max_idx]:
61
+ return - 1
62
+ return max_idx
54
63
```
55
64
56
65
### ** Java**
@@ -74,6 +83,43 @@ class Solution {
74
83
}
75
84
```
76
85
86
+ ### ** C++**
87
+
88
+ ``` cpp
89
+ class Solution {
90
+ public:
91
+ int dominantIndex(vector<int >& nums) {
92
+ int maxIdx = 0, n = nums.size();
93
+ for (int i = 1; i < n; ++i) {
94
+ if (nums[ i] > nums[ maxIdx] ) maxIdx = i;
95
+ }
96
+ for (int i = 0; i < n; ++i) {
97
+ if (i != maxIdx && nums[ i] * 2 > nums[ maxIdx] ) return -1;
98
+ }
99
+ return maxIdx;
100
+ }
101
+ };
102
+ ```
103
+
104
+ ### **Go**
105
+
106
+ ```go
107
+ func dominantIndex(nums []int) int {
108
+ maxIndex, n := 0, len(nums)
109
+ for i := 1; i < n; i++ {
110
+ if nums[i] > nums[maxIndex] {
111
+ maxIndex = i
112
+ }
113
+ }
114
+ for i := 0; i < n; i++ {
115
+ if i != maxIndex && nums[i]*2 > nums[maxIndex] {
116
+ return -1
117
+ }
118
+ }
119
+ return maxIndex
120
+ }
121
+ ```
122
+
77
123
### ** ...**
78
124
79
125
```
Original file line number Diff line number Diff line change @@ -44,7 +44,16 @@ The index of value 6 is 1, so we return 1.
44
44
### ** Python3**
45
45
46
46
``` python
47
-
47
+ class Solution :
48
+ def dominantIndex (self , nums : List[int ]) -> int :
49
+ max_idx, n = 0 , len (nums)
50
+ for i in range (1 , n):
51
+ if nums[i] > nums[max_idx]:
52
+ max_idx = i
53
+ for i in range (n):
54
+ if i != max_idx and nums[i] * 2 > nums[max_idx]:
55
+ return - 1
56
+ return max_idx
48
57
```
49
58
50
59
### ** Java**
@@ -66,6 +75,43 @@ class Solution {
66
75
}
67
76
```
68
77
78
+ ### ** C++**
79
+
80
+ ``` cpp
81
+ class Solution {
82
+ public:
83
+ int dominantIndex(vector<int >& nums) {
84
+ int maxIdx = 0, n = nums.size();
85
+ for (int i = 1; i < n; ++i) {
86
+ if (nums[ i] > nums[ maxIdx] ) maxIdx = i;
87
+ }
88
+ for (int i = 0; i < n; ++i) {
89
+ if (i != maxIdx && nums[ i] * 2 > nums[ maxIdx] ) return -1;
90
+ }
91
+ return maxIdx;
92
+ }
93
+ };
94
+ ```
95
+
96
+ ### **Go**
97
+
98
+ ```go
99
+ func dominantIndex(nums []int) int {
100
+ maxIndex, n := 0, len(nums)
101
+ for i := 1; i < n; i++ {
102
+ if nums[i] > nums[maxIndex] {
103
+ maxIndex = i
104
+ }
105
+ }
106
+ for i := 0; i < n; i++ {
107
+ if i != maxIndex && nums[i]*2 > nums[maxIndex] {
108
+ return -1
109
+ }
110
+ }
111
+ return maxIndex
112
+ }
113
+ ```
114
+
69
115
### ** ...**
70
116
71
117
```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int dominantIndex (vector<int >& nums) {
4
+ int maxIdx = 0 , n = nums.size ();
5
+ for (int i = 1 ; i < n; ++i) {
6
+ if (nums[i] > nums[maxIdx]) maxIdx = i;
7
+ }
8
+ for (int i = 0 ; i < n; ++i) {
9
+ if (i != maxIdx && nums[i] * 2 > nums[maxIdx]) return -1 ;
10
+ }
11
+ return maxIdx;
12
+ }
13
+ };
Original file line number Diff line number Diff line change
1
+ func dominantIndex (nums []int ) int {
2
+ maxIndex , n := 0 , len (nums )
3
+ for i := 1 ; i < n ; i ++ {
4
+ if nums [i ] > nums [maxIndex ] {
5
+ maxIndex = i
6
+ }
7
+ }
8
+ for i := 0 ; i < n ; i ++ {
9
+ if i != maxIndex && nums [i ]* 2 > nums [maxIndex ] {
10
+ return - 1
11
+ }
12
+ }
13
+ return maxIndex
14
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def dominantIndex (self , nums : List [int ]) -> int :
3
+ max_idx , n = 0 , len (nums )
4
+ for i in range (1 , n ):
5
+ if nums [i ] > nums [max_idx ]:
6
+ max_idx = i
7
+ for i in range (n ):
8
+ if i != max_idx and nums [i ] * 2 > nums [max_idx ]:
9
+ return - 1
10
+ return max_idx
You can’t perform that action at this time.
0 commit comments