参考:https://www.bilibili.com/video/BV14f4y1C7hg 宝藏up主!1.时间复杂度O(1)O(n)复杂度看最高的O(n2) 如果只是两个并列的for循环,时间复杂度还是O(n),100个并列的for循环,也是O(n)这里有继承,两个循环分摊一个任务O(logn)二分搜索O(nlogn)排序优化的方法:从低-级的复杂度寻找灵感O(n)->O(logn)使用二分搜索O(nlogn) -> O(n)遇到需要排序的题,想想能否通过数组,set, map,heap解O(n2)-> O(nlogn)遇到嵌套循环,想想能不能...
Bulb SwitcherThere are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it‘s off or turning off if it‘s on). For the nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.Example:Given n = 3. At first, the three bulbs are [off, off, off]. After first...
https://leetcode.com/problems/longest-valid-parentheses/Longest Valid ParenthesesGiven a string containing just the characters ‘(‘ and ‘)‘, find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is "()", which has length = 2.Another example is ")()())", where the longest valid parentheses substring is "()()", which has length...
Pow(x, n)Implement pow(x, n).https://leetcode.com/problems/powx-n/ 注意x和n都可能是负数。递归求解,比如求3的4次方,可以拆成3的2次方相乘;3的5次就是3^2相乘再乘2。 1/**2 * @param {number} x3 * @param {number} n4 * @return {number}5*/ 6var myPow = function(x, n) {7if(n >= 0){8return pow(Math.abs(n));9 }else{ 10return 1 / pow(Math.abs(n)); 11 } 1213function pow(n){ 14var temp = 0; 15if(n ===...
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...