Java for LeetCode 229 Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.
解题思路:
《编程之美》寻找发帖水王的原题,两次遍历,一次遍历查找可能出现次数超过nums.length/3的数字,(出现三次不同的数即抛弃),第二次验证即可。
JAVA实现如下:
public List<Integer> majorityElement(int[] nums) {
        		List<Integer> list = new ArrayList<Integer>();
		if (nums == null || nums.length == 0)
			return list;
		int left = nums[0], right=nums[0];
		int count1 = 1, count2 = 0;
		for (int i = 1; i < nums.length; i++) {
			if (nums[i] == left)
				count1++;
			else if(right==left){
				right=nums[i];
				count2=1;
			}
			else if(right==nums[i])
				count2++;
			else if(count1==0){
				left=nums[i];
				count1=1;
			}
			else if(count2==0){
				right=nums[i];
				count2=1;
			}
			else{
				count2--;
				count1--;
			}
		}
		count1=0;count2=0;
		for(int i=0;i<nums.length;i++){
			if(nums[i]==left)
				count1++;
			else if(nums[i]==right)
				count2++;
		}
		if(count1>nums.length/3)
			list.add(left);
		if(count2>nums.length/3)
			list.add(right);
		return list;
    }
Java for LeetCode 229 Majority Element II的更多相关文章
- [LeetCode] 229. Majority Element II  多数元素 II
		Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ... 
- leetcode 229 Majority Element II
		这题用到的基本算法是Boyer–Moore majority vote algorithm wiki里有示例代码 1 import java.util.*; 2 public class Majori ... 
- LeetCode 229. Majority Element II (众数之二)
		Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ... 
- leetcode 229. Majority Element II(多数投票算法)
		就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int& ... 
- (medium)LeetCode  229.Majority Element II
		Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ... 
- leetcode 169. Majority Element 、229. Majority Element II
		169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ... 
- 【LeetCode】229. Majority Element II
		Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ... 
- 【刷题-LeetCode】229. Majority Element II
		Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ... 
- LeetCode OJ 229. Majority Element II
		Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ... 
随机推荐
- 分词工具ICTCLAS5.0使用心得
			接触自然语言处理有一年多了,最基本的一些自然是分词,词性标注,命名实体识别之类的知识,有些应用知道原理是一回事,自己动手做起来又是另外一回事了.最近又开始重操旧业:分词.分词最著名的自然就是中科院的分 ... 
- 【AngularJS】—— 9 自定义过滤器
			AngularJS另一个特点就是提供了过滤器,可以通过操作UNIX下管道的方式,操作数据结果. 通过使用管道,可以便于双向的数据绑定中视图的展现. 过滤器在处理过程中,将数据变成新的格式,而且可以使用 ... 
- 微信封号浪潮再起 A货假代购还能在朋友圈泛滥多久?
			你的微信朋友圈是不是很活跃?是不是被很多所谓的名品所包围?没错,这些很多都是A货或假代购的伎俩.如果xmyanke的微信朋友圈出现这些东东,我就会直接屏蔽他的朋友圈权限.具体方法是:打开他的微信详细资 ... 
- jQuery学习:用按键移动方块
			<!doctype html> <html> <head> <meta charset="utf-8"> <style typ ... 
- Effective Java 读书笔记之二 对于所有对象都通用的方法
			尽管Object是一个具体的类,但设计它主要是为了扩展.它的所有非final方法都有明确的通用约定.任何一个类在override时,必须遵守这些通用约定. 一.覆盖equals时请遵守通用的约定 1. ... 
- 快速入门SaltStack
			导读 SaltStack是基于Python开发的一套C/S架构配置管理工具(功能不仅仅是配置管理,如使用salt-cloud配置AWS EC2实例),它的底层使用ZeroMQ消息队列pub/sub方式 ... 
- 网络之AFNetsorking
			AFNetsorking作为功能全面的网络第三方,既通俗好用又与时俱进-及时的更新使用了NSURLSession,不得不爱. AFNetsorking使用: 1,AFNetsorking GET请求 ... 
- [转载]JavaEE学习篇之——JDBC详解
			原文链接:http://blog.csdn.net/jiangwei0910410003/article/details/26164629 目录1.摘要2.JDBC的使用步骤 1.注册驱动 只做一次 ... 
- Android实用代码模块集锦
			1. 精确获取屏幕尺寸(例如:3.5.4.0.5.0寸屏幕) 1 2 3 4 5 6 public static double getScreenPhysicalSize(Activity ctx) ... 
- LED notification in Android device
			Code can control the LED notification in Android device, using android.app.Notification: 1 2 3 4 5 6 ... 
