c++ solution


  • 0
    W
    class Solution {
    public:
        int singleNonDuplicate(vector<int>& nums) {
        	if (nums.empty() || nums.size() % 2 == 0) return 0;
    		if (nums.size() == 1) return nums[0];    
    		int beg = 0; 
    		int end = nums.size() - 1;
    		int mid = nums.size() / 2;
    		while (mid > 0) {
    			//cout << "beg:" << beg << ", end:" << end << ", mid:" << mid << endl;
    			if (beg == end) return nums[beg];
    			if (nums[mid] == nums[mid+1]) mid -= 1;
    			if ((mid-beg+1) % 2 == 0) {beg = mid + 1;}
    			else {end = mid;} 
    			mid = (end-beg+1) / 2 + beg;
    		}
    		return nums[0];
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.