-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature:finish leetcode-so34,so36,so54
</subject> Branch: dev <type>: - [ ] Bug fix - [ ] Bug fix (Test) - [x] New feature - [ ] Breaking change - [ ] Documentation update - [ ] This change requires a documentation update <body> <footer> Signed-off-by: Certseeds <[email protected]>
- Loading branch information
Showing
9 changed files
with
405 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
MIT License | ||
CS203_DSAA_template | ||
Copyright (C) 2020-2022 nanoseeds | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
#include "leetcode_113_test.hpp" | ||
#include <queue> | ||
|
||
namespace leetcode_113 { | ||
using std::queue; | ||
|
||
|
||
vector<vector<int>> leetcode_113::pathSum(TreeNode *root, int target) { | ||
vector<vector<int32_t>> sums; | ||
if (root == nullptr) { | ||
return sums; | ||
} | ||
for (queue<std::pair<TreeNode *, const vector<int32_t>>> now{{{root, {root->val}}}}; !now.empty();) { | ||
const auto[head, vec] = now.front(); | ||
now.pop(); | ||
if (head->val == target && head->left == nullptr && head->right == nullptr) { | ||
sums.push_back(vec); | ||
continue; | ||
} | ||
if (head->left != nullptr) { | ||
vector<int32_t> path{vec}; | ||
path.push_back(head->left->val); | ||
head->left->val += head->val; | ||
now.emplace(head->left, path); | ||
} | ||
if (head->right != nullptr) { | ||
vector<int32_t> path{vec}; | ||
path.push_back(head->right->val); | ||
head->right->val += head->val; | ||
now.emplace(head->right, path); | ||
} | ||
} | ||
return sums; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
MIT License | ||
CS203_DSAA_template | ||
Copyright (C) 2020-2022 nanoseeds | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
//@Tag tree | ||
//@Tag 树 | ||
//@description 中序遍历 | ||
//@Plan 数据结构入门 Day12 | ||
//@Plan 剑指OfferII-I Day15 | ||
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_113_TEST_HPP | ||
#define CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_113_TEST_HPP | ||
|
||
#include <catch_main.hpp> | ||
#include <cstdint> | ||
#include <cstddef> | ||
#include <tree/treenode.hpp> | ||
#include <tree/treenode_link.hpp> | ||
#include <vector> | ||
|
||
namespace leetcode_113 { | ||
using std::vector; | ||
using TreeNode = TREE_NODE::TreeNode<int32_t>; | ||
|
||
struct leetcode_113 { | ||
static vector<vector<int>> pathSum(TreeNode *root, int target); | ||
}; | ||
|
||
using Catch::Matchers::Equals; | ||
using TreeNodeLink = TREE_NODE::TreeNodeLink<int32_t>; | ||
|
||
TEST_CASE("test_case 1 [test_113]", "[test_113]") { | ||
const vector<int32_t> input{5, | ||
4, 8, | ||
11, TreeNode::No, 13, 4, | ||
7, 2, TreeNode::No, TreeNode::No, TreeNode::No, TreeNode::No, 5, 1 | ||
}; | ||
static constexpr const auto target{22}; | ||
const vector<TreeNode *> numVecInput = TREE_NODE::numToTree<int32_t>(input); | ||
const vector<vector<int32_t>> output{{5, 4, 11, 2}, | ||
{5, 8, 4, 5}}; | ||
const TreeNodeLink link{numVecInput[0]}; | ||
CHECK_THAT(output, Equals(leetcode_113::pathSum(numVecInput[0], target))); | ||
} | ||
|
||
TEST_CASE("test_case 2 [test_113]", "[test_113]") { | ||
const vector<int32_t> input{1, | ||
2, 3}; | ||
static constexpr const auto target{5}; | ||
const vector<vector<int32_t>> output{}; | ||
const vector<TreeNode *> numVecInput = TREE_NODE::numToTree<int32_t>(input); | ||
const TreeNodeLink link{numVecInput[0]}; | ||
CHECK_THAT(output, Equals(leetcode_113::pathSum(numVecInput[0], target))); | ||
} | ||
|
||
TEST_CASE("test_case 3 [test_113]", "[test_113]") { | ||
const vector<int32_t> input{1, | ||
2}; | ||
static constexpr const auto target{0}; | ||
const vector<vector<int32_t>> output{}; | ||
const vector<TreeNode *> numVecInput = TREE_NODE::numToTree<int32_t>(input); | ||
const TreeNodeLink link{numVecInput[0]}; | ||
CHECK_THAT(output, Equals(leetcode_113::pathSum(numVecInput[0], target))); | ||
} | ||
} | ||
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_113_TEST_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
MIT License | ||
CS203_DSAA_template | ||
Copyright (C) 2020-2022 nanoseeds | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
#include "leetcode_426_test.hpp" | ||
#include <stack> | ||
|
||
namespace leetcode_426 { | ||
using std::stack; | ||
|
||
Node *leetcode_426::treeToDoublyList(Node *root) { | ||
if (root == nullptr) { | ||
return nullptr; | ||
} | ||
Node *base{nullptr}; | ||
Node *head = root; | ||
bool temp{false}; | ||
for (stack<Node *> sta; head != nullptr || !sta.empty(); head = head->right) { | ||
for (; head != nullptr; head = head->left) { | ||
sta.push(head); | ||
} | ||
head = sta.top(); | ||
sta.pop(); | ||
if (base != nullptr) { | ||
base->right = head; | ||
} | ||
head->left = base; | ||
if (!temp) { | ||
root = head; | ||
temp = true; | ||
} | ||
base = head; | ||
} | ||
root->left = base; | ||
base->right = root; | ||
return root; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
MIT License | ||
CS203_DSAA_template | ||
Copyright (C) 2020-2022 nanoseeds | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
//@Tag tree | ||
//@Tag 树 | ||
//@Plan 数据结构入门 Day14 | ||
//@Plan 剑指OfferII-I Day15 | ||
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_426_TEST_HPP | ||
#define CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_426_TEST_HPP | ||
|
||
#include <catch_main.hpp> | ||
#include <cstdint> | ||
#include <cstddef> | ||
#include <tree/treenode.hpp> | ||
#include <tree/treenode_link.hpp> | ||
#include <vector> | ||
|
||
namespace leetcode_426 { | ||
|
||
using TreeNode = TREE_NODE::TreeNode<int32_t>; | ||
using Node = TreeNode; | ||
|
||
struct leetcode_426 { | ||
static Node *treeToDoublyList(Node *root); | ||
}; | ||
|
||
using TreeNodeLink = TREE_NODE::TreeNodeLink<int32_t>; | ||
using TREE_NODE::numToTree; | ||
|
||
TEST_CASE("test_case 1 [test_426]", "[test_426]") { | ||
const vector<int32_t> input{4, 2, 5, 1, 3}; | ||
const vector<TreeNode *> numVecInput = numToTree<int32_t>(input); | ||
const TreeNodeLink link{numVecInput}; | ||
leetcode_426::treeToDoublyList(numVecInput.front()); | ||
CHECK(3 == numVecInput[0]->left->val); | ||
CHECK(5 == numVecInput[0]->right->val); | ||
CHECK(1 == numVecInput[1]->left->val); | ||
CHECK(3 == numVecInput[1]->right->val); | ||
CHECK(4 == numVecInput[2]->left->val); | ||
CHECK(1 == numVecInput[2]->right->val); | ||
CHECK(5 == numVecInput[3]->left->val); | ||
CHECK(2 == numVecInput[3]->right->val); | ||
CHECK(2 == numVecInput[4]->left->val); | ||
CHECK(4 == numVecInput[4]->right->val); | ||
} | ||
} | ||
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_426_TEST_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
MIT License | ||
CS203_DSAA_template | ||
Copyright (C) 2022 nanoseeds | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
#include "leetcode_so_54_test.hpp" | ||
#include "traverse.cpp" | ||
|
||
namespace leetcode_so_54 { | ||
using namespace Tree_Traverse; | ||
|
||
int32_t leetcode_so_54::kthLargest(TreeNode *root, int32_t k) { | ||
vector<int32_t> countVec; | ||
const auto functionPre = [&countVec](const TreeNode *tn) -> void { | ||
countVec.push_back(tn->val); | ||
}; | ||
iter::in(root, functionPre); | ||
const auto netElements = countVec.size(); | ||
vector<int32_t> element; | ||
static size_t count{0}; | ||
const auto function = [&element, k, netElements](const TreeNode *tn) -> void { | ||
++count; | ||
if (count + k == netElements + 1) { element.push_back(tn->val); } | ||
}; | ||
iter::in(root, function); | ||
count = 0; | ||
return element.front(); | ||
} | ||
|
||
} |
Oops, something went wrong.