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 ...
随机推荐
- Kernel Live-patching (by quqi99)
作者:张华 发表于:2016-02-27 版权声明:能够随意转载.转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 ( http://blog.csdn.net/quqi99 ) GC ...
- java json字符串转成 Map或List
import java.util.List; import java.util.Map; import java.util.Map.Entry; import net.sf.json.JSONArra ...
- Java编程中经常用到代码
http://www.toutiao.com/i6429293556086604289/
- socketserver模块三次登陆验证,身份验证
帅爆太阳的男人 1,socketserver是解决TCP服务器和多个客户端进行通信 服务器: import socketserver class MySocket(socketserver,BaseR ...
- [Codeforces 339D] Xenia and Bit Operations
[题目链接] https://codeforces.com/problemset/problem/339/D [算法] 线段树模拟即可 时间复杂度 :O(MN) [代码] #include<bi ...
- Flask-SQLAlchemy - 不使用外键连表查询。记得常回来看我
前言 相比于 Django 的 ORM ,SQLAlchemy "不依靠外键进行跨表联查" 的解决方案就比较多. 没啥好说的,只能怪自己学艺不精.. _(:з」∠)_ 解决办法 ...
- 分布式消息通信(ActiveMQ)
分布式消息通信(ActiveMQ) 应用场景 异步通信 应用解耦 流量削峰 # ActiveMQ安装 下载 http://activemq.apache.org/ 压缩包上传到Linux系统 apac ...
- Gamma阶段测试计划
前言 点击这一链接访问公课网(笨拙软件工程组). 一.Alpha阶段场景测试 1.1 鹿丸:无欲无求大三学生 保研无望 不在乎给分 只想选择干货多的课程 充实自己 需求和目标:了解各专业课程的授课内容 ...
- 个人微信号二次开发SDK协议,个人微信号二次开发api接口
通过这个API接口可以做什么? 通过我们提供的API接口您可以开发: 工作手机(如:X创,X码,XX管家等) 微信群讲课软件(如:讲课X师,一起X堂等) 微信社群管理软件(如:小X管家,微X助手等) ...
- T - Posterized(贪心思维)
Description Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked hi ...