Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
};