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().
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
}
};
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;
}
};
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;
}
};
Given s1, s2, s3, 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);
}
};