Skip to content

Commit ec8528b

Browse files
committed
update
1 parent d37466c commit ec8528b

File tree

8 files changed

+141
-141
lines changed

8 files changed

+141
-141
lines changed

Combinations/Combinations.cpp

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
// [1,4],
1515
//]
1616
//
17-
// Complexity: O(N!) time
18-
// Sol 1: combine(n, k) = combine(n-1, k-1) + combine(n-1, k)
19-
// Sol 2: combine(n, k) = join (1 ~ n-k+1) from k to 1
20-
// Sol 3: combine(n, k) = combine(n-1, k-1) + (n ~ k)
17+
// Complexity:
18+
// O(N!) time
2119
//============================================================================
2220

21+
#include <iostream>
2322
#include <vector>
2423
using namespace std;
2524

@@ -30,76 +29,43 @@ class Solution {
3029
}
3130

3231
vector<vector<int> > combine1(int n, int k) {
33-
vector<int> sub;
32+
vector<int> path;
3433
vector<vector<int> > res;
35-
combineHelper1(n, k, 1, sub, res);
34+
combineHelper(n, k, 1, path, res);
3635
return res;
3736
}
3837

39-
void combineHelper1(int n, int k, int s, vector<int> & sub, vector<vector<int> > & res) {
38+
void combineHelper(int n, int k, int begin, vector<int> & path, vector<vector<int> > & res) {
4039
if (k == 0) {
41-
res.push_back(sub);
40+
res.push_back(path);
4241
return;
4342
}
44-
for (int i = s; i <= n - k + 1; i++) {
45-
sub.push_back(i);
46-
combineHelper1(n, k - 1, i + 1, sub, res);
47-
sub.pop_back();
43+
int end = n - k + 1;
44+
for (int cur = begin; cur <= end; cur++) {
45+
path.push_back(cur);
46+
combineHelper(n, k - 1, cur + 1, path, res);
47+
path.pop_back();
4848
}
4949
}
5050

5151
vector<vector<int> > combine2(int n, int k) {
52-
vector<vector<int> > res(1, vector<int>());
53-
for (; k > 0; k--){
52+
vector<vector<int> > res;
53+
for (; k > 0; k--) {
5454
if (res.empty()) {
55-
for (int i = 1; i <= n - k + 1; i++) res.push_back(vector<int>(1, i));
55+
for (int cur = 1; cur <= n - k + 1; cur++) res.push_back(vector<int>(1, cur));
5656
continue;
5757
}
58-
59-
int N = res.size();
60-
for (int i = 0; i < N; i++) {
61-
int j = res[i].back() + 1;
62-
for (; j < n - k + 1; j++) {
63-
vector<int> copy = res[i];
64-
copy.push_back(j);
65-
res.push_back(copy);
58+
int M = res.size();
59+
for (int i = 0; i < M; i++) {
60+
int begin = res[i].back() + 1, end = n - k + 1;
61+
for (int cur = begin; cur <= end; cur++){
62+
if (cur == end) res[i].push_back(cur);
63+
else {
64+
auto copy = res[i];
65+
copy.push_back(cur);
66+
res.push_back(copy);
67+
}
6668
}
67-
res[i].push_back(j);
68-
}
69-
}
70-
return res;
71-
}
72-
73-
vector<vector<int> > combine3(int n, int k) {
74-
vector<int> sub;
75-
vector<vector<int> > res;
76-
combineHelper3(n, k, 1, sub, res);
77-
return res;
78-
}
79-
80-
void combineHelper3(int n, int k, int s, vector<int> & sub, vector<vector<int> > & res) {
81-
if (k == 0) {
82-
res.push_back(sub);
83-
return;
84-
}
85-
if (s > n) return;
86-
sub.push_back(s);
87-
combineHelper3(n, k - 1, s + 1, sub, res);
88-
sub.pop_back();
89-
combineHelper3(n, k, s + 1, sub, res);
90-
}
91-
92-
vector<vector<int> > combine4(int n, int k) {
93-
vector<vector<int> > res;
94-
if (k == 1) {
95-
for (int i = 1; i <= n; i++) res.push_back(vector<int>(1, i));
96-
return res;
97-
}
98-
for (int i = n; i >= k; i--) {
99-
auto last = combine4(i - 1, k - 1);
100-
for (auto it : last) {
101-
it.push_back(i);
102-
res.push_back(it);
10369
}
10470
}
10571
return res;

LetterCombinationsofaPhoneNumber/LetterCombinationsofaPhoneNumber.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,22 @@ class Solution {
2424
return letterCombinations1(digits);
2525
}
2626

27-
vector<string> letterCombinations1(string digits) {
28-
string sub;
27+
vector<string> letterCombinations1(string & digits) {
28+
string path;
2929
vector<string> res;
30-
letterCombinationsHelper1(digits, 0, sub, res);
30+
letterCombinationsHelper1(digits, 0, path, res);
3131
return res;
3232
}
3333

34-
void letterCombinationsHelper1(string & digits, int start, string & sub, vector<string> & res) {
35-
if (start == digits.size()) {
36-
res.push_back(sub);
34+
void letterCombinationsHelper1(string & digits, int begin, string & path, vector<string> & res) {
35+
if (begin == digits.size()) {
36+
res.push_back(path);
3737
return;
3838
}
39-
40-
int d = digits[start] - '2';
41-
for (char c : keypad[d]) {
42-
sub.push_back(c);
43-
letterCombinationsHelper1(digits, start + 1, sub, res);
44-
sub.pop_back();
39+
for (char c : keypad[digits[begin] - '2']) {
40+
path.push_back(c);
41+
letterCombinationsHelper1(digits, begin + 1, path, res);
42+
path.pop_back();
4543
}
4644
}
4745

PermutationSequence/PermutationSequence.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,25 @@ class Solution {
3232
}
3333

3434
string getPermutation1(int n, int k) {
35-
string res;
36-
for (int i = 1; i <= n; i++) res.push_back('0' + i);
35+
string res(n, '1');
36+
for (int i = 0; i < n; i++) res[i] += i;
3737
for (; k > 1; k--) next_permutation(begin(res), end(res));
3838
return res;
3939
}
4040

4141
string getPermutation2(int n, int k) {
42-
string res;
43-
vector<int> F(n + 1, 0);
44-
for (int i = 1; i <= n; i++) {
45-
if (i == 1) F[i] = 1;
46-
else F[i] = F[i - 1] * i;
47-
48-
}
49-
if (k > F[n]) return res;
50-
for (int i = 1; i <= n; i++) res.push_back('0' + i);
42+
vector<int> fs(n + 1, 1);
43+
for (int i = 2; i <= n; i++) fs[i] = fs[i - 1] * i;
44+
string res(n, '1');
45+
for (int i = 0; i < n; i++) res[i] += i;
46+
if (k > fs[n]) return res;
5147
k--;
5248
for (int i = 0; i < n - 1; i++) {
53-
int j = k / F[n - 1 - i] + i;
54-
char c = res[j];
49+
int j = i + k / fs[n - 1 - i];
50+
k %= fs[n - 1 - i];
51+
char t = res[j];
5552
for (; j > i; j--) res[j] = res[j - 1];
56-
res[i] = c;
57-
k %= F[n - 1 - i];
53+
res[j] = t;
5854
}
5955
return res;
6056
}

Permutations/Permutations.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ class Solution {
3232
return res;
3333
}
3434

35-
void permuteHelper2(vector<int> & num, int i, vector<vector<int> > & res) {
36-
if (i == (int)num.size()) {
35+
void permuteHelper2(vector<int> & num, int begin, vector<vector<int> > & res) {
36+
int end = num.size();
37+
if (begin == end) {
3738
res.push_back(num);
3839
return;
3940
}
40-
41-
for (int j = i; j < (int)num.size(); j++) {
42-
swap(num[i], num[j]);
43-
permuteHelper2(num, i + 1, res);
44-
swap(num[i], num[j]);
41+
for (int cur = begin; cur < end; cur++) {
42+
swap(num[begin], num[cur]);
43+
permuteHelper2(num, begin + 1, res);
44+
swap(num[begin], num[cur]);
4545
}
4646
}
4747
};

PermutationsII/PermutationsII.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,19 @@ class Solution {
3535
return res;
3636
}
3737

38-
void permuteUnique1Helper2(vector<int> & num, int i, vector<vector<int> > & res) {
39-
if (i == (int)num.size()) {
38+
void permuteUnique1Helper2(vector<int> & num, int begin, vector<vector<int> > & res) {
39+
int end = num.size();
40+
if (begin == end) {
4041
res.push_back(num);
4142
return;
4243
}
4344
unordered_set<int> visit;
44-
for (int j = i; j < (int)num.size(); j++) {
45-
if (visit.count(num[j])) continue;
46-
visit.insert(num[j]);
47-
swap(num[i], num[j]);
48-
permuteUnique1Helper2(num, i + 1, res);
49-
swap(num[i], num[j]);
45+
for (int cur = begin; j < end; j++) {
46+
if (visit.count(num[cur])) continue;
47+
visit.insert(num[cur]);
48+
swap(num[begin], num[cur]);
49+
permuteUnique1Helper2(num, begin + 1, res);
50+
swap(num[begin], num[cur]);
5051
}
5152
}
5253
};

Searcha2DMatrix/Searcha2DMatrix.cpp

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
// Given target = 3, return true.
1818
//
1919
// Complexity:
20-
// O(log(mn))
20+
// 2D Binary Search O(m*nlog(m*n)) time
21+
// Upper Right Linear Search O(m*n) time
22+
// 1D Binary Search O(log(m*n)) time
2123
//============================================================================
2224

2325
#include <iostream>
@@ -28,14 +30,47 @@ using namespace std;
2830
class Solution {
2931
public:
3032
bool searchMatrix(vector<vector<int> > & matrix, int target) {
33+
searchMatrix3(matrix, target);
34+
}
35+
36+
bool searchMatrix(vector<vector<int> > & matrix, int target) {
37+
if (matrix.empty() || matrix[0].empty()) return false;
38+
return searchMatrix1(matrix, target);
39+
}
40+
41+
bool searchMatrix1(vector<vector<int> > &matrix, int target) {
42+
int M = matrix.size(), N = matrix[0].size();
43+
return searchMatrixHelper1(matrix, target, 0, M - 1, 0, N - 1);
44+
}
45+
46+
bool searchMatrixHelper1(vector<vector<int> > &matrix, int target, int upper, int bottom, int left, int right) {
47+
if (upper > bottom || left > right) return false;
48+
int mid = left + (right - left) / 2;
49+
int row = upper;
50+
for (; row <= bottom && matrix[row][mid] <= target; row++) if (matrix[row][mid] == target) return true;
51+
return searchMatrixHelper1(matrix, target, row, bottom, left, mid - 1) || searchMatrixHelper1(matrix, target, upper, row - 1, mid + 1, right);
52+
}
53+
54+
bool searchMatrix2(vector<vector<int> > & matrix, int target) {
55+
int M = matrix.size(), N = matrix[0].size();
56+
int i = 0, j = N - 1;
57+
while (i < M && j >= 0) {
58+
if (matrix[i][j] == target) return true;
59+
if (matrix[i][j] < target) i++;
60+
else j--;
61+
}
62+
return false;
63+
}
64+
65+
bool searchMatrix3(vector<vector<int> > & matrix, int target) {
3166
int M = matrix.size();
3267
int N = matrix[0].size();
33-
int l = 0, u = M*N-1;
68+
int l = 0, u = M*N - 1;
3469
while (l <= u) {
35-
int m = l+(u-l)/2;
36-
if (matrix[m/N][m%N] == target) return true;
37-
if (matrix[m/N][m%N] < target) l = m+1;
38-
else u = m-1;
70+
int m = l + (u - l) / 2;
71+
if (matrix[m / N][m%N] == target) return true;
72+
if (matrix[m / N][m%N] < target) l = m + 1;
73+
else u = m - 1;
3974
}
4075
return false;
4176
}

Subsets/Subsets.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,35 @@ using namespace std;
3131

3232
class Solution {
3333
public:
34-
vector<vector<int> > subsets(vector<int> & S) {
35-
return subsets1(S);
34+
vector<vector<int> > subsets(vector<int> &S) {
35+
return subsets2(S);
3636
}
3737

38-
vector<vector<int> > subsets1(vector<int> & S) {
38+
vector<vector<int> > subsets1(vector<int> &S) {
3939
sort(begin(S), end(S));
40-
vector<int> sub;
40+
vector<int> path;
4141
vector<vector<int> > res;
42-
subsetsHelper1(S, 0, sub, res);
42+
subsetsHelper1(S, 0, path, res);
4343
return res;
4444
}
4545

46-
void subsetsHelper1(vector<int> & S, int start, vector<int> & sub, vector<vector<int> > & res) {
47-
res.push_back(sub);
48-
for (int i = start; i < S.size(); i++) {
49-
auto copy = sub;
50-
copy.push_back(S[i]);
51-
subsetsHelper1(S, i + 1, copy, res);
46+
void subsetsHelper1(vector<int> &S, int begin, vector<int> & path, vector<vector<int> > & res) {
47+
res.push_back(path);
48+
int end = S.size();
49+
for (int cur = begin; cur < end; cur++) {
50+
path.push_back(S[cur]);
51+
subsetsHelper1(S, cur + 1, path, res);
52+
path.pop_back();
5253
}
5354
}
5455

55-
vector<vector<int> > subsets2(vector<int> & S) {
56+
vector<vector<int> > subsets2(vector<int> &S) {
5657
sort(begin(S), end(S));
5758
vector<vector<int> > res(1, vector<int>());
58-
for (int i = 0; i < S.size(); i++) {
59-
int N = res.size();
60-
for (int j = 0; j < N; j++) {
59+
int N = S.size();
60+
for (int i = 0; i < N; i++) {
61+
int M = res.size();
62+
for (int j = 0; j < M; j++) {
6163
auto copy = res[j];
6264
copy.push_back(S[i]);
6365
res.push_back(copy);

0 commit comments

Comments
 (0)