problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::largest_number_at_least_twice_of_others::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static int dominantIndex (vector< int > &nums)
 

详细描述

在文件 leetcode.h413 行定义.

成员函数说明

◆ dominantIndex()

int leetcode::largest_number_at_least_twice_of_others::Solution::dominantIndex ( vector< int > &  nums)
static

在文件 leetcode.cpp943 行定义.

943 {
944 if(nums.size() < 2) {
945 return 0;
946 }
947 unsigned int max;
948 unsigned int index;
949 unsigned int second;
950 if(nums[0] < nums[1]) {
951 max = nums[1];
952 index = 1;
953 second = nums[0];
954 } else {
955 max = nums[0];
956 index = 0;
957 second = nums[1];
958 }
959 for(int i = 2; i < nums.size(); i++) {
960 if(nums[i] > max) {
961 second = max;
962 index = i;
963 max = nums[i];
964 } else if(nums[i] > second) {
965 second = nums[i];
966 }
967 }
968 if(max >= 2 * second) {
969 return index;
970 }
971 return -1;
972 }

被这些函数引用 leetcode::largest_number_at_least_twice_of_others::TEST().


该类的文档由以下文件生成: