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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s