Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 26bd72f

Browse files
author
kaidul
committed
After a long time..
1 parent 1ac64e3 commit 26bd72f

22 files changed

+376
-202
lines changed

‎source-code/Combinations.cpp‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,31 @@ class Solution {
1919
combineUtils(1, n, k, solution, result);
2020
return result;
2121
}
22-
};
22+
};
23+
24+
// Alternatives
25+
class Solution {
26+
public:
27+
void combineUtils(int idx, int n, int k, vector<int> &solution, vector<vector<int> > &result) {
28+
if(idx > n or solution.size() == k) {
29+
if(solution.size() == k)
30+
result.push_back(solution);
31+
return;
32+
}
33+
if(solution.size() < k) {
34+
solution.push_back(idx);
35+
combineUtils(idx + 1, n, k, solution, result);
36+
solution.pop_back();
37+
}
38+
39+
combineUtils(idx + 1, n, k, solution, result);
40+
}
41+
42+
vector<vector<int> > combine(int n, int k) {
43+
vector<vector<int> > result;
44+
vector<int> solution;
45+
if(k == 0 or n == 0) return result;
46+
combineUtils(1, n, k, solution, result);
47+
return result;
48+
}
49+
};

‎source-code/Convert_Sorted_List_to_Binary_Search_Tree.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Solution {
2323
}
2424
ListNode *slow = head, *fast = head->next;
2525
ListNode *tail = nullptr;
26-
while(fast) {
26+
while(fastand fast->next) {
2727
fast = fast->next;
2828
tail = slow;
2929
slow = slow->next;
@@ -43,4 +43,4 @@ class Solution {
4343
TreeNode *sortedListToBST(ListNode *head) {
4444
return sortedListToBSTUtils(head);
4545
}
46-
};
46+
};

‎source-code/Count_and_Say.cpp‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,17 @@ class Solution {
55
if(n == 0) return result;
66
result = "1";
77
while(--n) {
8-
char prev = 'x';
9-
int count = 0;
108
string tmp = "";
11-
for(int i = 0; i < result.length(); ++i) {
12-
if(prev != 'x'and prev != result[i]) {
13-
tmp += (char)(count + '0');
14-
tmp += prev;
15-
prev = result[i];
16-
count = 0;
9+
for(int i = 0; i < result.length(); i++) {
10+
int count = 1;
11+
char digit = result[i];
12+
while(i + 1 < result.length() and result[i] == result[i + 1]) {
13+
i++;
14+
count++;
1715
}
18-
if(prev == 'x') prev = result[i];
19-
count++;
16+
tmp += char(count + '0');
17+
tmp += digit;
2018
}
21-
tmp += (char)(count + '0');
22-
tmp += prev;
2319
result = tmp;
2420
}
2521
return result;

‎source-code/Decode_Ways.cpp‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,33 @@ class Solution {
1414
}
1515
return true;
1616
}
17+
18+
int numDecodingsUtil(string s, int indx, vector<int>& dp) {
19+
if(indx == (int)s.length()) {
20+
return 1;
21+
}
22+
if(dp[indx] != -1) {
23+
return dp[indx];
24+
}
25+
int ret = 0;
26+
if(s[indx] != '0') {
27+
ret += numDecodingsUtil(s, indx + 1, dp);
28+
}
29+
if(indx + 1 < s.length() and valid(s.substr(indx, 2))) {
30+
ret += numDecodingsUtil(s, indx + 2, dp);
31+
}
32+
33+
return dp[indx] = ret;
34+
}
1735

18-
int numDecodings(string s) {
36+
int numDecodings(string s) {
1937
if (s.empty()) {
2038
return 0;
2139
}
40+
// recursive
41+
// vector<int> dp(s.size(), -1);
42+
// return numDecodingsUtil(s, 0, dp);
43+
2244
vector<int> dp(s.size(), 0);
2345
if (s[0] != '0') {
2446
dp[0] = 1;
@@ -33,7 +55,6 @@ class Solution {
3355
dp[1]++;
3456
}
3557

36-
//DP
3758
for (int i = 2; i < s.size(); i++) {
3859
if (s[i] != '0') {
3960
dp[i] += dp[i - 1];
@@ -45,4 +66,4 @@ class Solution {
4566

4667
return dp[s.size() - 1];
4768
}
48-
};
69+
};

‎source-code/Flatten_Binary_Tree_to_Linked_List.cpp‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@
99
*/
1010
class Solution {
1111
public:
12-
void flatten(TreeNode *root) {
12+
void flatten(TreeNode* root) {
1313
while(root) {
1414
if(root->left) {
15-
TreeNode *pre = root->left;
16-
while(pre->right) {
17-
pre = pre->right;
18-
}
19-
pre->right = root->right;
15+
TreeNode* right = root->right;
2016
root->right = root->left;
2117
root->left = nullptr;
18+
TreeNode* left = root->right;
19+
while(left->right) {
20+
left = left->right;
21+
}
22+
left->right = right;
2223
}
2324
root = root->right;
2425
}
2526
}
26-
};
27+
};

‎source-code/Gas_Station.cpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ class Solution {
44
int n = (int) gas.size();
55
for(int i = 0; i < n; ++i) {
66
bool flag = true;
7-
for(int j = i, tank = 0; j < n + iand flag; ++j) {
7+
for(int j = i, tank = 0; j < n + i; ++j) {
88
int idx = j % n;
99
tank += gas[idx];
1010
tank -= cost[idx];
1111
if(tank < 0) {
1212
flag = false;
13+
break;
1314
}
1415
}
15-
if(flag) return j;
16+
if(flag) return i;
1617
}
1718
return -1;
1819
}
19-
};
20+
};

‎source-code/Goat_Latin.cpp‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
bool isVowel(char ch) {
3+
switch(tolower(ch)) {
4+
case 'a':
5+
case 'e':
6+
case 'i':
7+
case 'o':
8+
case 'u':
9+
return true;
10+
default:
11+
return false;
12+
}
13+
}
14+
public:
15+
string toGoatLatin(string S) {
16+
if(S.empty()) return S;
17+
int start = 0;
18+
string result;
19+
string suffix = "a";
20+
while(start < S.length()) {
21+
int end = S.find(' ', start);
22+
if(end == string::npos) {
23+
end = (int)S.length();
24+
}
25+
string word = S.substr(start, end - start);
26+
if(isVowel(word[0])) {
27+
word += "ma";
28+
} else {
29+
word += word[0];
30+
word += "ma";
31+
word = word.substr(1);
32+
}
33+
word += suffix;
34+
result += word;
35+
result += ' ';
36+
suffix += 'a';
37+
start = end + 1;
38+
}
39+
return result.substr(0, result.length() - 1); // or return result.erase(result.length() - 1, 1);
40+
}
41+
};

‎source-code/Longest_Consecutive_Sequence.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ class Solution {
2929
result = max(result, seqLen);
3030
}
3131

32-
return res;
32+
return result;
3333
}
34-
};
34+
};

‎source-code/Longest_Valid_Parenthesis.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
class Solution {
2+
public:
3+
int longestValidParentheses(string s) {
4+
stack<int> Stack;
5+
int result = 0;
6+
Stack.push(-1);
7+
for(int i = 0; i < (int)s.length(); i++) {
8+
if(s[i] == '(') {
9+
Stack.push(i);
10+
} else {
11+
if(Stack.size() == 1 or s[Stack.top()] != '(') {
12+
Stack.push(i);
13+
} else {
14+
Stack.pop();
15+
result = max(result, i - Stack.top());
16+
}
17+
}
18+
}
19+
20+
return result;
21+
}
22+
};
23+
24+
// Another way (slightly different)
25+
class Solution {
226
public:
327
int longestValidParentheses(string s) {
428
stack <int> Stack;

‎source-code/Merge_Sorted_Array.cpp‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ class Solution {
22
public:
33
// AC but Extra space : O(m)
44
/*
5-
void merge(int A[], int m, int B[], int n) {
6-
int tmp[m];
7-
memcpy(tmp, A, m * sizeof(int));
5+
void merge(vector<int>& A, int m, vector<int>& B, int n) {
6+
vector<int> tmp(A);
87
int k = 0, i = 0, j = 0;
98
while(i < m and j < n) {
10-
if(tmp[i] <= B[j]) A[k++] = tmp[i++];
11-
else A[k++] = B[j++];
9+
if(tmp[i] <= B[j])
10+
A[k++] = tmp[i++];
11+
else
12+
A[k++] = B[j++];
1213
}
1314
while(i < m) {
1415
A[k++] = tmp[i++];
@@ -18,13 +19,17 @@ class Solution {
1819
}
1920
}
2021
*/
22+
23+
2124

22-
void merge(int A[], int m, int B[], int n) {
23-
int count = m + n - 1;
25+
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
26+
int k = m + n - 1;
2427
m--; n--;
25-
while (m >= 0 and n >= 0) {
26-
A[count--] = A[m] > B[n] ? A[m--] : B[n--];
28+
while(m >= 0 and n >= 0) {
29+
nums1[k--] = nums1[m] > nums2[n] ? nums1[m--] : nums2[n--];
30+
}
31+
while(n >= 0) {
32+
nums1[k--] = nums2[n--];
2733
}
28-
while (n >= 0) { A[count--] = B[n--]; }
2934
}
30-
};
35+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /