Pretty easy understanding JAVA solution


  • -1
    A

    public class Solution {

     public List<Integer> majorityElement(int[] nums) {
    
        List<Integer> re = new ArrayList<Integer>();
        int times = (nums.length / 3) + 1;//at least this times
        // int size = nums.length/times;//at most these numbers
        Arrays.sort(nums);
        for(int i = 0; i < nums.length - times + 1; i++){
            if(nums[i] == nums[i + times - 1]){
                if(!re.contains(nums[i])){
                    re.add(nums[i]);
                } 
                i = i + times - 1;
            }
        }
        return re;
    }
    

    }


  • 0
    S

    It's not O(N) time and O(1) space if you sort it. Sorting algorithms vary. And Arrays.sort() is QuickSort with best case of O(NlogN) run-time. Even bucket sort could use O(N) time but need extra space.


  • 0
    A

    Yeah. I agree with u. Cz I did not finish the first time Leetcode yet, my goal for first time is just AC. hhh:)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.