738.单调递增的数字
讲解链接:https://programmercarl.com/0738.%E5%8D%95%E8%B0%83%E9%80%92%E5%A2%9E%E7%9A%84%E6%95%B0%E5%AD%97.html
class Solution {
public:int monotoneIncreasingDigits(int n) {//整数转字符串,变为字符串访问比诸位取出数字要好。string strNum = to_string(n);int flag = strNum.size();for(int i=strNum.size()-1;i>=0;i--) {//找到从哪个位置赋值为9if(strNum[i-1]>strNum[i]) {//i-1处减一,从i往后都赋为9flag = i;strNum[i-1] --;}}for(int i=flag;i<strNum.size();i++) {strNum[i]='9';}//将字符串转为十进制return stoic(strNum);}
};
968.监控二叉树
讲解链接:https://programmercarl.com/0968.%E7%9B%91%E6%8E%A7%E4%BA%8C%E5%8F%89%E6%A0%91.html
/*** 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) {}* };*/
class Solution {
public:int result;int traversal(TreeNode* cur) {//将空结点看作是有覆盖(标记为2)if(cur==NULL)return 2;int left = traversal(cur->left);int right = traversal(cur->right);//左右节点都有覆盖if(left == 2 && right ==2)return 0;//左右至少有一个节点没有被覆盖else if(left==0 || right==0) {result++;return 1;}elsereturn 2;}int minCameraCover(TreeNode* root) {result = 0;if(traversal(root) ==0)result++;return result;}};
代码随想录贪心算法总结
https://programmercarl.com/%E8%B4%AA%E5%BF%83%E7%AE%97%E6%B3%95%E6%80%BB%E7%BB%93%E7%AF%87.html#%E6%80%BB%E7%BB%93
文中最后有一张贪心算法题目的思维导图,真的很详细
最近的OneNote笔记同步掉链子了,手写的思路没有放上…