Single Number III

来源:互联网 发布:js奇偶数判断的代码 编辑:程序博客网 时间:2024/05/22 06:54

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

For example:

Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

思路:

(1)题意为给定一个整数数组,其中有两个元素仅仅出现一次,其余所有的元素都出现两次,请找出这个两个只出现一次的元素。

(2)这里我用一个数组遍历一次得出结果,希望大家如果有更简单的方法,留言我哦

public static int[] SingleNumber(int[] intgers) {int[] result = new int[2];int u=0;int[] arrays=new int[256];for (int i = 0; i < intgers.length; i++) {int j = intgers[i];if(arrays[j]!=0) {arrays[j]+=1;result[u]=j;u++;}elsearrays[j]=1;}return result;}