题目描写叙述

Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it..

Example

Given [1, 1, 1, 1, 2, 2, 2], return 1

Challenge

O(n) time and O(1) extra space

.

链接地址

http://www.lintcode.com/en/problem/majority-number/

解法

    int majorityNumber(vector<int> nums) {
// write your code here
int ret, num = 0;
for (int i = 0; i < nums.size(); i++) {
if (num == 0) {
ret = nums[i];
num++;
} else {
if (nums[i] == ret) {
num++;
} else {
num--;
}
}
}
return ret;
}

算法解释

一个思路。同一时候删除两个不同的数。那么结果不会变

最新文章

  1. 打造AngularJs2.0开发环境
  2. Python基础(5)--字典
  3. RESTful API的设计与开发
  4. 关于HTML条件注释你可能不知道的一些事儿
  5. Calling a C++ dll with unsigned char* parameters
  6. 引用iscroll的一个封装方法
  7. 初探原生js根据json数据动态创建table
  8. Selenium Web 自动化 - 项目持续集成(进阶)
  9. Javacript的变量和输出
  10. js&#39;初学笔记
  11. HTML 中有用的字符实体
  12. Unable to resolve target &#39;android-XX&#39; 类似的错误。
  13. ambari 安装HDP3.0.1后,启动服务的问题记录
  14. jsp页面跳转页面的几中方式(同步更新)
  15. (后台)org.apache.catalina.connector.ClientAbortException: null
  16. 类变量、实例变量--python
  17. SpringBoot+Mybatis多模块(module)项目搭建教程
  18. GO开发环境搭建
  19. Vue小案例 之 商品管理------添加商品
  20. Java 8新特性之Date/Time(八恶人-4)

热门文章

  1. win7 64位旗舰版下载
  2. webpack 环境搭建+实现热更新
  3. Luogu【P1725】琪露诺(单调队列,DP)
  4. scrapy之小试身手
  5. 关于cookie使用的一些问题
  6. C语言第三题
  7. mybatis如何传入一个list参数
  8. 【CF173B】Chamber of Secrets(二分图,最短路)
  9. 转 Python爬虫入门五之URLError异常处理
  10. Notepad++中常用的插件【转】