記錄陣列中出現的數字並統計個數
public static void main(String args[]){
int [] arr = {1,2,2,2,3,3,4,4,4,4};
testaaa(arr);
}
public static void testaaa(int [] str) {
Map<Integer, Integer> map=new HashMap<>();
for (int i = 0; i < str.length; i++) {
if(map.containsKey(str[i])) {
map.put(str[i], map.get(str[i])+1);//原來有則+1
}else {
map.put(str[i], 1); //原來沒有則放一個
}
}
for (Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("數字"+entry.getKey()+"出現了"+entry.getValue()+"次");
}
}
}
相關推薦
記錄陣列中出現的數字並統計個數
public class t02 {public static void main(String args[]){int [] arr = {1,2,2,2,3,3,4,4,4,4};testaaa(arr);}public static void testaaa(int [] str) { Map&l
找出陣列中出現一次兩個數,其他數字都出現偶數次
{ unsigned int flag =1; while(flag) //求異或結果,最低的為1的二進位制位,根據此位是否為1,將元素分為兩組,兩個不同的元素,在此位必然,一個為1,一個為0 { if(flag&sum) break;
找出陣列中不同元素並統計相同元素的個數
public void getDifference(int[] arr) { Dictionary<int, int> dic= new Dictionary<int, int>();
劍指offer-陣列中出現數字超過一半得數字
28.陣列中出現數字超過一半的數字 題目描述 陣列中有一個數字出現的次數超過陣列長度的一半,請找出這個數字。例如輸入一個長度為9的陣列{1,2,3,2,2,2,5,4,2}。由於數字2在陣列中出現了5次,超過陣列長度的一半,因此輸出2。如果不存在則輸出0。 解題思路:若陣列中有數字出現的
劍指offer之統計一個數字在排序陣列中出現的次數。
1.題目描述 統計一個數字在排序陣列中出現的次數。 2.問題分析 方法一: 因為是排序的陣列,首先在陣列中找到第一個值為k的位置begin,之後從begin找下一個不等於值k的位置end,則,次數為end - begin。 例如:1 2 2 3 3 3 5,k = 3
【Java】 劍指offer(40) 最小的k個數 《劍指Offer》Java實現合集 劍指offer(39) 陣列中出現次數超過一半的數字 《劍指Offer》Java實現合集
本文參考自《劍指offer》一書,程式碼採用Java語言。 更多:《劍指Offer》Java實現合集 題目 輸入n個整數,找出其中最小的k個數。例如輸入4、5、1、6、2、7、3、8這8個數字,則最小的4個數字是1、2、3、4。 思路 思路一:同劍指offer(39
T35:統計一個數字在排序陣列中出現的次數
思路很簡單 :就是利用二分法尋找第一個K出現的地方和最後一個K出現的地方 public class GetNumberOfK1 { public int GetNumberOfK(int [] array , int k) { int len=array.length;
統計一個數字在排序陣列中出現的次數。(牛客劍指offer)
題目描述: 統計一個數字在排序陣列中出現的次數 class Solution { public: int GetNumberOfK(vector<int> data ,int k) { } }; Solu
統計陣列中出現次數超過一半的數字
題目描述 陣列中有一個數字出現的次數超過陣列長度的一半,請找出這個數字。例如輸入一個長度為9的陣列{1,2,3,2,2,2,5,4,2}。由於數字2在陣列中出現了5次,超過陣列長度的一半,因此輸出2。如
【劍指offer{25-30}】複雜連結串列的複製、字串的排列、陣列中出現次數超過一半的數字、最小的K個數、連續子陣列的最大和
複雜連結串列的複製、字串的排列、陣列中出現次數超過一半的數字、最小的K個數、連續子陣列的最大和 複雜連結串列的複製 題目描述 輸入一個複雜連結串列(每個節點中有節點值,以及兩個指標,一個指向下一個節
統計一個數字在排序陣列中出現的次數.
題目:統計一個數字在排序陣列中出現的次數。 思路:採用二分查詢的思想,尋找該數字第一次出現的下標和最後一次出現的下標,最後將兩個下標相減加1,便可得出出現的次數。具體的程式碼實現如下所示: public class Solution { public int Ge
統計陣列中出現次數最多的元素並輸出
實驗過程中遇到一個實際問題:需要統計出10次計數的值中出現最多的一個數,比如輸入34 35 35 35 34 35 35 35 34 33 十個數,要求最終輸出35.如果出現兩個數同樣多,則輸出兩個元素中較小的那一個(也可以是較大的那一個,但是必須確定是其中一種)。 程式碼
統計一個數字在排序陣列中出現的次數
一個整型數組裡除了兩個數字之外,其他的數字都出現了兩次。請寫程式找出這兩個只出現一次的數字。 * 1、把陣列從頭到尾做異或操作,出現兩次的數字會相互抵消,因為自己和自己異或就是0,剩下的就是出現1次的數字的異或 * 2、把最後得到的異或值第一個為1的位的位置 * 3、
【C語言】統計陣列中出現次數超過一半的數字
//統計陣列中出現次數超過一半的數字 #include <stdio.h> int Find(int *arr, int len) { int num = 0; //當
《牛客網劍指offer37題》統計一個數字在排序陣列中出現的次數
題目:統計一個數字在排序陣列中出現的次數 結果:牛客網程式設計通過 思路及程式: //中心思想,用二分法找到第一個等於K的陣列元素,然後向兩邊移動,直到找到不等於k的元素 int GetNumberOfK(vector<int> data ,int k
(4)統計一個數字,在排序陣列中出現的次數
題目描述:統計一個數字K,在排序陣列中出現的次數; 例如:陣列data{1,2,2,3,3,3,3,4} , k = 3 則其出現次數應該為4; 思路一: 順序掃描,若data[i] = k , 計數Number ++; 時間複雜度為O(n); 思路二: 耗費時間主要還
c++在陣列中查詢元素並返回位置下標,統計某元素出現次數程式
IDE是VS2013. 在陣列中查詢元素並返回位置下標,若找到元素則返回下標(多個相同元素,返回最後一個),沒找到返回-1; 統計某元素出現次數程式,沒出現返回0; #include "stdafx.h" #include<iostream> using nam
劍指offer之陣列中出現次數超過一半的數字
1.題目描述 陣列中有一個數字出現的次數超過陣列長度的一半,請找出這個數字。例如輸入一個長度為9的陣列{1,2,3,2,2,2,5,4,2}。由於數字2在陣列中出現了5次,超過陣列長度的一半,因此輸出2。如果不存在則輸出0。 2.問題分析 方法一:使用map,統計每個數字
牛客網——數字在排序陣列中出現的次數
題目描述 統計一個數字在排序陣列中出現的次數。 package 劍指offer; /** * Created by Administrator on 2018/10/28. */ public class 數字在排序陣列中出現的次數 { public stat
劍指offer -- 數字在排序陣列中出現的次數
題目描述 統計一個數字在排序陣列中出現的次數。 解題思路 因為題目給出的陣列是排序了的,所以我們想到了二分查詢。利用二分查詢找到一個數字在陣列中第一次出現的位置和最後出現的位置即可。 AC程式碼 public class Solution { public int