* 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...
[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. Note: All numbers (i...
LeetCode -- Minimum Size Subarray Sum Question: Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array[2,3,1,2,4,3]ands = 7, the subarray[...
package leetcode func splitArray(nums []int, m int) int { maxNum, sum := 0, 0 for _, num := range nums { sum += num if num > maxNum { maxNum = num } } if m == 1 { return sum } low, high := maxNum, sum for low < high { mid := low + (high-low)>>1 if ...
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...
题目链接:https://leetcode.com/problems... dp,可以brute force,不过这题可以memory,所以可以iteration用dp table做或者recursion:dfs+suffix array来做,还是得用二维数组保存算过的结果。这题的subproblem是到第i个数时,能够得到sum为j的ways数量,由于sum可能是负数,所以要做一个shift,使其从0开始。滚动数组优化...
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...
Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base without leading zeroes. Input The single line of the input contains a pair of integers m, s (1 ...
题目要求出总和为 sum 的所有组合,组合需要去重。 这一题和第 47 题类似,只不过元素可以反复使用。 代码# Go packageleetcodeimport"sort"funccombinationSum(candidates[]int,targetint)[][]int{iflen(candidates)==0{return[][]int{}}c,res:=[]int{},[][]int{}sort.Ints(candidates)findcombinationSum...
https://leetcode.com/problems/partition-equal-subset-sum/discuss/90592/01-knapsack-detailed-explanationNice solution. I just figured out the differences betweenthisproblem and 518. Coin Change 2. Inthisproblem, we CANNOT reuse an elementwhileit can be reused in LT518. That is why we have dp...