Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5060bbf

Browse files
author
Sathish Babu
committed
✨ Added solution to 747
1 parented50c8b commit5060bbf

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

‎src/0747.Largest-Number-At-Least-Twice-of-Others/README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,27 @@
55
66
##Description
77

8+
In a given integer array nums, there is always exactly one largest element.
9+
10+
Find whether the largest element in the array is at least twice as much as every other number in the array.
11+
12+
If it is, return the index of the largest element, otherwise return -1.
13+
14+
**Example 1:**
15+
16+
```
17+
Input: nums = [3, 6, 1, 0]
18+
Output: 1
19+
Explanation: 6 is the largest integer, and for every other number in the array x,
20+
6 is more than twice as big as x. The index of value 6 is 1, so we return 1.
21+
```
22+
823
**Example 1:**
924

1025
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
26+
Input: nums = [1, 2, 3, 4]
27+
Output: -1
28+
Explanation: 4 isn't at least as big as twice the value of 3, so we return -1.
1329
```
1430

1531
##题意
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
package Solution
22

3-
funcSolution(xbool)bool {
4-
returnx
3+
funcSolution(nums []int)int {
4+
n:=len(nums)
5+
ifn==1 {
6+
return0
7+
}
8+
max,maxIndex:=-1,-1
9+
fori,num:=rangenums {
10+
ifnum>max {
11+
max,maxIndex=num,i
12+
}
13+
}
14+
for_,num:=rangenums {
15+
ifnum!=max&&2*num>max {
16+
return-1
17+
}
18+
}
19+
returnmaxIndex
520
}

‎src/0747.Largest-Number-At-Least-Twice-of-Others/Solution_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
//测试用例
1111
cases:= []struct {
1212
namestring
13-
inputsbool
14-
expectbool
13+
inputs[]int
14+
expectint
1515
}{
16-
{"TestCase",true,true},
17-
{"TestCase",true,true},
18-
{"TestCase",false,false},
16+
{"TestCase",[]int{3,6,1,0},1},
17+
{"TestCase",[]int{1,2,3,4},-1},
18+
{"TestCase",[]int{6},0},
1919
}
2020

2121
//开始测试

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp