/*****************************************************问题描述*************************************************
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
给定一个非空数值数组,数组中有一个数出现一次,其它数出现2次,找出出现一次的那个数
/*****************************************************我的解答*************************************************
/**
* @param {number[]} nums
* @return {number}
*/
var singleNumber = function(nums) {
nums.sort();
for(var index = nums.length - 1; index >= 1; )
{
if(nums[index] == nums[index - 1])
{
nums.splice(index - 1,2);
index -= 2;
}
else
{
return nums[index];
}
}
return nums[0];
};
console.log(singleNumber([4,1,2,1,2]));
leetCode刷题记录23_136_Single Number
最新推荐文章于 2023-09-07 10:47:13 发布