Binary Tree Level Order Traversal IIGiven a binary tree, return the bottom-up level order traversal of its nodes‘ values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7}, 3/ 9 20/ 15 7
return its bottom-up level order traversal as:[[15,7],[9,20],[3]
]https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ 与...
Combination Sum IIIFind all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Ensure that numbers within the set are sorted in ascending order.Example 1:Input: k = 3, n = 7Output:[[1,2,4]]
Example 2:Input: k = 3, n = 9Output:[[1,2,6], [1,3,5], [2,3,4]]https://leetcode.com/problems/co...
Patching ArrayGiven a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n] inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches required.Example 1:nums = [1, 3], n = 6Return 1.Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4.Now if we add/patch 2 to n...
Add and Search Word - Data structure designDesign a data structure that supports the following two operations:void addWord(word)
bool search(word)
search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.For example:addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
searc...
【JavaScript】Leetcode每日一题-组合总和4【题目描述】给你一个由 不同 整数组成的数组 nums ,和一个目标整数 target 。请你从 nums 中找出并返回总和为 target 的元素组合的个数。题目数据保证答案符合 32 位整数范围。示例1:输入:nums = [1,2,3], target = 4
输出:7
解释:
所有可能的组合为:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
请注意,顺序不同的序列被视作不同的组合。
示例2:输入:nu...
解题思路分析:该题是在一个整数数组中找到一个和最大的连续子数组,并返回和值。那么如何找到一个和最大的连续子数组呢?我们知道,这肯定需要遍历数组才行;好,那我们就开始遍历数组。首先,我们初始化最大和 sum 和当前和 currSum,对于 currSum,如果它小于0,我们就将数组中下一值赋给它;否则就将数组中下一值与其相加。然后,我们取当前 sum 和 currSum 的最大值即可。代码实现:var maxSubArray = function(nums) {//首先...
Number of Digit OneGiven an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 数学题,真是为难了数学拙计的我了。递归分治,拿8192举栗子:把8192拆成:1-999 -> 递归(999)1000-1999 -> 1000个1 + 递归(999)2001-2999 -> 递归(999)..8000-8192 ->...
Remove Invalid ParenthesesRemove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.Note: The input string may contain letters other than the parentheses ( and ).Examples:"()())()" -> ["()()()", "(())()"]
"(a)())()" -> ["(a)()()", "(a())()"]
")(" -> [""]https://leetcode.com/problems/remove-invalid-parentheses/ ( 和 )总是成对出现的,...
Nim GameYou are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game give...
壹 ? 引题目来自LeetCode的525. 连续数组,难度中等,题目描述如下:给定一个二进制数组 nums , 找到含有相同数量的 0 和 1 的最长连续子数组,并返回该子数组的长度。示例 1:输入: nums = [0,1]
输出: 2
说明: [0, 1] 是具有相同数量 0 和 1 的最长连续子数组。
示例 2:输入: nums = [0,1,0]
输出: 2
说明: [0, 1] (或 [1, 0]) 是具有相同数量0和1的最长连续子数组。
提示:1 <= nums.length <= 105
nums[i] 不是 0 就是 1贰 ? 题...
题目描述:给定一个二叉树,检查它是否是镜像对称的。题目分析下面这种二叉树就是镜像对称的,符合题目要求: 1/ 2 2/ \ / 3 4 4 3解法 1:递归检查根据题目“对称”的定义,递归过程如下:对称节点的 val 是否相同依次递归对称节点的 left1 和 right2、right1 和 left2(结合上面的例子更好理解)代码实现如下:// ac地址:https://leetcode-cn.com/problems/symmetric-tree/
// 原文地址:https://xxoo521.com/2020-02-...
Search a 2D MatrixWrite an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right.The first integer of each row is greater than the last integer of the previous row. For example,Consider the following matrix:[[1, 3, 5, 7],[10, 11, 16, 20],[23, 30, 34, 50]
]
Given target = 3, return true.http...
壹 ? 引本题来自LeetCode155. 最小栈,难度简单,题目描述如下:设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。push(x) —— 将元素 x 推入栈中。
pop() —— 删除栈顶的元素。
top() —— 获取栈顶元素。
getMin() —— 检索栈中的最小元素。示例:输入:["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]
输出:[null,null,null,null,-3,null,0,-2]
...
258. Add DigitsDigit root 数根问题/*** @param {number} num* @return {number}*/var addDigits = function(num) {var b = (num-1) % 9 + 1 ;return b;
};//之所以num要-1再+1;是因为特殊情况下:当num是9的倍数时,0+9的数字根和0的数字根不同。 性质说明 1.任何数加9的数字根还是它本身。(特殊情况num=0) 小学学加法的时候我们都明白,一个数字加9,就是把十位加1,各位减1。因此十位加个位的和是不变的;如果有进位,...
原题连接:https://leetcode-cn.com/problems/ugly-number-ii/
解题思路:
该题可使用堆解决,利用了堆能够快速插入和取出元素,并始终能够按要求排序的特点。创建一个小顶堆,初始状态下堆中存储元素1,即为第一个丑数。第一次遍历刚好可以计算出下一组丑数2、3、4。因为堆中元素一直保持了从小到大排序,假设堆中已经存储了所有丑数,那么只需要从堆中取出n个数即可。我们无需在每次运行时都计算出所有的丑数,再进行取出操作,每...