added binaryTreePaths and h-Index_II

This commit is contained in:
Vally 2015-11-03 21:32:13 +02:00
parent edc90061b0
commit 65427e833b
3 changed files with 98 additions and 1 deletions

View File

@ -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 |
@ -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|

View File

@ -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;
}
};

View File

@ -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;
}
};