Skip to content

Commit 9286604

Browse files
committed
Day - 77 work
1 parent 0895920 commit 9286604

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 111 |
10-
| Current Streak | 76 days |
11-
| Longest Streak | 76 ( August 17, 2015 - October 31, 2015 ) |
9+
| Total Problems | 113 |
10+
| Current Streak | 77 days |
11+
| Longest Streak | 77 ( August 17, 2015 - November 01, 2015 ) |
1212

1313
</center>
1414

@@ -188,3 +188,5 @@ Include contains single header implementation of data structures and some algori
188188
| Given a list, rotate the list to the right by k places, where k is non-negative. For example: <ul><li>Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL</li></ul>| [rotateList.cpp](leet_code_problems/rotateList.cpp)|
189189
| Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.). You have the following 3 operations permitted on a word:<ul><li>Insert a character</li></ul><ul><li>Delete a character.</li></ul><ul><li>Replace a character</li></ul>.|[editDistance.cpp](leet_code_problems/editDistance.cpp)|
190190
|Given a binary tree, Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL.You may only use constant extra space.You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).| [connectNextPointers.cpp](leet_code_problems/connectNextPointers.cpp)|
191+
|Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is *"((()))", "(()())", "(())()", "()(())", "()()()"*| [generate_parenthesis.cpp](leet_code_problems/generate_parenthesis.cpp)|
192+
|Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example, Given nums = [0, 1, 3] return 2.| [missing_number.cpp](leet_code_problems/missing_number.cpp)|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
3+
*
4+
* For example, given n = 3, a solution set is:
5+
*
6+
* "((()))", "(()())", "(())()", "()(())", "()()()"
7+
*
8+
*/
9+
10+
#include <iostream>
11+
#include <vector>
12+
#include <string>
13+
14+
15+
void generate_parenthesis_helper( int n, int left, int right, std::string curr, std::vector<std::string> & result ) {
16+
if (right == n) {
17+
result.push_back(curr);
18+
} else {
19+
if (left < n) {
20+
generate_parenthesis_helper( n, left + 1, right, curr + '(', result);
21+
}
22+
if (right < left) {
23+
generate_parenthesis_helper( n, left, right + 1, curr + ')', result);
24+
}
25+
}
26+
}
27+
28+
std::vector<std::string> generate_parenthesis( int n ) {
29+
std::vector<std::string> result;
30+
std::string curr{ "" };
31+
generate_parenthesis_helper(n, 0, 0, curr, result);
32+
return result;
33+
}
34+
35+
int main() {
36+
std::vector<std::string> result = generate_parenthesis(3);
37+
for ( auto s : result ) {
38+
std::cout << "{ " << s << " }" << std::endl;
39+
}
40+
return 0;
41+
}

leet_code_problems/missing_number.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
3+
*
4+
* For example,
5+
* Given nums = [0, 1, 3] return 2.
6+
*/
7+
8+
#include <iostream>
9+
#include <vector>
10+
11+
int missingNumber( std::vector<int>& nums ) {
12+
int missing_sum = 0;
13+
for ( auto n : nums ) {
14+
missing_sum += n;
15+
}
16+
int n = nums.size();
17+
int expected_sum = 0;
18+
if ( n % 2 == 0 ) {
19+
expected_sum = (n/2) * (n + 1);
20+
} else {
21+
expected_sum = n * ( (n+1)/2 );
22+
}
23+
return expected_sum - missing_sum;
24+
}
25+
26+
void printVec( std::vector<int> & vec ) {
27+
std::cout << "Vec:";
28+
for ( auto v : vec ) {
29+
std::cout << v << " ";
30+
}
31+
std::cout << std::endl;
32+
}
33+
34+
int main() {
35+
std::vector<int> vec{ 0, 1, 3 };
36+
printVec(vec);
37+
std::cout << "Missing number in above vector is :" << missingNumber(vec) << std::endl;
38+
return 0;
39+
}

leet_code_problems/run

21.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)