zoukankan      html  css  js  c++  java
  • Lonely Integer

    There are N integers in an array A. All but one integer occur in pairs. Your task is to find the number that occurs only once.

    Sample Input:2

    3
    1 1 2
    

    Sample Output:2

    2

    Sample Input:3

    5
    0 0 1 2 1
    

    Sample Output:3

    2
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Scanner;
    
    public class Solution {
       static int lonelyinteger(int[] a) {
    	   HashMap<Integer,Integer> map=new HashMap();
           for(int i=0;i<a.length;i++){
        	
        	   if(map.containsKey(a[i])==false){
        		   map.put(a[i], 1);    		   
        	   }
        	   else
        		   map.put(a[i], 2);     	   
           }
    //       for(Map.EntrySet entry : map)
    //       {
    //       system.out.println(entry.getkey()+ : + entry.getValue());
    //       }
           for(Integer i : map.keySet())
           {
           if( map.get(i) ==1)
        	   return i.intValue();
        	   }
    //       }
          return 0;
        }
       public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int res;
            
            int _a_size = Integer.parseInt(in.nextLine());
            int[] _a = new int[_a_size];
            int _a_item;
            String next = in.nextLine();
            String[] next_split = next.split(" ");
            
            for(int _a_i = 0; _a_i < _a_size; _a_i++) {
                _a_item = Integer.parseInt(next_split[_a_i]);
                _a[_a_i] = _a_item;
            }
            
            res = lonelyinteger(_a);
            System.out.println(res);
            
        }
    }
    

      

  • 相关阅读:
    sql mysql
    sql练习
    re正则表达式
    《大道至简 第二章》读后感
    读《大道至简:编程的精义》有感
    爬取汽车之家新闻图片的python爬虫代码
    Linux系统封装成iso文件
    使用Zabbix进行IPMI监控
    zabbix支持的主要监控方式
    python虚拟环境搭建
  • 原文地址:https://www.cnblogs.com/gaoxiangde/p/4326724.html
Copyright © 2011-2022 走看看