![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
onefanyf
这个作者很懒,什么都没留下…
展开
-
17. Letter Combinations of a Phone Number
#17. Letter Combinations of a Phone Number这道题是一道状态更新的问题。基本的逻辑是,digits数组每一次循环,向之前得到的字符串数组增加新的字符。var letterCombinations = function(digits) { if(digits.length===0) { return []; } le...原创 2020-02-21 01:18:00 · 90 阅读 · 0 评论 -
206. Reverse Linked List
#206. Reverse Linked List把一个链表倒转var reverseList = function(head) { if(!head){ return head; } let new_head = head; let current = null; while(head.next) { ...原创 2020-01-24 07:27:04 · 91 阅读 · 0 评论 -
64. Minimum Path Sum
#64. Minimum Path SumGiven a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move eith...原创 2020-01-24 07:03:19 · 72 阅读 · 0 评论 -
67. Add Binary
#67. Add BinaryGiven two binary strings, return their sum (also a binary string).The input strings are both non-empty and contains only characters 1 or 0.Example 1:Input: a = “11”, b = “1”Output...原创 2020-01-24 06:10:09 · 86 阅读 · 0 评论 -
328. Odd Even Linked List
#328. Odd Even Linked ListExample 1:Input: 1->2->3->4->5->NULLOutput: 1->3->5->2->4->NULLExample 2:Input: 2->1->3->5->6->4->7->NULLOutput: 2...原创 2020-01-24 05:46:57 · 62 阅读 · 0 评论 -
11. Container With Most Water
#11. Container With Most WaterGiven n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at...原创 2020-01-24 05:20:52 · 54 阅读 · 0 评论 -
228. Summary Ranges
#228. Summary RangesExample 1:Input: [0,1,2,4,5,7]Output: [“0->2”,“4->5”,“7”]Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.Example 2:Input: [0,2,3,4,6,8,9]O...原创 2020-01-24 02:39:13 · 71 阅读 · 0 评论 -
63. Unique Paths II
#63. Unique Paths IIA robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is tr...原创 2020-01-18 05:38:11 · 62 阅读 · 0 评论 -
53. Maximum Subarray
#53. Maximum SubarrayGiven an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.这是一个动态规划的问题。当前状态的最大值是上一个状态和上一个状态+当前值的最大值。...原创 2020-01-18 04:24:44 · 67 阅读 · 1 评论 -
459. Repeated Substring Pattern
#459. Repeated Substring PatternGiven a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given s...原创 2020-01-18 03:58:16 · 66 阅读 · 0 评论 -
56. Merge Intervals
#56. Merge Intervals这道题是一道求并集的题。首先把给的数组数组按照数组子项的第0项大小排序。然后比较相邻的两个数组子项,前一项的第1项和后一项的第0项。var merge = function(intervals) { if(intervals.length<2) return intervals; intervals.sort((a,b) =>...原创 2020-01-18 03:42:42 · 76 阅读 · 0 评论 -
20. Valid Parentheses
#20. Valid ParenthesesGiven a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.An input string is valid if:1.Open brackets must be close...原创 2020-01-18 01:35:08 · 55 阅读 · 0 评论 -
205. Isomorphic Strings
#205. Isomorphic StringsGiven two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be...原创 2020-01-18 01:25:00 · 63 阅读 · 0 评论 -
686. Repeated String Match
#686. Repeated String MatchGiven two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1.For example, with A = “ab...原创 2020-01-18 00:46:24 · 80 阅读 · 0 评论 -
31. Next Permutation
#31. Next PermutationImplement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as ...原创 2020-01-17 09:18:51 · 131 阅读 · 0 评论 -
543. Diameter of Binary Tree
#543. Diameter of Binary TreeGiven a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in ...原创 2020-01-17 08:32:13 · 64 阅读 · 0 评论 -
299. Bulls and Cows
#299. Bulls and Cows这道题是给了两个数字字符串,让你分别找到对应位置数字相同的,和数字相同却不在对应位置的数字的数量。首先遍历secret,对应位置相同的很好找。然后把位置不对应的放在map里,然后再遍历一遍。var getHint = function(secret, guess) { let countA = 0; let countB = 0; ...原创 2020-01-17 08:07:16 · 189 阅读 · 0 评论 -
226. Invert Binary Tree
#226. Invert Binary Tree这道题是把树的每一个左右子节点交换。var invertTree = function(root) { if(root !== null) { let temp = root.left; root.left = invertTree(root.right); root.right = in...原创 2020-01-17 03:43:49 · 60 阅读 · 0 评论 -
1. Two Sum
#1. Two SumGiven an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not us...原创 2020-01-17 03:24:17 · 55 阅读 · 0 评论 -
88. Merge Sorted Array
88. Merge Sorted ArrayGiven two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:The number of elements initialized in nums1 and nums2 are m and n respectively...原创 2020-01-17 03:15:57 · 60 阅读 · 0 评论 -
总结一些树的问题
#965. Univalued Binary Tree用DFS来解决这个问题,如果根存在,就把节点的值放在数组中,然后继续遍历它的左右子节点。最后判断数组中元素是否都相等就可以了。相同就是独一无二的树。function dfs(root,val) { if(root) { val.push(root.val); dfs(root.left,val);...原创 2019-11-20 00:06:49 · 188 阅读 · 0 评论 -
总结在Rotate Sorted Array中的问题
文章目录#33. Search in Rotated Sorted Array#81. Search in Rotated Sorted Array II#153. Find Minimum in Rotated Sorted Array#154. Find Minimum in Rotated Sorted Array II#33. Search in Rotated Sorted Array...原创 2019-11-16 02:26:57 · 76 阅读 · 0 评论 -
总结用先序/中序/后序遍历构造树
#105. Construct Binary Tree from Preorder and Inorder Traversal这道题是用先序(根左右)遍历和中序(左根右)遍历来构造树,这棵二叉树的根节点很好确定,就是先序的第一项。因为题目中说这棵树没有重复的项,因此根据这个根元素在中序遍历中的位置,可以分别确定先序和中序数组中左右子树的下标。然后再递归。var buildTree = func...原创 2019-11-16 01:58:33 · 206 阅读 · 0 评论 -
31. Next Permutation
#31. Next Permutation这道题是求比当前排列更大的下一个排列。数字的排列是有顺序的,我们可以发现这个顺序是,数字从个位向前的顺序来看,后一个都应该比前一个大,如果后一个比前一个小,那么这个顺序就存在比它更大的排列。因此我们要做的第一步就是找到这个不再符合后一个比前一个大的数字的位置。当我们找到需要重新排列的子数组arr后,下一步是在子数组中找到比arr[0]大的第一个数,交...原创 2019-11-15 23:55:54 · 53 阅读 · 0 评论 -
总结几道DFS的问题
为了感谢某人花了两天给我讲明白了DFS的问题…我得赶紧把它们记下来…问题来自leetcode,问题的集锦来自https://zhuanlan.zhihu.com/p/62884431#695这道题给了一个二维数组,0表示水,1表示陆地。问连在一起的1最多有几个。a 是岛屿的数组s 是状态数组i,j代表位置def dfs(a, s, i, j, ans)://在dfs中,我们首先要修...原创 2019-10-13 08:57:35 · 252 阅读 · 0 评论 -
1029. Two City Scheduling
#1029. Two City SchedulingThere are 2N people a company is planning to interview. The cost of flying the i-th person to city A is costs[i][0], and the cost of flying the i-th person to city B is cost...原创 2019-10-09 23:13:26 · 113 阅读 · 0 评论 -
696. Count Binary Substrings
#696. Count Binary SubstringsGive a string s, count the number of non-empty (contiguous) substrings that have the same number of 0’s and 1’s, and all the 0’s and all the 1’s in these substrings are g...原创 2019-10-09 22:44:01 · 64 阅读 · 0 评论 -
1071. Greatest Common Divisor of Strings
#1071. Greatest Common Divisor of StringsFor strings S and T, we say “T divides S” if and only if S = T + … + T (T concatenated with itself 1 or more times)Return the largest string X such that X d...原创 2019-10-08 04:13:44 · 97 阅读 · 0 评论 -
653. Two Sum IV - Input is a BST*
#653. Two Sum IV - Input is a BSTGiven a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.这道题的意思是,给一个BST和一...原创 2019-10-08 03:32:43 · 89 阅读 · 0 评论 -
108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search TreeGiven an array where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tree is def...原创 2019-10-08 02:48:11 · 62 阅读 · 0 评论 -
657. Robot Return to Origin
#657. Robot Return to OriginThere is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves....原创 2019-09-19 02:50:00 · 64 阅读 · 0 评论 -
953. Verifying an Alien Dictionary
#953. Verifying an Alien DictionaryIn an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowe...原创 2019-10-09 23:26:21 · 76 阅读 · 0 评论 -
237. Delete Node in a Linked List
#237. Delete Node in a Linked ListWrite a function to delete a node (except the tail) in a singly linked list, given only access to that node.var deleteNode = function(node) { node.val = node.ne...原创 2019-10-09 23:30:49 · 57 阅读 · 0 评论 -
896. Monotonic Array
#896. Monotonic ArrayAn array is monotonic if it is either monotone increasing or monotone decreasing.An array A is monotone increasing if for all i <= j, A[i] <= A[j]. An array A is monotone...原创 2019-10-10 00:31:28 · 55 阅读 · 0 评论 -
206. Reverse Linked List
#206. Reverse Linked ListReverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULL/** * Definition for singly-linked list. * fun...原创 2019-10-10 00:43:08 · 46 阅读 · 0 评论 -
1137. N-th Tribonacci Number
#1137. N-th Tribonacci NumberThe Tribonacci sequence Tn is defined as follows:T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.Given n, return the value of Tn./** * @param {numbe...原创 2019-10-10 00:47:36 · 76 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree
#104. Maximum Depth of Binary TreeGiven a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node./**...原创 2019-10-10 03:28:49 · 59 阅读 · 0 评论 -
206. Reverse Linked List
#206. Reverse Linked ListReverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULL这道题是把一个链表反转。/** * Definition for singly-linked...原创 2019-10-10 09:05:15 · 58 阅读 · 0 评论 -
1002. Find Common Characters
#1002. Find Common CharactersGiven an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates). For exa...原创 2019-10-01 05:22:24 · 55 阅读 · 0 评论 -
557. Reverse Words in a String III
#557. Reverse Words in a String IIIGiven a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.Example 1:Inp...原创 2019-10-01 05:22:15 · 51 阅读 · 0 评论