马上要开始找实习了,又开始重启刷题计划了!加油冲冲冲!刷题的顺序follow代码随想录的60天刷题计划!感谢FuCosmo的总结!之前都是按照C++的语法进行刷题的,这次也同样使用C++。
这些题过年前都刷过了,所以过的快一些。通过写一些题解的方式来,帮助自己回顾这些方法,记住一些核心点。
class Solution {
public:
int search(vector<int>& nums, int target) {
int left = 0;
int right = nums.size() - 1;
int mid;
while (left <= right) {
mid = (left + right) / 2;
if (nums[mid] == target){
return mid;
} else if (nums[mid] < target){
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
};
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int i = 0;
int j = 0;
while (i < nums.size()) {
if (nums[i] != val) {
nums[j] = nums[i];
j++;
}
i++;
}
return j;
}
};
vector ans; vector ans(n); ans.push_back(num);sort(ans.begin(), ans.end());class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
int i = 0;
vector<int> ans;
while (i < nums.size()){
ans.push_back(nums[i] * nums[i]);
i++;
}
sort(ans.begin(), ans.end());
return ans;
}
};
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
int i = 0;
vector<int> ans;
for (int num : nums) {
if (num < 0) {
i++;
} else {
break;
}
}
if (i == 0) {
for (int num : nums) {
ans.push_back(num * num);
}
return ans;
} else {
int j = i - 1;
while (i < nums.size() && j >= 0) {
if (nums[i] * nums[i] < nums[j] * nums[j]) {
ans.push_back(nums[i] * nums[i]);
i++;
} else {
ans.push_back(nums[j] * nums[j]);
j--;
}
}
while (i < nums.size()) {
ans.push_back(nums[i] * nums[i]);
i++;
}
while (j >= 0){
ans.push_back(nums[j] * nums[j]);
j--;
}
}
return ans;
}
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
int n = nums.size();
vector<int> ans(n);
for (int i = 0, j = n - 1, pos = n - 1; i <= j; pos--) {
if (nums[i] * nums[i] > nums[j] * nums[j]) {
ans[pos] = nums[i] * nums[i];
i++;
} else {
ans[pos] = nums[j] * nums[j];
j--;
}
}
return ans;
}
};
条件表达式?True : Falseint ans = INT_MAX// 有点冗余的写法
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int left = 0;
int right = left + 1;
int cnt = nums[0];
int n = nums.size();
int minlength = n + 1;
while(right < n) {
if (cnt < target) {
cnt += nums[right];
right++;
} else if (cnt >= target) {
if ((right - left) < minlength) {
minlength = right - left;
}
cnt -= nums[left];
left++;
}
}
// 当right已经走到最右端了,但cnt依旧大于0
while(left < n) {
if (cnt >= target) {
if ((right - left) < minlength) {
minlength = right - left;
}
cnt -= nums[left];
left++;
} else {
break;
}
}
return (minlength > n) ? 0 : minlength;
}
};
// 看完题解后的优化代码
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int left = 0;
int right = 0;
int cnt = 0;
int n = nums.size();
int minlength = n + 1;
while(right < n) {
cnt += nums[right];
while (cnt >= target) {
minlength = min(minlength, right - left + 1);
cnt -= nums[left];
left++;
}
right++;
}
return (minlength > n) ? 0 : minlength;
}
};
push(), pop(), top(), empty(), size();priority_queue,greater>q emplace()与emplace_back()来代替insert()与push_back()class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int,int> ans;
for (int num : nums) {
ans[num]++;
}
// 排序
priority_queue<pair<int, int>> q;
for (auto item : ans) {
q.emplace(item.second, item.first);
}
//求解
vector<int> res;
while(k) {
res.emplace_back(q.top().second);
q.pop();
--k;
}
return res;
}
};
今天主要是一些理论学习
二叉树的分类
二叉树的存储方式
如果父节点的数组下标为i,则左孩子为i * 2 + 1, 右孩子为i * 2+ 2数组可以用来表示二叉树
二叉树的遍历方法
二叉树的定义方法
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int X) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == NULL) return root;
swap(root->left, root->right);
invertTree(root->left);
invertTree(root->right);
return root;
}
};
class Solution {
public:
bool compare(TreeNode* left, TreeNode* right) {
// 首先比较空节点的情况
if (left == NULL && right != NULL) return false;
else if (left != NULL && right == NULL) return false;
else if (left == NULL && right == NULL) return true;
// 比较数值不一致的情况
else if (left->val != right->val) return false;
// 比较左子树的左节点和右子树的右节点
bool outside = compare(left->left, right->right);
// 比较左子树的右节点和左子树的左节点
bool inside = compare(left->right, right->left);
bool isSame = outside && inside;
return isSame;
}
bool isSymmetric(TreeNode* root) {
if (root == nullptr) return true;
return compare(root->left, root->right);
}
};