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.

 1 int majorityElement(int* nums, int numsSize)
 2 {
 3     if(numsSize==1)
 4         return nums[0];
 5
 6
 7     int i,j,k;
 8     for(i=1;i<numsSize;i++)
 9     {
10         for(j=i-1;j>=0;j--)
11         {
12             if(nums[i]>=nums[j])
13                 break;
14         }
15
16         if(i!=j-1)
17         {
18             int temp=nums[i];
19             for(k=i-1;k>j;k--)
20             {
21                 nums[k+1]=nums[k];
22             }
23             nums[k+1]=temp;
24         }
25     }
26
27     int Time;
28     Time=numsSize/2;
29     int count=1;
30     for(i=0;i<numsSize-1;i++)
31     {
32         if(nums[i]==nums[i+1])
33         {
34             count++;
35             if(count>Time)
36             {
37                 return nums[i];
38             }
39         }
40         else
41         {
42             count=1;
43         }
44     }
45
46
47     return 0;
48 }

转载于:https://www.cnblogs.com/vpoet/p/4660492.html

LeeCode-Majority Element相关推荐

  1. Majority Element:主元素

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  2. leetcode 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. LeetCode Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. [LeetCode] Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. 【LeetCode】169. Majority Element

    原题链接:https://leetcode.com/problems/majority-element/description/ 要求: Given an array of size n, find ...

  6. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  7. Leetcode - 169. Majority Element (多数投票问题)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. 【LeetCode从零单排】No.169 Majority Element(hashmap用法)

    题目 Given an array of size n, find the majority element. The majority element is the element that app ...

  9. [LeetCode]: 169: Majority Element

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

  10. 【LeetCode 169】Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

最新文章

  1. mysql innodb 数据复制_五千万的数据 复制到另张表 是先复制数据再加索引还是先建好索引再复制数据 表引擎Innodb...
  2. 老树发新芽 靠PS之类的软件,上市30年的Adobe股价创新高
  3. ***常用vbs脚本
  4. oracle如何清空一个用户下的所有表中的数据?
  5. 一款小清新的 SpringBoot+ Mybatis 前后端分离后台管理系统项目
  6. android 布局之scrollview
  7. 解决: WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
  8. [李景山php]每天TP5-20170110|thinkphp5-Model.php-3
  9. solrcloud和zookeeper的搭建、使用、心得、教训
  10. 【UWA 学堂】部分渲染课程的调价通知
  11. 阿尔法贝塔剪枝——中国象棋人机对战
  12. jvm垃圾回收机制查看及说明
  13. 解决布局点击事件穿透的问题
  14. Linux的磁盘分区、基本目录结构
  15. MySQL有哪些“饮鸩止渴”提高性能的方法?
  16. qnap raid5升级raid6_实践出真知!100TB的RAID5到底能否重建成功?
  17. ali arthas 火焰图_手把手教你看懂Chrome火焰图!(调试性能必备)
  18. JS实现鼠标中心放大图片功能原理及实例演示
  19. Zynq笔记(一) —PL端(FPGA部分)的设计流程
  20. 私域营销突围,需要一点“无忌”精神

热门文章

  1. swift_039(Swift中的KVC的使用方法)
  2. 跳一跳python源码下载_Python玩跳一跳【简】
  3. cesium加载百度地图_四大Webgis地图框架的对比选择
  4. Java蓝桥杯——贪心算法
  5. Vicious Keyboard CodeForces - 801A (暴力+模拟)
  6. 三分钟学会用SpringMVC搭建最小系统(超详细)_转载
  7. 【spring教程之中的一个】创建一个最简单的spring样例
  8. flexpaper 背景色变化
  9. [Flex]Flex SDK 4(Gumbo)改善的Layout和Scroller(二)
  10. 浏览器禁止跨域请求json数据解决方法--jsonp