Skip to content

Commit 959e309

Browse files
committed
Add solution 747
1 parent 172d2fc commit 959e309

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package leetcode
2+
3+
func dominantIndex(nums []int) int {
4+
maxNum, flag, index := 0, false, 0
5+
for i, v := range nums {
6+
if v > maxNum {
7+
maxNum = v
8+
index = i
9+
}
10+
}
11+
for _, v := range nums {
12+
if v != maxNum && 2*v > maxNum {
13+
flag = true
14+
}
15+
}
16+
if flag {
17+
return -1
18+
}
19+
return index
20+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question747 struct {
9+
para747
10+
ans747
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para747 struct {
16+
nums []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans747 struct {
22+
one int
23+
}
24+
25+
func Test_Problem747(t *testing.T) {
26+
27+
qs := []question747{
28+
29+
{
30+
para747{[]int{3, 6, 1, 0}},
31+
ans747{1},
32+
},
33+
34+
{
35+
para747{[]int{1, 2, 3, 4}},
36+
ans747{-1},
37+
},
38+
39+
{
40+
para747{[]int{1}},
41+
ans747{0},
42+
},
43+
}
44+
45+
fmt.Printf("------------------------Leetcode Problem 747------------------------\n")
46+
47+
for _, q := range qs {
48+
_, p := q.ans747, q.para747
49+
fmt.Printf("【input】:%v 【output】:%v\n", p, dominantIndex(p.nums))
50+
}
51+
fmt.Printf("\n\n\n")
52+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [747. Largest Number At Least Twice of Others](https://leetcode.com/problems/largest-number-at-least-twice-of-others/)
2+
3+
4+
## 题目
5+
6+
You are given an integer array `nums` where the largest integer is **unique**.
7+
8+
Determine whether the largest element in the array is **at least twice** as much as every other number in the array. If it is, return *the **index** of the largest element, or return* `-1` *otherwise*.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: nums = [3,6,1,0]
14+
Output: 1
15+
Explanation: 6 is the largest integer.
16+
For every other number in the array x, 6 is at least twice as big as x.
17+
The index of value 6 is 1, so we return 1.
18+
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: nums = [1,2,3,4]
25+
Output: -1
26+
Explanation: 4 is less than twice the value of 3, so we return -1.
27+
```
28+
29+
**Example 3:**
30+
31+
```
32+
Input: nums = [1]
33+
Output: 0
34+
Explanation: 1 is trivially at least twice the value as any other number because there are no other numbers.
35+
36+
```
37+
38+
**Constraints:**
39+
40+
- `1 <= nums.length <= 50`
41+
- `0 <= nums[i] <= 100`
42+
- The largest element in `nums` is unique.
43+
44+
## 题目大意
45+
46+
给你一个整数数组 nums ,其中总是存在 唯一的 一个最大整数 。请你找出数组中的最大元素并检查它是否 至少是数组中每个其他数字的两倍 。如果是,则返回 最大元素的下标 ,否则返回 -1 。
47+
48+
## 解题思路
49+
50+
- 简单题。先扫描一遍找到最大值和下标。再扫描一遍检查最大值是否是其他数字的两倍。
51+
52+
## 代码
53+
54+
```go
55+
package leetcode
56+
57+
func dominantIndex(nums []int) int {
58+
maxNum, flag, index := 0, false, 0
59+
for i, v := range nums {
60+
if v > maxNum {
61+
maxNum = v
62+
index = i
63+
}
64+
}
65+
for _, v := range nums {
66+
if v != maxNum && 2*v > maxNum {
67+
flag = true
68+
}
69+
}
70+
if flag {
71+
return -1
72+
}
73+
return index
74+
}
75+
```

0 commit comments

Comments
 (0)