[LeetCode] 747. Largest Number At Least Twice of Others_Easy
In a given integer array nums, there is always exactly one largest element.
Find 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, otherwise return -1.
Example 1:
Input: nums = [3, 6, 1, 0]
Output: 1
Explanation: 6 is the largest integer, and for every other number in the array x,
6 is more than twice as big as x. The index of value 6 is 1, so we return 1.
Example 2:
Input: nums = [1, 2, 3, 4]
Output: -1
Explanation: 4 isn't at least as big as twice the value of 3, so we return -1.
Note:
numswill have a length in the range[1, 50].- Every
nums[i]will be an integer in the range[0, 99].
思路就是简单的先找最大值, 然后再pass一遍.
Code
class Solution:
def dominantIndex(self, nums):
m = max(nums)
for num in nums:
if num != m and num*2 > m:
return -1
return nums.index(m)
[LeetCode] 747. Largest Number At Least Twice of Others_Easy的更多相关文章
- leetcode 747. 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_easy】747. Largest Number At Least Twice of Others
problem 747. Largest Number At Least Twice of Others 题意: solution1: class Solution { public: int dom ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- JavaScript中sort方法的一个坑(leetcode 179. Largest Number)
在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...
- Leetcode:Largest Number详细题解
题目 Given a list of non negative integers, arrange them such that they form the largest number. For e ...
- [LeetCode][Python]Largest Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/largest ...
- leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数
这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...
- [LeetCode] 179. Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. Example ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
随机推荐
- rxjs 常用的静态操作符
操作符文档 API create const { Observable } = require('rxjs'); // 创建 Observables var observable = Observab ...
- io流和序列化
1.使用File操作文件 public class IoTest { public static void main(String[] args) throws IOException { /* 01 ...
- [No0000EC]C# 字符串(String)
在 C# 中,可以使用字符数组来表示字符串,但是,更常见的做法是使用 string 关键字来声明一个字符串变量.string 关键字是 System.String 类的别名. 创建 String 对象 ...
- hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion
http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...
- MySQL获取分组后的TOP 1和TOP N记录-转
有时会碰到一些需求,查询分组后的最大值,最小值所在的整行记录或者分组后的top n行的记录,在一些别的数据库可能有窗口函数可以方面的查出来,但是MySQL没有这些函数,没有直接的方法可以查出来,可通过 ...
- Django:模型model和数据库mysql(二)
上一篇把简单的模型与数据库的搭建写了一遍,但模型中有很多深入好用的写法补充一下. 同样的栗子,建立新的模型与数据库来写一写 1.依然是搭建环境 >>>django-admin sta ...
- 如何写好.babelrc?Babel的presets和plugins配置解析
什么是Babel The compiler for writing next generation JavaScript. 官网是这么说的,翻译一下就是下一代JavaScript 语法的编译器. 作为 ...
- jvisualvm All-in-One Java Troubleshooting Tool
java 监控.故障.性能可视化分析 VisualVM: Download https://visualvm.github.io/download.html All-in-One Java Troub ...
- distributed computing_the World Wide Web
RESTful Web APIs_2013 I'm going to show you a better way to do distributed computing, using the idea ...
- python学习目录(转载)
python基础篇 python 基础知识 python 初始python python 字符编码 python 类型及变量 python 字符串详解 python 列表详解 ...