国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 荃湾区| 新蔡县| 渑池县| 乡宁县| 香港| 盱眙县| 桐城市| 石阡县| 天气| 稻城县| 康平县| 牙克石市| 达拉特旗| 小金县| 巴塘县| 南安市| 驻马店市| 桑植县| 淮安市| 仪陇县| 武城县| 青浦区| 辛集市| 翁源县| 望江县| 阳朔县| 江永县| 保德县| 嘉定区| 电白县| 浮梁县| 遂溪县| 汝州市| 临桂县| 东至县| 涞源县| 雷州市| 阿图什市| 淅川县| 神农架林区| 开封县|