Skip to content

Commit

Permalink
Merge branch 'master' of github.com:haoel/leetcode
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Nov 4, 2015
2 parents 9b15c6a + a4ea8c6 commit 3475545
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ LeetCode

###LeetCode Algorithm

(Notes: "♥" means you need buy a book from Leetcode)
(Notes: "♥" means you need to buy a book from Leetcode)


| # | Title | Solution | Difficulty |
Expand All @@ -15,12 +15,14 @@ LeetCode
|283|[Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [C++](./algorithms/cpp/moveZeroes/moveZeroes.cpp)|Easy|
|279|[Perfect Squares](https://leetcode.com/problems/perfect-squares/) | [C++](./algorithms/cpp/perfectSquares/PerfectSquares.cpp)|Medium|
|278|[First Bad Version](https://leetcode.com/problems/first-bad-version/)| [C++](./algorithms/cpp/firstBadVersion/FirstBadVersion.cpp), [Java](./algorithms/java/src/firstBadVersion/firstBadVersion.java)|Easy|
|275|[H-Index II](https://leetcode.com/problems/h-index-ii/)| [C++](./algorithms/cpp/h-Index/h-Index_II.cpp)|Medium|
|274|[H-Index](https://leetcode.com/problems/h-index/)| [C++](./algorithms/cpp/h-Index/h-Index.cpp)|Medium|
|273|[Integer to English Words](https://leetcode.com/problems/integer-to-english-words/)| [C++](./algorithms/cpp/integerToEnglishWords/IntegerToEnglishWords.cpp)|Medium|
|268|[Missing Number](https://leetcode.com/problems/missing-number/)| [C++](./algorithms/cpp/missingNumber/MissingNumber.cpp)|Medium|
|264|[Ugly Number II](https://leetcode.com/problems/ugly-number-ii/)| [C++](./algorithms/cpp/uglyNumber/UglyNumber.II.cpp)|Medium|
|263|[Ugly Number](https://leetcode.com/problems/ugly-number/)| [C++](./algorithms/cpp/uglyNumber/UglyNumber.cpp)|Easy|
|258|[Add Digits](https://leetcode.com/problems/add-digits/)| [C++](./algorithms/cpp/addDigits/addDigits.cpp)|Easy|
|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)| [C++](./algorithms/cpp/binaryTreePaths/binaryTreePaths.cpp)|Easy|
|242|[Valid Anagram](https://leetcode.com/problems/valid-anagram/)| [C++](./algorithms/cpp/anagrams/ValidAnagram.cpp)|Easy|
|241|[Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/)|[C++](./algorithms/cpp/differentWaysToAddParentheses/DifferentWaysToAddParentheses.cpp)|Medium|
|240|[Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/)|[C++](./algorithms/cpp/search2DMatrix/search2DMatrix.II.cpp)|Medium|
Expand Down
59 changes: 59 additions & 0 deletions algorithms/cpp/binaryTreePaths/binaryTreePaths.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Source : https://leetcode.com/problems/binary-tree-paths/
// Author : Calinescu Valentin
// Date : 2015-10-23

/***************************************************************************************
*
* Given a binary tree, return all root-to-leaf paths.
*
* For example, given the following binary tree:
*
* 1
* / \
* 2 3
* \
* 5
*
* All root-to-leaf paths are:
* ["1->2->5", "1->3"]
*
* Credits:
* Special thanks to @jianchao.li.fighter for adding this problem and creating all test
* cases.
*
***************************************************************************************/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<string> TreePaths;
void DFS(TreeNode* node, string answer)
{
answer += "->" + to_string(node->val);
if(node->left == NULL && node->right == NULL)
TreePaths.push_back(answer);
else
{
if(node->left != NULL)
DFS(node->left, answer);
if(node->right != NULL)
DFS(node->right, answer);
}
}
vector<string> binaryTreePaths(TreeNode* root) {
if(root != NULL)
{
DFS(root, "");
for(int i = 0; i < TreePaths.size(); i++)
TreePaths[i].erase(TreePaths[i].begin(), TreePaths[i].begin() + 2);
}
return TreePaths;
}
};
36 changes: 36 additions & 0 deletions algorithms/cpp/h-Index/h-Index_II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Source : https://leetcode.com/problems/h-index-ii/
// Author : Calinescu Valentin
// Date : 2015-10-23

/***************************************************************************************
*
* Follow up for H-Index: What if the citations array is sorted in ascending order?
* Could you optimize your algorithm?
*
***************************************************************************************/



/*
* Solutions
* =========
*
* At every step we need to check whether this element is not less than
* the remaining number of elements bigger than it(including itself) and all the values of
* the other elements smaller than it are not more than that number. The h_index is this
* number of elements bigger than it(including itself).
*
* Time Complexity: O(N)
* Space Complexity: O(1)
*
*/
class Solution {
public:
int hIndex(vector<int>& citations) {
int h_index = 0;
for(int i = citations.size() - 1; i >= 0; i--)
if(citations[i] >= citations.size() - i && (i - 1 < 0 || citations[i - 1] <= citations.size() - i))
h_index = citations.size() - i;
return h_index;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,72 @@ void connect1(TreeLinkNode *root) {
v.pop_back();
}
}

void connect3(TreeLinkNode *root) {
if(root == NULL) return;

queue<TreeLinkNode*> q;
TreeLinkNode *prev, *last;
prev = last = root;

q.push(root);
while(!q.empty()) {
TreeLinkNode* p = q.front();
q.pop();

prev->next = p;
if(p->left ) q.push(p->left);
if(p->right) q.push(p->right);

if(p == last) { // meets last of current level
// now, q contains all nodes of next level
last = q.back();
p->next = NULL; // cut down.
prev = q.front();
}
else {
prev = p;
}
}
}

// constant space
// key point: we can use `next` pointer to represent
// the buffering queue of level order traversal.
void connect4(TreeLinkNode *root) {
if(root == NULL) return;

TreeLinkNode *head, *tail;
TreeLinkNode *prev, *last;

head = tail = NULL;
prev = last = root;

#define push(p) \
if(head == NULL) { head = tail = p; } \
else { tail->next = p; tail = p; }

push(root);
while(head != NULL) {
TreeLinkNode* p = head;
head = head->next; // pop

prev->next = p;
if(p->left ) push(p->left);
if(p->right) push(p->right);

if(p == last) {
last = tail;
p->next = NULL; // cut down.
prev = head;
}
else {
prev = p;
}
}
#undef push
}

void printTree(TreeLinkNode *root){
if (root == NULL){
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;

/**
Expand Down Expand Up @@ -130,6 +131,71 @@ void connect2(TreeLinkNode *root) {
}
}

void connect3(TreeLinkNode *root) {
if(root == NULL) return;

queue<TreeLinkNode*> q;
TreeLinkNode *prev, *last;
prev = last = root;

q.push(root);
while(!q.empty()) {
TreeLinkNode* p = q.front();
q.pop();

prev->next = p;
if(p->left ) q.push(p->left);
if(p->right) q.push(p->right);

if(p == last) { // meets last of current level
// now, q contains all nodes of next level
last = q.back();
p->next = NULL; // cut down.
prev = q.front();
}
else {
prev = p;
}
}
}

// constant space
// key point: we can use `next` pointer to represent
// the buffering queue of level order traversal.
void connect4(TreeLinkNode *root) {
if(root == NULL) return;

TreeLinkNode *head, *tail;
TreeLinkNode *prev, *last;

head = tail = NULL;
prev = last = root;

#define push(p) \
if(head == NULL) { head = tail = p; } \
else { tail->next = p; tail = p; }

push(root);
while(head != NULL) {
TreeLinkNode* p = head;
head = head->next; // pop

prev->next = p;
if(p->left ) push(p->left);
if(p->right) push(p->right);

if(p == last) {
last = tail;
p->next = NULL; // cut down.
prev = head;
}
else {
prev = p;
}
}
#undef push
}

void printTree(TreeLinkNode *root){
if (root == NULL){
return;
Expand Down

0 comments on commit 3475545

Please sign in to comment.