* int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * }*/publicclassSolution {publicintsumOfLeftLeaves(TreeNode root) {if(root==null)return0;elseif(root.left!=null&& root.left.left==null&& root.left.right==null)returnroot.left.val+sumOfLeftLeaves(roo...
sum+=sumOfLeftLeaves(root.left); sum+=sumOfLeftLeaves(root.right);returnsum; } 解法二:非递归,使用栈 如果不想采用递归的方法的话,可以考虑使用栈,遍历所有的节点,找出复合条件的节点计算即可。贴上了leetcode上给出的别人写的代码参考一下作为学习 publicstaticintsumOfLeftLeaves(TreeNode1 root) {intsu...
2.不符合上面那种情况的从当前节点劈开为两颗子树分别统计相加即可 AC代码: /*** Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * }*/publicclassSolution {publicintsumOfLeftLeaves(TreeNode roo...
package leetcode import ( "sort" ) func combinationSum2(candidates []int, target int) [][]int { if len(candidates) == 0 { return [][]int{} } c, res := []int{}, [][]int{} sort.Ints(candidates) // 这里是去重的关键逻辑 findcombinationSum2(candidates, target, 0, c, &res) ...
还有leetcode 474. Ones and Zeroes若干0和1组成字符串最大数量+动态规划DP+背包问题 代码如下: #include <iostream> #include <vector> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <stack> ...
[Leetcode] Combination Sum 组合数之和 Combination Sum I Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times....
0058-length-of-last-word.go 0062-unique-paths.go 0063-unique-paths-ii.go 0066-plus-one.go 0070-climbing-stairs.go 0073-set-matrix-zeroes.go 0074-search-a-2d-matrix.go 0075-sort-colors.go 0076-minimum-window-substring.go 0077-combinations.go 0078-subsets.go 0079-word-search.go 0083-remov...
0283-move-zeroes.cpp 0286-walls-and-gates.cpp 0287-find-the-duplicate-number.cpp 0290-word-pattern.cpp 0295-find-median-from-data-stream.cpp 0297-serialize-and-deserialize-binary-tree.cpp 0300-longest-increasing-subsequence.cpp 0303-range-sum-query-immutable.cpp 0304-range-sum-query-2d-immutab...
题目链接:https://leetcode.com/problems... dp,可以brute force,不过这题可以memory,所以可以iteration用dp table做或者recursion:dfs+suffix array来做,还是得用二维数组保存算过的结果。这题的subproblem是到第i个数时,能够得到sum为j的ways数量,由于sum可能是负数,所以要做一个shift,使其从0开始。滚动数组优化...
Sum of Square Numbers 2. Solution 代码语言:javascript 代码运行次数:0 运行 classSolution{public:booljudgeSquareSum(int c){int root=int(sqrt(c))+1;for(int i=0;i<root;i++){int difference=c-i*i;int j=int(sqrt(difference));if(i*i+j*j==c){returntrue;}}returnfalse;}};...