Detail Implementation

7. Reverse Integer

class Solution {
public:
    int reverse(int x) {
        int sign = 1;
        if (x == 0) return 0;
        if (x == INT_MIN) return 0;
        if (x  INT_MAX / 10) return 0;
            result = result * 10 + rem;
            rem = x % 10;
            x = x / 10;
        }
        if (rem != 0) {
            if (result > INT_MAX / 10) return 0;
            result = result * 10 + rem;
        }
        return result*sign;
    }
};

9. Palindrome Number

class Solution {
public:
    bool isPalindrome(int x) {
        if (x < 0) return false;
        int val = 0;
        int temp = x;
        while (temp != 0) {
            temp = temp / 10;
            val++;
        }
        for (int i = 0; i < val/2; ++i) {
            int temp_1 = x % 10;
            int temp_2 = (x / (int)pow(10, val-1-2*i)) % 10;
            if (temp_1 != temp_2) return false;
            x = x / 10;
        }
        return true;
    }
};

57. Insert Interval

class Solution {
public:
    vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
        vector<Interval>::iterator it = intervals.begin();
        while (it != intervals.end()) {
            if (newInterval.end < it->start) {
                intervals.insert(it, newInterval);
                return intervals;
            } else if (newInterval.start > it->end) {
                it++;
                continue;
            } else {
                newInterval.start = min(newInterval.start, it->start);
                newInterval.end = max(newInterval.end, it->end);
                it = intervals.erase(it);
            }
        }
        intervals.insert(intervals.end(), newInterval);
        return intervals;
    }
};

56. Merge Intervals

/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
class Solution {
public:
    vector<Interval> merge(vector<Interval>& intervals) {
        int size = intervals.size();
        vector<Interval> result;
        for (int i = 0; i < size; ++i) {
            result = insert(result, intervals[i]);
        }
        return result;
    }
private:
    vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
        vector<Interval>::iterator it = intervals.begin();
        while (it != intervals.end()) {
            if (newInterval.end < it->start) {
                intervals.insert(it, newInterval);
                return intervals;
            } else if (newInterval.start > it->end) {
                it++;
                continue;
            } else {
                newInterval.start = min(newInterval.start, it->start);
                newInterval.end = max(newInterval.end, it->end);
                it = intervals.erase(it);
            }
        }
        intervals.insert(intervals.end(), newInterval);
        return intervals;
    }
};






									

Array

414. Third Maximum Number

class Solution {
public:
    int thirdMax(vector<int>& nums) {
        long int first = LONG_MIN, second = LONG_MIN, third = LONG_MIN;
        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] == first || nums[i] == second || nums[i] == third) continue;
            if (nums[i] > first) {
                third = second;
                second = first;
                first = nums[i];
            }
            else if (nums[i] > second) {
                third = second;
                second = nums[i];
            }
            else if (nums[i] > third) {
                third = nums[i];
            }
        }
        return third == LONG_MIN?first:third;
    }
};

88. Merge Sorted Array

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        if (m == 0) nums1 = nums2;
        int idx = m + n - 1;
        while (m > 0 && n > 0) {
            if (nums2[n-1] < nums1[m-1]) {
                nums1[idx] = nums1[m-1];
                m--;
            }
            else {
                nums1[idx] = nums2[n-1];
                n--;
            }
            idx--;
        }
        while (n > 0) {
            nums1[idx] = nums2[n-1];
            n--;
            idx--;
        } 
    }
};

27. Remove Element

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int size = nums.size();
        int i = 0;
        int j = 0;
        for (i = 0; i < size; ++i) {
            if (nums[i] == val) continue;
            nums[j] = nums[i];
            j++;
        }
        return j;
    }
};