【力扣】169、多数元素

【力扣】169、多数元素_第1张图片

var majorityElement = function(nums) {
        const countInRang = (start,end,num)=>{
          let count = 0;
          for(let i=start;i<=end;i++){
            if(nums[i]==num){
              count++;
            }
          }
          return count;
        }

        const majority = (start,end)=>{
          if(start == end) return nums[start]
          let mid = start+Math.floor((end-start)/2)
          //左侧众数
          const l =majority(start,mid);
          //右侧众数
          const r = majority(mid+1,end);
          if(l==r) return l;
          //合并然后去找
          const l_cout = countInRang(start,end,l);
          const r_cout = countInRang(start,end,r);
          return l_cout>r_cout ? l : r;
        }
        return majority(0,nums.length-1)
      };

你可能感兴趣的:(leetcode,javascript,算法)