LeetCode (I)

Integer to Roman

  Total Accepted: 2872  Total Submissions: 9293 My Submissions

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

class Solution {
public:
    string intToRoman(int num) {
        string ans;
        int t = num / 1000;
        for (int i = 0; i < t; ++i) ans += "M";
        t = num % 1000 / 100;
        if (t == 9) {
            ans += "CM";
        } else if (t >= 5) {
            ans += "D";
            for (int i = 0; i < t - 5; ++i) ans += "C";
        } else if (t == 4) {
            ans += "CD";
        } else {
            for (int i = 0; i < t; ++i) ans += "C";
        }
        t = num % 100 / 10;
        if (t == 9) {
            ans += "XC";
        } else if (t >= 5) {
            ans += "L";
            for (int i = 0; i < t - 5; ++i) ans += "X";
        } else if (t == 4) {
            ans += "XL";
        } else {
            for (int i = 0; i < t; ++i) ans += "X";
        }
        t = num % 10;
        if (t == 9) {
            ans += "IX";
        } else if (t >= 5) {
            ans += "V";
            for (int i = 0; i < t - 5; ++i) ans += "I";
        } else if (t == 4) {
            ans += "IV";
        } else {
            for (int i = 0; i < t; ++i) ans += "I";
        }
        return ans;
    }
};






Implement strStr()

  Total Accepted: 3610  Total Submissions: 18268 My Submissions

Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

class Solution {
public:
    char *strStr(char *haystack, char *needle) {
        return strstr(haystack, needle);    // should implement KMP
    }
};







Insertion Sort List

  Total Accepted: 3011  Total Submissions: 11904 My Submissions

Sort a linked list using insertion sort.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *insertionSortList(ListNode *head) {
        ListNode *root = new ListNode(INT_MIN);
        root->next = head;
        ListNode *p = head, *q = root;
        while (p != NULL) {
            ListNode *s = root->next, *t = root;
            while (s != p && s->val < p->val)
                s = s->next, t = t->next;
            if (s != p) {
                q->next = p->next;
                p->next = s;
                t->next = p;
                p = q->next;
            } else {
                p = p->next, q = q->next;
            }
        }
        ListNode *ans = root->next;
        delete root;
        return ans;
    }
};







Insert Interval

  Total Accepted: 2984  Total Submissions: 14653 My Submissions

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

You may assume that the intervals were initially sorted according to their start times.

Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10]

/**
 * 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> insert(vector<Interval> &intervals, Interval newInterval) {
        vector<Interval> ans;
        int start = newInterval.start, end = newInterval.end;
        const int n = intervals.size();
        int i = 0;
        while (i < n && intervals[i].end < newInterval.start) ans.push_back(intervals[i++]);
        if (i < n) start = min(start, intervals[i].start);
        while (i < n && intervals[i].start <= newInterval.end)
            ++i;
        if (i > 0) end = max(end, intervals[i - 1].end);
        ans.push_back(Interval(start, end));
        while (i < n) ans.push_back(intervals[i++]);
        return ans;
    }
};






Interleaving String

  Total Accepted: 2941  Total Submissions: 16683 My Submissions

Given s1s2s3, find whether s3 is formed by the interleaving of s1 and s2.

For example,
Given:
s1 = "aabcc",
s2 = "dbbca",

When s3 = "aadbbcbcac", return true.
When s3 = "aadbbbaccc", return false.

class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        if (s1.length() + s2.length() != s3.length()) return false;
        set<pair<int, int> > hash;
        queue<pair<int, int> > s;
        s.push(make_pair(0, 0));
        hash.insert(make_pair(0, 0));
        int i = 0;
        while (s3[i]) {
            int n = s.size();
            if (n == 0) return false;
            while (n--) {
                pair<int, int> p = s.front();
                s.pop();
                int a = p.first, b = p.second;
                if (s1[a] == s3[i] && hash.find(make_pair(a + 1, b)) == hash.end()) {
                    s.push(make_pair(a + 1, b));
                    hash.insert(make_pair(a + 1, b));
                }
                if (s2[b] == s3[i] && hash.find(make_pair(a, b + 1)) == hash.end()) {
                    s.push(make_pair(a, b + 1));
                    hash.insert(make_pair(a, b + 1));
                }
            }
            ++i;
        }
        return (s.size() > 0);
    }
};







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值