原创

[百战LeetCode][34.数组中只出现一次的两个数字]


==========================努力奋斗财源广进==========================

一、算法题目

一个整型数组里除了两个数字只出现一次,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。 数据范围:数组长度2≤n≤1000,数组中每个数的大小0<val≤1000000 要求:空间复杂度 O(1),时间复杂度 O(n)

提示:输出时按非降序排列。

1、我的题解

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param array int整型一维数组 
     * @return int整型一维数组
     */
    public int[] FindNumsAppearOnce (int[] array) {
        // write code here

        int[] res = new int[2];
        HashMap<Integer, Object> set = new HashMap<>();
        int length = array.length;
        for(int i = 0; i < length; i++)
        {
            if(set.containsKey(array[i]))
            {
                set.remove(array[i],null);
            }
            else
            {
                set.put(array[i],null);
            }
        }
        int i = 0;
        for(Integer num:set.keySet())
        {
            res[i++] = num;
        }
        return res;
    }
}

2、优秀题解

//待定

三、解法心得

四、自我监督

评论区记录复习记录

  • 作者:北斗七点半联系作者
  • 发表时间:2022-12-06 23:06
  • 版权声明:禁止转载
  • 非公众号转发
  • 评论