Majority Number
2024-08-26 06:56:10
题目描写叙述
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;
}
算法解释
一个思路。同一时候删除两个不同的数。那么结果不会变
最新文章
- 打造AngularJs2.0开发环境
- Python基础(5)--字典
- RESTful API的设计与开发
- 关于HTML条件注释你可能不知道的一些事儿
- Calling a C++ dll with unsigned char* parameters
- 引用iscroll的一个封装方法
- 初探原生js根据json数据动态创建table
- Selenium Web 自动化 - 项目持续集成(进阶)
- Javacript的变量和输出
- js&#39;初学笔记
- HTML 中有用的字符实体
- Unable to resolve target &#39;android-XX&#39; 类似的错误。
- ambari 安装HDP3.0.1后,启动服务的问题记录
- jsp页面跳转页面的几中方式(同步更新)
- (后台)org.apache.catalina.connector.ClientAbortException: null
- 类变量、实例变量--python
- SpringBoot+Mybatis多模块(module)项目搭建教程
- GO开发环境搭建
- Vue小案例 之 商品管理------添加商品
- Java 8新特性之Date/Time(八恶人-4)