File tree Expand file tree Collapse file tree 3 files changed +98
-1
lines changed Expand file tree Collapse file tree 3 files changed +98
-1
lines changed Original file line number Diff line number Diff line change 33
44###LeetCode Algorithm
55
6- (Notes: "&hearts ; " means you need buy a book from Leetcode)
6+ (Notes: "&hearts ; " means you need to buy a book from Leetcode)
77
88
99| # | Title | Solution | Difficulty |
@@ -15,12 +15,14 @@ LeetCode
1515| 283| [ Move Zeroes] ( https://leetcode.com/problems/move-zeroes/ ) | [ C++] ( ./algorithms/cpp/moveZeroes/moveZeroes.cpp ) | Easy|
1616| 279| [ Perfect Squares] ( https://leetcode.com/problems/perfect-squares/ ) | [ C++] ( ./algorithms/cpp/perfectSquares/PerfectSquares.cpp ) | Medium|
1717| 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|
18+ | 275| [ H-Index II] ( https://leetcode.com/problems/h-index-ii/ ) | [ C++] ( ./algorithms/cpp/h-Index/h-Index_II.cpp ) | Medium|
1819| 274| [ H-Index] ( https://leetcode.com/problems/h-index/ ) | [ C++] ( ./algorithms/cpp/h-Index/h-Index.cpp ) | Medium|
1920| 273| [ Integer to English Words] ( https://leetcode.com/problems/integer-to-english-words/ ) | [ C++] ( ./algorithms/cpp/integerToEnglishWords/IntegerToEnglishWords.cpp ) | Medium|
2021| 268| [ Missing Number] ( https://leetcode.com/problems/missing-number/ ) | [ C++] ( ./algorithms/cpp/missingNumber/MissingNumber.cpp ) | Medium|
2122| 264| [ Ugly Number II] ( https://leetcode.com/problems/ugly-number-ii/ ) | [ C++] ( ./algorithms/cpp/uglyNumber/UglyNumber.II.cpp ) | Medium|
2223| 263| [ Ugly Number] ( https://leetcode.com/problems/ugly-number/ ) | [ C++] ( ./algorithms/cpp/uglyNumber/UglyNumber.cpp ) | Easy|
2324| 258| [ Add Digits] ( https://leetcode.com/problems/add-digits/ ) | [ C++] ( ./algorithms/cpp/addDigits/addDigits.cpp ) | Easy|
25+ | 257| [ Binary Tree Paths] ( https://leetcode.com/problems/binary-tree-paths/ ) | [ C++] ( ./algorithms/cpp/binaryTreePaths/binaryTreePaths.cpp ) | Easy|
2426| 242| [ Valid Anagram] ( https://leetcode.com/problems/valid-anagram/ ) | [ C++] ( ./algorithms/cpp/anagrams/ValidAnagram.cpp ) | Easy|
2527| 241| [ Different Ways to Add Parentheses] ( https://leetcode.com/problems/different-ways-to-add-parentheses/ ) | [ C++] ( ./algorithms/cpp/differentWaysToAddParentheses/DifferentWaysToAddParentheses.cpp ) | Medium|
2628| 240| [ Search a 2D Matrix II] ( https://leetcode.com/problems/search-a-2d-matrix-ii/ ) | [ C++] ( ./algorithms/cpp/search2DMatrix/search2DMatrix.II.cpp ) | Medium|
Original file line number Diff line number Diff line change 1+ // Source : https://leetcode.com/problems/binary-tree-paths/
2+ // Author : Calinescu Valentin
3+ // Date : 2015-10-23
4+
5+ /* **************************************************************************************
6+ *
7+ * Given a binary tree, return all root-to-leaf paths.
8+ *
9+ * For example, given the following binary tree:
10+ *
11+ * 1
12+ * / \
13+ * 2 3
14+ * \
15+ * 5
16+ *
17+ * All root-to-leaf paths are:
18+ * ["1->2->5", "1->3"]
19+ *
20+ * Credits:
21+ * Special thanks to @jianchao.li.fighter for adding this problem and creating all test
22+ * cases.
23+ *
24+ ***************************************************************************************/
25+ /* *
26+ * Definition for a binary tree node.
27+ * struct TreeNode {
28+ * int val;
29+ * TreeNode *left;
30+ * TreeNode *right;
31+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
32+ * };
33+ */
34+ class Solution {
35+ public:
36+ vector<string> TreePaths;
37+ void DFS (TreeNode* node, string answer)
38+ {
39+ answer += " ->" + to_string (node->val );
40+ if (node->left == NULL && node->right == NULL )
41+ TreePaths.push_back (answer);
42+ else
43+ {
44+ if (node->left != NULL )
45+ DFS (node->left , answer);
46+ if (node->right != NULL )
47+ DFS (node->right , answer);
48+ }
49+ }
50+ vector<string> binaryTreePaths (TreeNode* root) {
51+ if (root != NULL )
52+ {
53+ DFS (root, " " );
54+ for (int i = 0 ; i < TreePaths.size (); i++)
55+ TreePaths[i].erase (TreePaths[i].begin (), TreePaths[i].begin () + 2 );
56+ }
57+ return TreePaths;
58+ }
59+ };
Original file line number Diff line number Diff line change 1+ // Source : https://leetcode.com/problems/h-index-ii/
2+ // Author : Calinescu Valentin
3+ // Date : 2015-10-23
4+
5+ /* **************************************************************************************
6+ *
7+ * Follow up for H-Index: What if the citations array is sorted in ascending order?
8+ * Could you optimize your algorithm?
9+ *
10+ ***************************************************************************************/
11+
12+
13+
14+ /*
15+ * Solutions
16+ * =========
17+ *
18+ * At every step we need to check whether this element is not less than
19+ * the remaining number of elements bigger than it(including itself) and all the values of
20+ * the other elements smaller than it are not more than that number. The h_index is this
21+ * number of elements bigger than it(including itself).
22+ *
23+ * Time Complexity: O(N)
24+ * Space Complexity: O(1)
25+ *
26+ */
27+ class Solution {
28+ public:
29+ int hIndex (vector<int >& citations) {
30+ int h_index = 0 ;
31+ for (int i = citations.size () - 1 ; i >= 0 ; i--)
32+ if (citations[i] >= citations.size () - i && (i - 1 < 0 || citations[i - 1 ] <= citations.size () - i))
33+ h_index = citations.size () - i;
34+ return h_index;
35+ }
36+ };
You can’t perform that action at this time.
0 commit comments