Algorithm

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;
    }
};






		
Advertisements

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;
    }
};











									

Stack

20. Valid Parentheses

class Solution {
public:
    bool isValid(string s) {
        stack<char> stk;
        int len = s.length();
        if (len == 0) return true;
        if (len == 1) return false;
        for (int i = 0; i < len; ++i) {
            if (s[i] == '(' || s[i] == '{' || s[i] == '[') stk.push(s[i]);
            else {
                if (stk.empty()) return false;
                else {
                    char tmp = stk.top();
                    if ((s[i] == ')' && tmp == '(') || (s[i] == '}' && tmp == '{') || (s[i] == ']' && tmp == '[')) stk.pop();
                    else return false;
                }
            }
        }
        if (stk.empty()) return true;
        return false;
    }
};

Hash Table

1. Two Sum

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> hashtable;
        for (int i = 0; i < nums.size(); ++i) {
            int res = target - nums[i];
            if (hashtable.count(res) == 0){
              hashtable[nums[i]] = i;  
            } 
            else {
                return {i, hashtable[res]};
            }
        }
        return {-1, -1};
    }
};

219. Contains Duplicate II

class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int, int> hashmap;
        for (int i = 0; i < nums.size(); ++i) {
            if(hashmap.count(nums[i]) != 0) {
                int ind = i - hashmap[nums[i]];
                if (ind <= k) return true;
                else hashmap[nums[i]] = i;
            }
            else hashmap[nums[i]] = i;
        }
        return false;
    }
};









									

String

1. Get Substring


string s;
string substring = s.substr(start, len);

No. 125 Valid Palindrome

class Solution {
public:
    bool isPalindrome(string s) {
        int start = 0, end = s.size()-1;
        while(start<end)
        {
            if(!isalnum(s[start])) ++start; // skip non alphanumerical characters
            else if(!isalnum(s[end])) --end;
            else if(tolower(s[start++]) != tolower(s[end--])) return false;
        }
        return true;
    }
};