Skip to content

Commit a883834

Browse files
committed
Added the chapter Simulation
1 parent 2d3d3e3 commit a883834

37 files changed

Lines changed: 1365 additions & 0 deletions

‎cpp/SUMMARY.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,18 @@
157157
* [Word Break II](dp/word-break-ii.md)
158158
* [图](graph/README.md)
159159
* [Clone Graph](graph/clone-graph.md)
160+
* [模拟](simulation/README.md)
161+
* [Reverse Integer](simulation/reverse-integer.md)
162+
* [Palindrome Number](simulation/palindrome-number.md)
163+
* [Insert Interval](simulation/insert-interval.md)
164+
* [Merge Intervals](simulation/merge-intervals.md)
165+
* [Minimum Window Substring](simulation/minimum-window-substring.md)
166+
* [Multiply Strings](simulation/multiply-strings.md)
167+
* [Substring with Concatenation of All Words](simulation/substring-with-concatenation-of-all-words.md)
168+
* [Pascal's Triangle](simulation/pascal-s-triangle.md)
169+
* [Pascal's Triangle II](simulation/pascal-s-triangle-ii.md)
170+
* [Spiral Matrix](simulation/spiral-matrix-ii.md)
171+
* [ZigZag Conversion](simulation/zigzag-conversion.md)
172+
* [Divide Two Integers](simulation/divide-two-integers.md)
173+
* [Text Justification](simulation/text-justification.md)
174+
* [Max Points on a Line](simulation/max-points-on-a-line.md)

‎cpp/code/divide-two-integers-1.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Divide Two Integers
2+
// 时间复杂度O(logn),空间复杂度O(1)
3+
class Solution {
4+
public:
5+
int divide(int dividend, int divisor) {
6+
// 当 dividend = INT_MIN时,-dividend会溢出,所以用 long long
7+
long long a = dividend >= 0 ? dividend : -(long long)dividend;
8+
long long b = divisor >= 0 ? divisor : -(long long)divisor;
9+
10+
// 当 dividend = INT_MIN时,divisor = -1时,结果会溢出,所以用 long long
11+
long long result = 0;
12+
while (a >= b) {
13+
long long c = b;
14+
for (int i = 0; a >= c; ++i, c <<= 1) {
15+
a -= c;
16+
result += 1 << i;
17+
}
18+
}
19+
20+
return ((dividend^divisor) >> 31) ? (-result) : (result);
21+
}
22+
};

‎cpp/code/divide-two-integers-2.cpp‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Divide Two Integers
2+
// 时间复杂度O(logn),空间复杂度O(1)
3+
class Solution {
4+
public:
5+
int divide(int dividend, int divisor) {
6+
int result = 0; // 当 dividend = INT_MIN时,divisor = -1时,结果会溢出
7+
const bool sign = (dividend > 0 && divisor < 0) ||
8+
(dividend < 0 && divisor > 0); // 异号
9+
10+
// 当 dividend = INT_MIN时,-dividend会溢出,所以用 unsigned int
11+
unsigned int a = dividend >= 0 ? dividend : -dividend;
12+
unsigned int b = divisor >= 0 ? divisor : -divisor;
13+
14+
while (a >= b) {
15+
int multi = 1;
16+
unsigned int bb = b;
17+
while (a >= bb) {
18+
a -= bb;
19+
result += multi;
20+
21+
if (bb < INT_MAX >> 1) { // 防止溢出
22+
bb += bb;
23+
multi += multi;
24+
}
25+
}
26+
}
27+
if (sign) return -result;
28+
else return result;
29+
}
30+
};

‎cpp/code/insert-interval.cpp‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
struct Interval {
2+
int start;
3+
int end;
4+
Interval() : start(0), end(0) { }
5+
Interval(int s, int e) : start(s), end(e) { }
6+
};
7+
8+
// Insert Interval
9+
// 时间复杂度O(n),空间复杂度O(1)
10+
class Solution {
11+
public:
12+
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
13+
vector<Interval>::iterator it = intervals.begin();
14+
while (it != intervals.end()) {
15+
if (newInterval.end < it->start) {
16+
intervals.insert(it, newInterval);
17+
return intervals;
18+
} else if (newInterval.start > it->end) {
19+
it++;
20+
continue;
21+
} else {
22+
newInterval.start = min(newInterval.start, it->start);
23+
newInterval.end = max(newInterval.end, it->end);
24+
it = intervals.erase(it);
25+
}
26+
}
27+
intervals.insert(intervals.end(), newInterval);
28+
return intervals;
29+
}
30+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Max Points on a Line
2+
// 暴力枚举法,以边为中心,时间复杂度O(n^3),空间复杂度O(1)
3+
class Solution {
4+
public:
5+
int maxPoints(vector<Point> &points) {
6+
if (points.size() < 3) return points.size();
7+
int result = 0;
8+
9+
for (int i = 0; i < points.size() - 1; i++) {
10+
for (int j = i + 1; j < points.size(); j++) {
11+
int sign = 0;
12+
int a, b, c;
13+
if (points[i].x == points[j].x) sign = 1;
14+
else {
15+
a = points[j].x - points[i].x;
16+
b = points[j].y - points[i].y;
17+
c = a * points[i].y - b * points[i].x;
18+
}
19+
int count = 0;
20+
for (int k = 0; k < points.size(); k++) {
21+
if ((0 == sign && a * points[k].y == c + b * points[k].x) ||
22+
(1 == sign&&points[k].x == points[j].x))
23+
count++;
24+
}
25+
if (count > result) result = count;
26+
}
27+
}
28+
return result;
29+
}
30+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Max Points on a Line
2+
// 暴力枚举,以点为中心,时间复杂度O(n^2),空间复杂度O(n)
3+
class Solution {
4+
public:
5+
int maxPoints(vector<Point> &points) {
6+
if (points.size() < 3) return points.size();
7+
int result = 0;
8+
9+
unordered_map<double, int> slope_count;
10+
for (int i = 0; i < points.size()-1; i++) {
11+
slope_count.clear();
12+
int samePointNum = 0; // 与i重合的点
13+
int point_max = 1; // 和i共线的最大点数
14+
15+
for (int j = i + 1; j < points.size(); j++) {
16+
double slope; // 斜率
17+
if (points[i].x == points[j].x) {
18+
slope = std::numeric_limits<double>::infinity();
19+
if (points[i].y == points[j].y) {
20+
++ samePointNum;
21+
continue;
22+
}
23+
} else {
24+
slope = 1.0 * (points[i].y - points[j].y) /
25+
(points[i].x - points[j].x);
26+
}
27+
28+
int count = 0;
29+
if (slope_count.find(slope) != slope_count.end())
30+
count = ++slope_count[slope];
31+
else {
32+
count = 2;
33+
slope_count[slope] = 2;
34+
}
35+
36+
if (point_max < count) point_max = count;
37+
}
38+
result = max(result, point_max + samePointNum);
39+
}
40+
return result;
41+
}
42+
};

‎cpp/code/merge-intervals.cpp‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
struct Interval {
2+
int start;
3+
int end;
4+
Interval() : start(0), end(0) { }
5+
Interval(int s, int e) : start(s), end(e) { }
6+
};
7+
8+
// Merge Interval
9+
//复用一下Insert Intervals的解法即可
10+
// 时间复杂度O(n1+n2+...),空间复杂度O(1)
11+
class Solution {
12+
public:
13+
vector<Interval> merge(vector<Interval> &intervals) {
14+
vector<Interval> result;
15+
for (int i = 0; i < intervals.size(); i++) {
16+
insert(result, intervals[i]);
17+
}
18+
return result;
19+
}
20+
private:
21+
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
22+
vector<Interval>::iterator it = intervals.begin();
23+
while (it != intervals.end()) {
24+
if (newInterval.end < it->start) {
25+
intervals.insert(it, newInterval);
26+
return intervals;
27+
} else if (newInterval.start > it->end) {
28+
it++;
29+
continue;
30+
} else {
31+
newInterval.start = min(newInterval.start, it->start);
32+
newInterval.end = max(newInterval.end, it->end);
33+
it = intervals.erase(it);
34+
}
35+
}
36+
intervals.insert(intervals.end(), newInterval);
37+
return intervals;
38+
}
39+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Minimum Window Substring
2+
// 时间复杂度O(n),空间复杂度O(1)
3+
class Solution {
4+
public:
5+
string minWindow(string S, string T) {
6+
if (S.empty()) return "";
7+
if (S.size() < T.size()) return "";
8+
9+
const int ASCII_MAX = 256;
10+
int appeared_count[ASCII_MAX];
11+
int expected_count[ASCII_MAX];
12+
fill(appeared_count, appeared_count + ASCII_MAX, 0);
13+
fill(expected_count, expected_count + ASCII_MAX, 0);
14+
15+
for (size_t i = 0; i < T.size(); i++) expected_count[T[i]]++;
16+
17+
int minWidth = INT_MAX, min_start = 0; // 窗口大小,起点
18+
int wnd_start = 0;
19+
int appeared = 0; // 完整包含了一个T
20+
//尾指针不断往后扫
21+
for (size_t wnd_end = 0; wnd_end < S.size(); wnd_end++) {
22+
if (expected_count[S[wnd_end]] > 0) { // this char is a part of T
23+
appeared_count[S[wnd_end]]++;
24+
if (appeared_count[S[wnd_end]] <= expected_count[S[wnd_end]])
25+
appeared++;
26+
}
27+
if (appeared == T.size()) { // 完整包含了一个T
28+
// 收缩头指针
29+
while (appeared_count[S[wnd_start]] > expected_count[S[wnd_start]]
30+
|| expected_count[S[wnd_start]] == 0) {
31+
appeared_count[S[wnd_start]]--;
32+
wnd_start++;
33+
}
34+
if (minWidth > (wnd_end - wnd_start + 1)) {
35+
minWidth = wnd_end - wnd_start + 1;
36+
min_start = wnd_start;
37+
}
38+
}
39+
}
40+
41+
if (minWidth == INT_MAX) return "";
42+
else return S.substr(min_start, minWidth);
43+
}
44+
};

‎cpp/code/multiply-strings-1.cpp‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Multiply Strings
2+
// @author 连城 (http://weibo.com/lianchengzju)
3+
// 一个字符对应一个int
4+
// 时间复杂度O(n*m),空间复杂度O(n+m)
5+
typedef vector<int> bigint;
6+
7+
bigint make_bigint(string const& repr) {
8+
bigint n;
9+
transform(repr.rbegin(), repr.rend(), back_inserter(n),
10+
[](char c) { return c - '0'; });
11+
return n;
12+
}
13+
14+
string to_string(bigint const& n) {
15+
string str;
16+
transform(find_if(n.rbegin(), prev(n.rend()),
17+
[](char c) { return c > '\0'; }), n.rend(), back_inserter(str),
18+
[](char c) { return c + '0'; });
19+
return str;
20+
}
21+
22+
bigint operator*(bigint const& x, bigint const& y) {
23+
bigint z(x.size() + y.size());
24+
25+
for (size_t i = 0; i < x.size(); ++i)
26+
for (size_t j = 0; j < y.size(); ++j) {
27+
z[i + j] += x[i] * y[j];
28+
z[i + j + 1] += z[i + j] / 10;
29+
z[i + j] %= 10;
30+
}
31+
32+
return z;
33+
}
34+
35+
class Solution {
36+
public:
37+
string multiply(string num1, string num2) {
38+
return to_string(make_bigint(num1) * make_bigint(num2));
39+
}
40+
};

0 commit comments

Comments
 (0)