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 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.
记录,第一大的数和第二大的数,判断2倍关系就行了。
class Solution {
public:
int dominantIndex(vector<int>& nums) {
int max1 = -1;
int max2 = -1;
int id = 0;
for(int i = 0; i < nums.size(); ++i) {
int x = nums[i];
if (x > max1) max2 = max1, max1 = x, id = i;
else if (x > max2) max2 = x;
}
if (max1 >= 2*max2) return id;
return -1;
}
};
leetcode 747. Largest Number At Least Twice of Others的更多相关文章
- [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 ...
- 【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 ...
随机推荐
- html-屏蔽按键盘空格键是滚动条向下滚动
document.onkeydown = function(ev){ var e = ev || event; if(e.keyCode == 32){ return false; } }
- js-控制浏览器和移动端的后退按钮 . popstate
//控制浏览器和移动端的后退按钮 if (window.history && window.history.pushState) { $(window).on('popstate', ...
- String()和.toString()的区别
一.相同点:都可以转为字符串类型: 二.不同点: 1..toString() :null.toString()和undefined.toString() 程序报错误; 2..toString(): . ...
- GitHub 上受欢迎的 Android UI Library 整理(一)
抽屉菜单 https://github.com/mikepenz/MaterialDrawer ★7337 - 安卓抽屉效果实现方案https://github.com/Yalantis/Side-M ...
- [simple-orm-mybaits]基于Mybatis的ORM封装介绍
目录 前言 ORM框架现状 Mybatis优缺点 simple-orm-mybatis设计思路介绍 simple-orm-mybatis使用说明 simple-orm-mybatis实际使用 推荐最佳 ...
- android toolbar 假标题居中
<android.support.v7.widget.Toolbar android:id="@+id/toolbar_top" android:layout_height= ...
- asp.net Excel导入&导出
1.Excel数据导入到数据库中: //该方法实现从Excel中导出数据到DataSet中,其中filepath为Excel文件的绝对路径,sheetname为表示那个Excel表: p ...
- sh_Spring整合Hibernate
分别介绍了Sping和Hibernate,以下是将它们整合到一块去了. 一.Hibernate内容 1.创建PO类. package cn.tgb.domain; //User实体 public ...
- Jenkins和Maven构建持续集成
真是运维的福利,不用在敲Linux命令了 须要的工具:Linux或window.Jenkins.tomcat7.Jdk.maven.项目部署的war包 1.首先从Jenkins官网下载最新的Jenki ...
- js json 对象
JSON 语法规则 JSON 语法是 JavaScript 对象表示法语法的子集. 数据在名称/值对中 数据由逗号分隔 大括号保存对象 中括号保存数组 JSON 名称/值对 JSON 数据的书写格式是 ...