上一页 1 2 3 4 5 6 7 8 9 ··· 23 下一页
思路 1、判断左子树是否为空,若为空则直接往右走,若不为空则22、将当前节点root的右子树接到当前root节点的左孩子节点的最右下边的孩子节点3、将当前节点root的左子树接到右子树上,并将左节点置为NULL。 /** * Definition for a binary tree node. * Read More
posted @ 2021-09-26 16:04 A-inspire Views(30) Comments(0) Diggs(0)
思路 代码: class Solution { public: vector<int> countBits(int num) { vector<int>result(num+1); result[0] = 0; for(int i =1;i<=num;i++) { if(i%2 == 0){ res Read More
posted @ 2021-09-26 16:03 A-inspire Views(27) Comments(0) Diggs(0)
思路: 先对数组进行排序,然后遍历寻找连续序列并计数,注意相等的情况 代码 class Solution { public: int longestConsecutive(vector<int>& nums) { if(nums.empty()) return 0; sort(nums.begin( Read More
posted @ 2021-09-26 16:00 A-inspire Views(21) Comments(0) Diggs(0)
思路:回溯 递归 代码 class Solution { public: void back_track_Dfs(vector<string>&res,string path,int n,int lc,int rc) { if(rc>lc||rc>n||lc>n)//不满足条件 { return; Read More
posted @ 2021-09-26 15:59 A-inspire Views(33) Comments(0) Diggs(0)
# 思路深度优先遍历遍历整个数组,遇到1,ans++,ans是记录岛的个数的运行一下dfs函数,把这个岛所有陆地给我沉喽,这个岛全部的1变成0等把grid全遍历完,grid就全是0了,再把ans输出,这个ans就是我们记录的岛的个数注意:grid竟然是char类型的,所有1和0都要加单引号哦 代码 Read More
posted @ 2021-09-26 15:57 A-inspire Views(48) Comments(0) Diggs(0)
思路 动态规划 斐波拉契呀数列 代码 class Solution { public: int climbStairs(int n) { vector<int>res_dp(n+5,0); res_dp[1] = 1; res_dp[2] = 2; for(int i = 3;i<=n;i++) { Read More
posted @ 2021-09-26 15:56 A-inspire Views(22) Comments(0) Diggs(0)
思路 深度优先遍历:回溯,递归(终止条件:选择的当前数字数量等于原始数组的大小) 状态变量1.递归到第几层,depth2.已经选择了哪些数Path3.布尔数组 used非叶子节点选择分枝 代码: class Solution { public: void backtrack(vector<vecto Read More
posted @ 2021-09-26 15:55 A-inspire Views(24) Comments(0) Diggs(0)
方法1:递归思路 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left Read More
posted @ 2021-09-26 15:53 A-inspire Views(24) Comments(0) Diggs(0)
方法1 递归解法: 左右根 代码 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val( Read More
posted @ 2021-09-26 15:50 A-inspire Views(41) Comments(0) Diggs(0)
前序遍历迭代算法: 二叉树的前序遍历二叉树的遍历,整体上看都是好理解的。三种遍历的迭代写法中,数前序遍历最容易理解。递归思路:先树根,然后左子树,然后右子树。每棵子树递归。 代码: /** * Definition for a binary tree node. * struct TreeNode Read More
posted @ 2021-09-26 15:49 A-inspire Views(43) Comments(0) Diggs(0)
上一页 1 2 3 4 5 6 7 8 9 ··· 23 下一页