剑指offer-40-数组中只出现一次的数字
数组中只出现一次的数字_牛客网
https://www.nowcoder.com/practice/e02fdb54d7524710a7d664d082bb7811?tpId=13&tqId=11193&tPage=2&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
简单方法
可以先用最简单的HashMap的方法来做,这样主要是为了练习Map的用法。
import java.util.HashMap; public class Solution { public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) { //哈希算法 HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i=0; i < array.length; i++){ if(map.containsKey(array[i])) map.put(array[i],2); else map.put(array[i],1); } int count = 0; for(int i=0; i < array.length; i++){ if(map.get(array[i]) == 1){ if(count == 0){ num1[0] = array[i]; count++;
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
小白刷剑指offer 文章被收录于专栏
跟着小白一起刷剑指offer,通过讨论加深印象吧~ 没有人不学习就能够掌握知识,知识就是需要学习的~