Summary RangesGiven a sorted integer array without duplicates, return the summary of its ranges.For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].https://leetcode.com/problems/summary-ranges/ 简单的模拟题,合并连续的数字,test case排过序而且不会重复。 1/**2 * @param {number[]} nums3 * @return {string[]}4*/ 5var summaryRanges = function(nums) {6var result = [];7var start = null;8for...
Maximum SubarrayFind the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [?2,1,?3,4,?1,2,1,?5,4],the contiguous subarray [4,?1,2,1] has the largest sum = 6.https://leetcode.com/problems/maximum-subarray/ 找出和最大的子串。动态规划 ,维护一个变量previous,记录之前的最大值。当前的最大值就是Math.max(previous + nums[i], num...
壹 ? 引今天来做一道十分烦躁的题目,为什么烦躁,因为我字母写错了提交了三次错了三次!!!我的leetcode正确率大大下降!!那么这道题是leetcode的485. 最大连续1的个数,题目描述如下:给定一个二进制数组, 计算其中最大连续1的个数。示例 1:输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意:输入的数组只包含 0 和1。 输入数组的长度是正整数,且不超过 10,000。我们先简...
Combination Sum IIGiven a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combination.Note:All numbers (including target) will be positive integers.Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).The solution set ...
https://leetcode.com/problems/valid-sudoku/Valid SudokuDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character ‘.‘.A partially filled sudoku which is valid. Note:A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated. 正好用了一下最...
Lowest Common Ancestor of a Binary Search TreeGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).” _______6_____...
Copy List with Random PointerA linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.https://leetcode.com/problems/copy-list-with-random-pointer/ 第一把直接暴力两轮遍历。第一轮遍历copy链表,用hash表记录下各个节点,第二乱遍历去赋值链表里的random对象。然后稍稍改进了一下,一次遍历里把能的找到...
题目描述189. 旋转数组给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。示例 1:输入: [1,2,3,4,5,6,7] 和 k = 3输出: [5,6,7,1,2,3,4]解释:向右旋转 1 步: [7,1,2,3,4,5,6]向右旋转 2 步: [6,7,1,2,3,4,5]向右旋转 3 步: [5,6,7,1,2,3,4]示例 2:输入: [-1,-100,3,99] 和 k = 2输出: [3,99,-1,-100]解释: 向右旋转 1 步: [99,-1,-100,3]向右旋转 2 步: [3,99,-1,-100]说明:尽可能想出更多的解决方案,至少有三种不...
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 ->...