题解 | #数组中只出现一次的两个数字#

数组中只出现一次的两个数字

http://www.nowcoder.com/practice/389fc1c3d3be4479a154f63f495abff8

两个集合查重

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param array int整型一维数组 
     * @return int整型一维数组
     */
    public int[] FindNumsAppearOnce (int[] array) {
        // write code here
        int[] we =new int[2];
        int w=0;
        Set<Integer> op = new HashSet<>();
        Set<Integer> you = new HashSet<>();
        for(int i : array){
           
            if(!you.add(i)){
                op.add(i);
            }
        }
        for(int i : array){
            if(!op.contains(i)){
                we[w] = i;
                w++;
            }
        }
        if(we[0] > we[1]){
            w=we[0];
            we[0]=we[1];
            we[1]=w;
        }
        return we;
    }
}
全部评论

相关推荐

MomonKa:我拿Java简历投了pdd前端也给我简历过筛了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务