8.3 细节实现

边备考做点简单滴

to do

8.3 细节实现_第1张图片

1] Reverse Integer

want to check if overflowed, reduce the bigger number INT_MAX for comparison

  • version 1: note using int sign instead of bool
    int reverse(int x) {
        int sign = x<0? -1 : 1;
        x = abs(x);
        int r = 0;
        for (; x; x/=10) {
            if (r > INT_MAX/10 || r*10 > INT_MAX - x%10) return 0;
            r = r*10 + x%10;
        }
        return sign*r;
    }
  • version 2: checking if overflowed at each step
    int reverse(int x) {
        int r = 0;
        for (; x; x/=10) {
            int tail = x%10;
            int newr = r*10 + tail;
            if ((newr-tail)/10 != r) return 0;
            r = newr;
        }
        return r;
    }

2] Palindrome Number

    bool isPalindrome(int x) {
        if (x<0) return false;
        
        int divisor = 1;
        while (x/divisor>=10) divisor *= 10;
        
        while (x>0) {
            int first = x/divisor;
            int last = x%10;
            if (first!=last) return false;
            x = x % divisor /10;
            divisor /= 100;
        }
        return true;
    }
  • here is my failed one
    cannot handle 100021 F, 10022001 T at the same time
    Tho if this function is used a lot, can implement binary search and hardcode the vector powOf10s to aoivd the linear look up of the first divisor.
class Solution {
public:
    // if n < 0, return 1 which will not function;
    // binary search to improve
    int  findPowOf10(int n, vector& powOf10s) {
        for (int i=0; i powOf10s;   
        for (int i=0;; ++i) {
            int upperBound = pow(10, i+1)-1;
            if (upperBound < 0) break;
            powOf10s.push_back(upperBound);
        }
        
        while (x>0) {
            int last = x%10;
            int divisor = findPowOf10(x, powOf10s); 
            int first = x / divisor;
            if (first != last) return false;
            x = x % divisor /10;
        }
        return true;
    }
};

3] Insert Interval

ehh why running error? runs fine on vm

/**
 * 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: 
    struct info {
      int ct;
      bool gap;
      info(): ct(0), gap(true){};
      info(int c, bool g): ct(c), gap(g){};
    };
    
    // count number of occurrences of intervals, before the section that contains val
    info sectionCtsBefore(vector& intervals, int val) {
        int lastb = INT_MIN;
        int i=0;
        for (; i& intervals, int starti, int endi) {
        if (starti>endi || starti>=intervals.size()) return;
        intervals.erase(intervals.begin()+starti, intervals.begin()+endi+1);
        cout<<"erased\n";
    }
    
    vector insert(vector& intervals, Interval newInterval) {
        if (intervals.empty()) {
            intervals.push_back(newInterval);
            return intervals;
        }
        int fst = newInterval.start;
        int snd = newInterval.end;
        info fsti = sectionCtsBefore(intervals, fst);
        cout<<"fsti: "<

这个也run code runtime error,不信╭(╯^╰)╮.but accp

class Solution {
public:
    vector insert(vector& intervals, Interval newInterval) {
        int fst = newInterval.start;
        int snd = newInterval.end;
        for (auto it = intervals.begin(); itstart) {
                intervals.insert(it, Interval(fst, snd));
                return intervals;
            } else if (fst <= it->end) {
                fst = min(fst, it->start);
                snd = max(snd, it->end);
                it = intervals.erase(it);
            } else {
                ++it;
            }
        }
        intervals.insert(intervals.end(), Interval(fst, snd));
        return intervals;
    }
};

你可能感兴趣的:(8.3 细节实现)