Majority Element(ARRAY-BINARY SEARCH)

QUESTION

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

FIRST TRY

每找出两个不同的element,则成对删除。最终剩下的一定就是所求的。

class Solution {
public:
    int majorityElement(vector<int> &num) {
        int ret;
        int nTimes = 0;
        for(int i = 0; i < num.size(); i++){
            if(nTimes == 0)
            {
                ret = num[i];
                nTimes++;
            }
            else if(ret == num[i]) nTimes++;
            else nTimes--; //2 different number, delete
        }
        return ret;
    }
};

Result: Accepted

可扩展到⌊ n/k ⌋的情况,每k个不同的element进行成对删除。

时间: 2024-11-10 08:12:23

Majority Element(ARRAY-BINARY SEARCH)的相关文章

LeetCode 33 Search in Rotated Sorted Array [binary search] &lt;c++&gt;

LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前半部分接到后半部分后面,得到一个新数组,在新数组中查找给定数的下标,如果没有,返回-1.时间复杂度限制\(O(log_2n)\) C++ 我的想法是先找到数组中最大值的位置.然后以此位置将数组一分为二,然后在左右两部分分别寻找target. 二分寻找最大值的时候,因为左半部分的数一定大于nums[l],所以n

169. Majority Element (Array)

Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in the array. Moore voting algorithm--每找出两

LeetCode 5题 --Array+Binary search

这5道题都是array的binary search就可以搞定了 分别是leetcode(35)--Search Insert Position  leetcode(33)--Search in Rotated Sorted Array  leetcode(81)--Search in Rotated Sorted Array II  leetcode(34)--Search for a Range  leetcode(74)--Search a 2D Matrix 一:leetcode(35)-

Jan 23 - Search In Rotated Array II; Array; Binary Search;

public class Solution { public boolean search(int[] nums, int target) { int len = nums.length; if(len == 0) return false; int low = 0; int high = len-1; while(low < high){ int mid = (low+high)/2; // do not concern the integer overrange situation if(n

LeetCode -- Search in Rotated Sorted Array(Binary Search)

题目地址:https://leetcode.com/problems/search-in-rotated-sorted-array/ Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the ar

Jan 16 - Search for a Range; Array; Binary Search;

注意index不要出现out of bounds的情况: 代码: public class Solution { public int[] searchRange(int[] nums, int target) { int len = nums.length; int high = len-1; int low = 0; int index1 = -1, index2 = -1; int[] result = new int[2]; result[0] = index1; result[1] =

Jan 16 - Search Insert Position; Array; Binary Search; Iteration&amp;Recursion;---Iteration再补上

Recursion: 代码: public class Solution { public int searchInsert(int[] nums, int target) { int len = nums.length; if(len == 0) return 0; return findPosition(nums, 0, len-1, target); } public int findPosition(int[] nums, int start, int end, int target){

find second maximum element in Binary search tree

一个BST, 问怎么找到第二大的节点.首先说了个naive的方法,serialize, 他问有没有更有效的方法.最后用的iterative的方法向右遍历子节点 log(n) 或者inorder, O(n): public int find(TreeNode root) { TreeNode first = root; TreeNode second = null; while (root != null) { if (root.right == null && root.left == n

Binary Search - Jump on the Stones

Binary Search algorithm. Wikipedia definition: In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary s

33. Search in Rotated Sorted Array &amp;&amp; 81. Search in Rotated Sorted Array II &amp;&amp;

33. Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise