Missing Number

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?

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int len = nums.size();
        if (len==1) {
            return nums[0]==1?0:1;
        }
        sort(nums.begin(), nums.end());
        if (nums[0]!=0) {
            return 0;
        }
        for (int i=1; i<len; i++) {
            if (i==0&&nums[i]!=0) {
                return 0;
            }
            if (nums[i]!=nums[i-1]+1) {
                return nums[i]-1;
            }
        }
        return nums[len-1]+1;
    }
};

第二种解法:
```c++
class Solution {
public:
int missingNumber(vector& nums) {
int result = nums.size();
int i=0;

    for(int num:nums){
        result ^= num;
        result ^= i;
        i++;
    }

    return result;
}

};
```

2017/11/30

Given an integer array, find three numbers whose product is maximum and output the maximum product.

class Solution {
public:
    int maximumProduct(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int len = nums.size();
        int tmp1 = nums[len-1]*nums[len-2]*nums[len-3];
        int tmp2 = nums[len-1]*nums[0]*nums[1];
        return max(tmp1, tmp2);
    }
};

主要考虑含有负数的情况。

2017/11/30 posted in  leetcode

Two Sum II - Input array is sorted

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution and you may not use the same element twice.

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        int str = 0;
        int end = numbers.size()-1;
        while (numbers[str]+numbers[end]!=target) {
            if (numbers[str]+numbers[end]>target) {
                end--;
            }else{
                str++;
            }
        }
        return vector<int>({str+1,end+1});
    }
};
2017/11/30 posted in  leetcode

Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges.求数组中,连续数字的区间。

class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        
        vector<string> result;
        if(nums.size()==0) return result;
        
        int end = nums[0]+1;
        int total = 0;
        if (nums.size()==1) {
            result.push_back(to_string(nums[0]));
            return result;
        }
        nums.push_back(INT_MAX); //充当哨兵
        for (int i=1; i<nums.size(); i++) {
            if (nums[i]==end) {
                end++;
                total++;
                continue;
            }else{
                end = nums[i]+1;
                if (total==0) {
                    result.push_back(to_string(nums[i-1]));
                }else{
                    string tmp = to_string(nums[i-1-total])+"->"+to_string(nums[i-1]);
                    result.push_back(tmp);
                }
                total = 0;
            }
        }
        return result;
    }
};
2017/11/30 posted in  leetcode

Majority Element

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int total = 1;
        int tmp = 1;
        int result = nums[0];
        if (nums.size()==1) {
            return result;
        }
        nums.push_back(INT_MAX); //在最后增加一个边界哨兵
        for (int i=1; i<=nums.size(); i++) {
            if (nums[i]==nums[i-1]) {
                tmp++;
            }else{
                result = tmp>total ? nums[i-1]:result;
                total = max(tmp, total);
                tmp = 1;
            }
        }
        
        return result;
    }
};
2017/11/30 posted in  leetcode

Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int tmp = INT_MIN;
        int log = 0;
        for (int i =0; i<nums.size(); i++) {
            if (tmp!=nums[i]) {
                tmp = nums[i];
            }else{
                log++;
            }
            nums[i-log] = nums[i];
        }
        
        return nums.size()-log;
    }
};

2017/11/30