本文主要是介绍代码随想录算法训练营第三十七天|● 738.单调递增的数字 ● 968.监控二叉树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
-
738.单调递增的数字
-
class Solution { public:int monotoneIncreasingDigits(int n) {string a = to_string(n);int k = 0;for (int i = a.size() - 1; i > 0; i--) {if (a[i] < a[i - 1]) {k = i;a[i-1]--;}}cout << k << endl;if (k != 0){for (int i = a.size() - 1; i >= k; i--) {a[i] = '9';}}return stoi(a); } } ;
-
968.监控二叉树
-
class Solution { public:int ans = 0;// 0是未被覆盖,1是摄像头位,2是被覆盖int dfs(TreeNode* node) {if (node==NULL)return 2;int left = dfs(node->left);int right = dfs(node->right);if (left == 0 || right == 0) {ans++;return 1;}if (left == 1 || right == 1) {return 2;}return 0;}int minCameraCover(TreeNode* root) {if (dfs(root) == 0)ans++;return ans;} };
这篇关于代码随想录算法训练营第三十七天|● 738.单调递增的数字 ● 968.监控二叉树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!