Leetcode747至少是其他数字两倍的最大数
Leetcode747至少是其他数字两倍的最大数
在一个给定的数组nums中,总是存在一个最大元素 。查找数组中的最大元素是否至少是数组中每个其他数字的两倍。如果是,则返回最大元素的索引,否则返回-1。
Given an array of integers nums, write a method that returns the "pivot" index of this array.We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.
示例 1:
输入: nums = [3, 6, 1, 0]
输出: 1
解释: 6是最大的整数, 对于数组中的其他整数,
6大于数组中其他元素的两倍。6的索引是1, 所以我们返回1.
示例 2:
输入: nums = [1, 2, 3, 4]
输出: -1
解释: 4没有超过3的两倍大, 所以我们返回 -1.
提示:
nums的长度范围在[1, 50].- 每个
nums[i]的整数范围在[0, 99].
java:
class Solution {
public int dominantIndex(int[] nums) {
int tmp=0,max=0,secondMax=0;
for(int i=0;i< nums.length;i++){
if(max<nums[i]){
secondMax=max;
max=nums[i];
tmp=i;
}else if(secondMax<nums[i]){
secondMax=nums[i];
}
}
if(max>=secondMax*2){
return tmp;
}else {
return -1;
}
}
}
python3
class Solution:
def dominantIndex(self, nums: List[int]) -> int:
"""
:type nums:list
:return: int
"""
maxAll=maxSecond=tmp=0
for i in range(len(nums)):
if nums[i]>maxAll:
maxSecond=maxAll
maxAll=nums[i]
tmp=i
elif maxSecond<nums[i]:
maxSecond=nums[i]
if maxAll>=maxSecond*2:
return tmp
return -1
思路:
这道题比较简单,就是从左遍历到最后记录并替换最大、第二大数值和索引。
如果有更好的方法请告知,谢谢

Leetcode747至少是其他数字两倍的最大数的更多相关文章
- LeetCode747 至少是其他数字两倍的最大数
在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 1: 输入: nums = [3, ...
- [Swift]LeetCode747. 至少是其他数字两倍的最大数 | Largest Number At Least Twice of Others
In a given integer array nums, there is always exactly one largest element. Find whether the largest ...
- LeetCode:至少是其他数字两倍的最大数【747】
LeetCode:至少是其他数字两倍的最大数[747] 题目描述 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素 ...
- Java实现 LeetCode 747 至少是其他数字两倍的最大数(暴力)
747. 至少是其他数字两倍的最大数 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 ...
- [LeetCode] Largest Number At Least Twice of Others 至少是其他数字两倍的最大数
In a given integer array nums, there is always exactly one largest element. Find whether the largest ...
- Leetcode747.Largest Number At Least Twice of Others至少是其他数字两倍的最大数
在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 1: 输入: nums = [3, ...
- [LC]747题 Largest Number At Least Twice of Others (至少是其他数字两倍的最大数)
①中文题目 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 1: 输入: nums ...
- C#LeetCode刷题之#747-至少是其他数字两倍的最大数( Largest Number At Least Twice of Others)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3746 访问. 在一个给定的数组nums中,总是存在一个最大元素 ...
- 747. Largest Number At Least Twice of Others比所有数字都大两倍的最大数
[抄题]: In a given integer array nums, there is always exactly one largest element. Find whether the l ...
随机推荐
- MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建
这次换了台电脑,所以须要又一次配置一次项目开发环境,过程中的种种,记录下来,便于以后再次安装.同一时候给大家一个參考. 1.JDK的安装 首先下载JDK,这个从sun公司官网能够下载.依据自己的系统选 ...
- web 开发之js---js获取select标签选中的值
var obj = document.getElementByIdx_x(”testSelect”); //定位id var index = obj.selectedIndex; // 选中索引 va ...
- POJ 1651 Multiplication Puzzle 区间dp(水
题目链接:id=1651">点击打开链 题意: 给定一个数组,每次能够选择内部的一个数 i 消除,获得的价值就是 a[i-1] * a[i] * a[i+1] 问最小价值 思路: dp ...
- XMU 1040 Schedule 【拓扑排序】
1040: Schedule Time Limit: 500 MS Memory Limit: 64 MBSubmit: 12 Solved: 2[Submit][Status][Web Boar ...
- securecrt中vim行号下划线问题及SecureCRT里root没有高亮的设置,修改linux终端命令行颜色
背景:在用raspberry用SecureCRT下的vim打开文件时出现用set nu时行有下划线,于是找了下解决办法,如下:vim行号下划线问题在vim中发现开启显示行号(set number) ...
- mipi差分信号原理
差分信号,什么是差分信号 一个差分信号是用一个数值来表示两个物理量之间的差异.从严格意义上来讲,所有电压信号都是差分的,因为一个电压只能是相对于另一个电压而言的.在某些系统里,系统’地’被用作电压基准 ...
- 杂项-DB:Druid
ylbtech-杂项-DB:Druid Apache Druid (incubating) is a high performance analytics data store for event-d ...
- 31. ExtJs4回车事件监听
转自:https://710542316.iteye.com/blog/2148542 监听表单字段事件: Ext使得对用户某个动作的监听特别简单,诸如单击某个元素或者按下某个键盘上的键. 一个经常性 ...
- E20170610-hm
presence n. 出席; 仪表; 风度; 鬼魂,神灵; defence n. 防御; 辩护; 防御工事; 后卫; phyle n. 种族,宗族; race n. 赛跑; 民族; 人种; ...
- argis地图