数组中占比超过一半的元素称之为主要元素。给你一个 整数 数组,找出其中的主要元素。若没有,返回 -1 。请设计时间复杂度为 O(N) 、空间复杂度为 O(1) 的解决方案。
难度:简单
示例 1:
输入:[1,2,5,9,5,9,5,5,5]
输出:5
示例 2:
输入:[3,2]
输出:-1
示例 3:
输入:[2,2,1,1,1,2,2]
输出:2
链接:https://leetcode-cn.com/problems/find-majority-element-lcci
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; class Solution { public int majorityElement(int[] nums) { int candidate = -1; int count = 0; for (int num : nums) { if (count == 0) { candidate = num; } if (num == candidate) { count++; } else { count--; } } count = 0; int length = nums.length; for (int num : nums) { if (num == candidate) { count++; } } return count * 2 > length ? candidate : -1; } public int[] stringToInt(String[] arr) { int[] ints = new int[arr.length]; for (int i=0; i<arr.length; i++) { ints[i] = Integer.parseInt(arr[i]); } return ints; } public static void main(String[] args) throws IOException { // int[] nums = {1,2,5,9,5,9,5,5,5}; // System.out.println(new Solution().majorityElement(nums)); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; while ((str = br.readLine()) != null) { try { // nums String[] numsArray = str.replace("[", "").replace("]", "").split(","); System.out.println("numsArray: " + Arrays.toString(numsArray)); int[] nums = new Solution().stringToInt(numsArray); System.out.println(new Solution().majorityElement(nums)); } catch (Exception e) { // e.printStackTrace(); // 回车一行就退出 break; } } } }View Code
连续输入[1,2,5,9,5,9,5,5,5]:
public static void main(String[] args) throws IOException {
// int[] nums = {1,2,5,9,5,9,5,5,5};
// System.out.println(new Solution().majorityElement(nums));
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
String str;
while ((str = br.readLine()) != null) {
try {
// nums
String[] numsArray =
str.replace("[", "").replace("]", "").split(",");
System.out.println("numsArray: " + Arrays.toString(numsArray));
int[] nums = new Solution().stringToInt(numsArray);
System.out.println(new Solution().majorityElement(nums));
} catch (Exception e) {
// e.printStackTrace();
// 回车一行就退出
break;
}
}
}
c++
#include "iostream" #include "vector" using namespace std; class Solution { public: int majorityElement(vector<int>& nums) { int candidate = -1; int count = 0; for (int& num: nums) { if (count == 0) { candidate = num; } if (num == candidate) { count++; } else { count--; } } count = 0; int length = nums.size(); for (int& num:nums) { if (num == candidate) { count++; } } return count * 2 > length ? candidate : -1; } }; int main() { vector<int> nums = {1,2,5,9,5,9,5,5,5}; cout << Solution().majorityElement(nums) << endl; }View Code
c
#include "stdio.h" int majorityElement(int* nums, int numsSize) { int candidate = -1; int count = 0; for (int i=0; i<numsSize; i++) { if (count == 0) { candidate = nums[i]; } if (nums[i] == candidate) { count++; } else { count--; } } count = 0; int length = numsSize; for (int i=0; i<numsSize; i++) { if (nums[i] == candidate) { count++; } } return count * 2 > length ? candidate : -1; } int main() { int nums[] = {1,2,5,9,5,9,5,5,5}; int numsSize = sizeof(nums) / sizeof(nums[0]); printf("%d ", majorityElement(nums, numsSize)); }View Code