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

题目

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.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

代码

import java.util.Hashtable;
import java.util.Iterator;
public class Solution {
    public int majorityElement(int[] num) {
          Hashtable<Integer,Integer> rightList = new Hashtable<Integer,Integer>();
		   for(int i=0;i<num.length;i++)
		      {
			  if(rightList.get(num[i])==null){
				  rightList.put(num[i], 1);
			  }
			  else{
				  rightList.put(num[i], rightList.get(num[i])+1);
			  }
		     }
		   int result_value=0;
		   int result_key=0;
		   for(Iterator itr = rightList.keySet().iterator(); itr.hasNext();){
			  		   int key = (Integer) itr.next();
			  		   if(rightList.get(key)>result_value){
			  			  result_key=key;
			  			  result_value=rightList.get(key);
			  		   }
		   }
		   return result_key;
    }
}

代码下载:https://github.com/jimenbian/GarvinLeetCode

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

时间: 2024-09-18 12:25:28

【LeetCode从零单排】No.169 Majority Element(hashmap用法)的相关文章

[LeetCode]169.Majority Element

[题目] 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. [分析] 思路是计算数组前一半出现元素的频率,

leetcode 169 Majority Element 冰山查询

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. 思路: Find k different element

【LeetCode从零单排】No26.Remove Duplicates from Sorted Array

题目      题目要求:去除sort int数组中的重复项.      Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory

【LeetCode从零单排】No198.House Robber &amp;amp;&amp;amp;No91.Decode Ways&amp;amp;&amp;amp;139 word break(动态规划典型应用)

1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security

【LeetCode从零单排】No70.ClimbingStairs

题目           爬楼梯问题,这是一道很有趣的问题.首先看题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 这道题一共有三个方法实现(我想到的): 1.递归(对应代码climbStairs) 如果楼梯有

【LeetCode从零单排】No121 Best Time to Buy and Sell Stock

题目 Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. 又

【LeetCode从零单排】No58.Length of Last Word

题目 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space

【LeetCode从零单排】No.9 Palindrome Number

题目       这道题是迄今为止最快通过的一道题,改了两次就过了,runtime一般(中等偏下,这点不太满意).Palindrome就是判断一个整数是否对称. Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints:Could negative integers be palindromes? (ie, -1) If you are

【LeetCode从零单排】No.135Candy(双向动态规划)

1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more can