腾讯//求众数

给定一个大小为 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。

你可以假设数组是非空的,并且给定的数组总是存在众数。

示例 1:

输入: [3,2,3]
输出: 3

示例 2:

输入: [2,2,1,1,1,2,2]
输出: 2
class Solution {
public:
    int majorityElement(vector<int>& nums) {
        if(nums.size()==1)
            return nums[0];
        map<int,int> tables;
        for(int i = 0; i < nums.size(); i++){
            if(tables.count(nums[i])){
                tables[nums[i]]++;
                if(tables[nums[i]]>nums.size()/2)
                          return nums[i];
            }else{
                tables[nums[i]] = 1;
            }
        }
    }
};
class Solution {
    // 众数求解算法
    public int majorityElement(int[] nums) {
        // 黑板(用于记录数据用)
        LinkedHashMap<Integer, Integer> blackboard = new LinkedHashMap<>();
        // 左边记录到的位置
        int leftPs = 0;
        // 右边记录到的位置
        int rightPs = nums.length - 1;
        // 众数
        Integer mode = null;

        tag: while (leftPs <= rightPs) {
            for (int i = leftPs; i <= rightPs; i++) {
                if (blackboard.get(nums[i]) == null) {
                    blackboard.put(nums[i], 1);
                    leftPs = i + 1;
                    if (leftPs > rightPs) {
                        mode = nums[i];
                    }
                    break;
                } else {
                    int count = blackboard.get(nums[i]);
                    count++;
                    if (count>nums.length/2) {
                        mode = nums[i];
                        break tag;
                    }
                    blackboard.put(nums[i], count);
                }
            }
            for (int i = rightPs; i >= leftPs; i--) {
                if (blackboard.get(nums[i]) == null) {
                    blackboard.put(nums[i], 1);
                    rightPs = i - 1;
                    if (rightPs < leftPs) {
                        mode = nums[i];
                    }
                    break;
                } else {
                    int count = blackboard.get(nums[i]);
                    count++;
                    if (count>nums.length/2) {
                        mode = nums[i];
                        break tag;
                    }
                    blackboard.put(nums[i], count);
                }
            }
        }
        return mode;
    }
}

 

posted @ 2018-10-26 19:55  strawqqhat  阅读(92)  评论(0)    收藏  举报
#home h1{ font-size:45px; } body{ background-image: url("放你的背景图链接"); background-position: initial; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-origin: initial; background-clip: initial; height:100%; width:100%; } #home{ opacity:0.7; } .wall{ position: fixed; top: 0; left: 0; bottom: 0; right: 0; } div#midground{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -1; -webkit-animation: cc 200s linear infinite; -moz-animation: cc 200s linear infinite; -o-animation: cc 200s linear infinite; animation: cc 200s linear infinite; } div#foreground{ background: url("https://i.postimg.cc/z3jZZD1B/foreground.png"); z-index: -2; -webkit-animation: cc 253s linear infinite; -o-animation: cc 253s linear infinite; -moz-animation: cc 253s linear infinite; animation: cc 253s linear infinite; } div#top{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -4; -webkit-animation: da 200s linear infinite; -o-animation: da 200s linear infinite; animation: da 200s linear infinite; } @-webkit-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-o-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-moz-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @keyframes cc { 0%{ background-position: 0 0; } 100%{ background-position: 600% 0; } } @keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-webkit-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-moz-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-ms-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } }