[leetcode-748-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.
Note:
nums
will have a length in the range[1, 50]
.- Every
nums[i]
will be an integer in the range[0, 99]
.
思路:
扫描一遍数组,记录最大值与第二大的值,如果最大的是第二大的两倍以上,那么返回索引即可。
int dominantIndex(vector<int>& nums)
{
int len = nums.size();
int biggest = ,second = ,index = ; for(int i=;i<len;i++)
{
if(nums[i]>biggest)
{
second = biggest;
biggest = nums[i];
index = i;
}
else if(nums[i]>second)
{
second = nums[i];
}
}
if(biggest>=second*)return index;
else return -;
}
[leetcode-748-Largest Number At Least Twice of Others]的更多相关文章
- 【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 ...
- 【leetcode】Largest Number
题目简述: Given a list of non negative integers, arrange them such that they form the largest number. Fo ...
- leetcode 179. Largest Number 求最大组合数 ---------- java
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 【leetcode】Largest Number ★
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
随机推荐
- git 码云 使用记录
使用了码云的私有仓库. 一.首先下载安装git 安装完成后,在开始菜单里找到“Git”->“Git Bash”,蹦出一个类似命令行窗口的东西,就说明Git安装成功! 二.创建版本库 什么是版本库 ...
- cmd tab自动补全
- 慎使用sql的enum字段类型
在sql的优化中,会有同学提到一点:使用enum字段类型,代替其他tinyint等类型.以前这也是不少人喜欢优化的,但是现在细想,是非常不合理的. 优点: 1.可以设置区间范围,比如设置性别:1男2女 ...
- Linux phpmailer发送邮件失败的解决方法
(本地windows phpmailer发送ok 放到linux发送失败) 原因:linux 通过465端口进行更安全的SMTPS协议发送邮件 windows 是基于smtp 25端口的 因此 可 ...
- requests+mongodb爬取今日头条,多进程
import json import os from urllib.parse import urlencode import pymongo import requests from bs4 imp ...
- linux进程篇 (三) 进程间的通信3 IPC通信
3 IPC通信 用户空间 进程A <----无法通信----> 进程B -----------------|--------------------------------------|- ...
- python note 001
.tilte() .upper() .lower() --- \n \t --- "apple"+" "+"pen" --- .strip( ...
- JavaScript预解析
定义:JavaScript"预解析",可以理解为把变量或函数预先解析到它们被使用的环境中. 通俗点讲,即认为浏览器在正式运行JavaScript代码前, 第一步,会预先根据关键字v ...
- E. Almost Regular Bracket Sequence
题目链接:http://codeforces.com/contest/1095/problem/E 解题心得: 刚开始拿到这个题的时候还真的没什么思路,后来仔细想想还是比较简单的.首先题目要求翻转一个 ...
- [Codefroces401D]Roman and Numbers(状压+数位DP)
题意:给定一个数,求将该数重新排列后mod m==0的方案数 重新排列就考虑到用到哪些数,以及此时mod m的值 于是dp[i][j]表示状态i中mod m==j的方案数 注意:转移的时候只要找到一种 ...