-
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.
- Loading branch information
liujj
committed
Jul 9, 2022
1 parent
580b2d1
commit 3a75843
Showing
5 changed files
with
242 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <iostream> | ||
#include <cmath> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <vector> | ||
#include <stack> | ||
#include <queue> | ||
#include <string> | ||
#include <map> | ||
#include <set> | ||
#include <algorithm> | ||
using namespace std; | ||
|
||
int maxSubArray(vector<int>& nums) { | ||
int pre = 0; | ||
int maxAns = nums[0]; | ||
for (const auto &x : nums) { | ||
pre = max(pre + x, x); | ||
maxAns = max(maxAns, pre); | ||
cout << "pre: " << pre << " ,maxAns: " << maxAns << endl; | ||
} | ||
|
||
return maxAns; | ||
} | ||
|
||
int main() { | ||
vector<int> nums = {-2,1,-3,4,-1,2,1,-5,4}; | ||
|
||
cout << maxSubArray(nums) << endl; | ||
|
||
return 0; | ||
} |
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,39 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <set> | ||
#include <algorithm> | ||
using namespace std; | ||
bool isStraight(vector<int>& nums) { | ||
// set<int> ans; | ||
// int zero = 0; | ||
|
||
// for (int i : nums) { | ||
// if (ans.find(i) != ans.end()) return false; | ||
// if (i) ans.insert(i); | ||
|
||
// } | ||
|
||
// return ((*ans.rbegin() - *ans.begin()) < 5); | ||
|
||
//用一个桶,用来计数 | ||
int bucket[14] = {0}; // 因为有14张牌 | ||
for (int num : nums){ | ||
//如果出现重复的牌,且该牌不是大小王0,则返回false。 | ||
if (num != 0 && bucket[num] > 0) return false; | ||
//桶计数 | ||
bucket[num]++; | ||
} | ||
//用双指针定位到最大的牌和最小的牌 | ||
int left = 1; | ||
int right = 13; | ||
while (bucket[left] == 0) left++; | ||
while (bucket[right] == 0) right--; | ||
return right - left + 1 <= 5; | ||
} | ||
|
||
int main() { | ||
vector<int> nums = {4,7,5,9,2}; | ||
// vector<vector<int> > num = {{1,2,3,4,5}, {123}}; | ||
cout << isStraight(nums) << endl;; | ||
return 0; | ||
} |
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,65 @@ | ||
#include <iostream> | ||
#include <queue> | ||
#include <algorithm> | ||
using namespace std; | ||
// Definition for a binary tree node. | ||
struct TreeNode { | ||
int val; | ||
TreeNode *left; | ||
TreeNode *right; | ||
TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
}; | ||
|
||
int minDepth(TreeNode* root) { | ||
// 深度优先 O(N) | ||
if (nullptr == root) return 0; | ||
|
||
if (root->left == nullptr && root->right == nullptr) { | ||
return 1; | ||
} | ||
int min_depth = INT32_MAX; | ||
|
||
if (nullptr != root->left) { | ||
min_depth = min(minDepth(root->left), min_depth); | ||
} | ||
|
||
if (nullptr != root->right) { | ||
min_depth = min(minDepth(root->right), min_depth); | ||
} | ||
|
||
return min_depth + 1; | ||
} | ||
|
||
int minDepth(TreeNode* root) { | ||
if (nullptr == root) return 0; | ||
|
||
std::queue<std::pair<TreeNode *, int>> que; | ||
que.push(make_pair(root, 1)); | ||
|
||
while (!que.empty()) { | ||
TreeNode *node = que.front().first; | ||
int depth = que.front().second; | ||
que.pop(); | ||
|
||
if (nullptr == node->left && nullptr == node->right) { | ||
return depth; | ||
} | ||
|
||
if (nullptr != node->left) { | ||
que.push(node->left, depth + 1); | ||
} | ||
|
||
id (nullptr != node->right) { | ||
que.push(node->right, depth + 1); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
int main() { | ||
// cout << min_depth << endl; | ||
|
||
return 0; | ||
} |
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,55 @@ | ||
#include <iostream> | ||
#include <queue> | ||
using namespace std; | ||
|
||
struct TreeNode { | ||
int val; | ||
TreeNode *left; | ||
TreeNode *right; | ||
TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
}; | ||
|
||
/* | ||
TreeNode* invertTree(TreeNode* root) { | ||
// 终止条件 | ||
if (nullptr == root) return nullptr; | ||
// 左子树翻转 | ||
TreeNode *left = invertTree(root->left); | ||
// 右子树翻转 | ||
TreeNode *right = invertTree(root->right); | ||
root->right = left; | ||
root->left = right; | ||
return root; | ||
} | ||
*/ | ||
|
||
TreeNode* invertTree(TreeNode* root) { | ||
if (nullptr == root) return nullptr; | ||
|
||
queue<TreeNode *> que; | ||
que.push(root); | ||
while (!que.empty()) { | ||
TreeNode *tmp = que.front(); | ||
que.pop(); | ||
|
||
TreeNode *left = tmp->left; | ||
tmp->left = tmp->right; | ||
tmp->right = tmp; | ||
if (nullptr != tmp->left) que.push(tmp->left); | ||
if (nullptr != tmp->right) que.push(tmp->right); | ||
} | ||
|
||
return root; | ||
} | ||
|
||
int main() { | ||
|
||
TreeNode *root = invertTree(new TreeNode(4, new TreeNode(2), new TreeNode(7))); | ||
cout << root->val << ", " << root->left->left << ", "<< root->right << endl; | ||
return 0; | ||
} |