260. Single Number III

来源:互联网 发布:手机crm软件排名 编辑:程序博客网 时间:2024/03/29 19:29

题目

iven 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].

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

源码

ps:好像不太满足线性的要求,不过提交代码过了。

/** * @param {number[]} nums * @return {number[]} */var singleNumber = function(nums) {    var result=[];    if(nums.length==2){        return nums;    }    nums.sort();    j=0;    for(i=0;i<nums.length;i++){       if(nums[i]==nums[i+1]){            i++;       }        else{          result[j++]=nums[i];        }    }    return result;};
0 0