国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

[LeetCode] Single Number III ( a New Questions Added today)

2019-11-15 01:09:50
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
[LeetCode] Single Number III ( a New Questions Added today)

Given an array of numbersnums, 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:

Givennums = [1, 2, 1, 3, 2, 5], return[3, 5].

Note:

  1. The order of the result is not important. So in the above example,[5, 3]is also correct.
  2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

這是今天剛加上去的一道題。

個(gè)人覺(jué)得這道題和之前single number的兩道差不多。依舊用hashset即可做出。

唯一要注意的就是最后return的時(shí)候不能直接return hashset。為了偷懶我直接弄了個(gè)新的int[]。

代碼如下。~

public class Solution {    public int[] singleNumber(int[] nums) {        if(nums.length==2&&nums[0]!=nums[1]){            return nums;        }          HashSet<Integer> store=new HashSet<Integer>();        HashSet<Integer> result=new HashSet<Integer>();        for(int i=0;i<nums.length;i++){            if(!result.add(nums[i])){                result.remove(nums[i]);                store.add(nums[i]);            }else{                if(store.contains(nums[i])){                    result.remove(nums[i]);                }            }        }        int[] PRint=new int[2];        print[0]=result.iterator().next();        result.remove(result.iterator().next());        print[1]=result.iterator().next();        return print;            }}


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 芜湖市| 磐石市| 潮安县| 大邑县| 田东县| 盐津县| 临漳县| 淮南市| 兴山县| 乌兰县| 子洲县| 长葛市| 鹤峰县| 辽阳县| 元谋县| 富平县| 佛冈县| 岳普湖县| 台山市| 长沙市| 左云县| 托里县| 敖汉旗| 钦州市| 赣州市| 盈江县| 稷山县| 晋州市| 文登市| 永宁县| 清新县| 郓城县| 京山县| 香格里拉县| 永济市| 西贡区| 江陵县| 姜堰市| 碌曲县| 万全县| 奎屯市|