标签:
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n
, find the one that is missing from the array.
For example, Given nums = [0, 1, 3]
return 2
.
Note: Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
思路:先排序再查找。
代码:
1 class Solution { 2 public: 3 int missingNumber(vector<int>& nums) { 4 int i=0,res=1; 5 sort(nums.begin(),nums.end()); 6 while(i<nums.size()&&(res&1)^(nums[i]&1)){ 7 res=nums[i++]; 8 } 9 return i<nums.size()?nums[i]-1:i; 10 } 11 };
标签:
原文地址:http://www.cnblogs.com/Deribs4/p/5721412.html