leetcode
文章平均质量分 89
Vendredimatin
这个作者很懒,什么都没留下…
展开
-
洛谷搜索题单(BFS/DFS)13道题总结
洛谷搜索题单(BFS/DFS)13道题总结P1219 八皇后经典深搜问题,本题的难点在于如何判断斜线上是否已有棋子,可使用数学中斜率和截距来判断是否是在同一条直线上。P2392 kkksc03考前临时抱佛脚深搜问题,但本题又是01背包问题,把作业分别放进左脑和右脑,最后去左脑和右脑的最大值就是该课的最终结果,一开始想得太复杂了,想把左脑和右脑一起处理。因此应该将搜索简单化,编写代码时尽量简化状态背包容量相当于t/2,最好的结果当然是t/2,意味着左脑和右脑之间没有时间差,即求时间差尽量小,单脑时间原创 2021-07-25 23:43:07 · 1544 阅读 · 0 评论 -
Leetcode 69. Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x, where x is guaranteed to be a non-negative integer.Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.Example 1:In原创 2020-06-29 12:03:57 · 90 阅读 · 0 评论 -
Leetcode 300.longest-increasing-subsequence
method 1 dp使用动态规划,dp[i]代表以第i个数结尾时的最大长度//dp[i]表示以num[i]结尾的序列的最大长度int lengthOfLIS(vector<int>& nums) { if (nums.size() == 0) return 0; vector<int> dp(nums.size(), 1); for (int i = 1; i < nums.size(); i++) { for (int j = 0; j <原创 2020-06-29 11:47:41 · 103 阅读 · 0 评论 -
Leetcode 240. Search a 2D Matrix II
Write 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 in ascending from left to right.Integers in each column are sorted in ascending from top to bottom.Exampl原创 2020-06-29 11:34:50 · 78 阅读 · 0 评论 -
Leetcode 454. 4Sum II
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range o原创 2020-06-29 11:01:58 · 59 阅读 · 0 评论 -
Leetcode 378. Kth Smallest Element in a Sorted Matrix
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.Note that it is the kth smallest element in the sorted order, not the kth distinct element.Example:matrix = [[ 1, 5, 9]原创 2020-06-29 10:46:52 · 105 阅读 · 0 评论 -
Leetcode 212. Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board.Each word must be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizo...原创 2020-02-15 12:37:41 · 94 阅读 · 0 评论 -
Leetcode 208. Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods.Example:Trie trie = new Trie();trie.insert(“apple”);trie.search(“apple”); // returns truetrie.search(“app”); // returns falsetr...原创 2020-02-15 12:32:21 · 94 阅读 · 0 评论 -
Leetcode backtracking Top interview Question 7道题总结
本篇文章是对leetcode tree和Top Interview Questions标签下7道backtracking类型题目的总结Leetcode 46. Permuation求一个数组的排列,不断加入新的元素递归,递归结束后删去,并在新一轮循环中同一个位置加入新的元素Leetcode 22. Generate Parentheses求(和)的排列,思路同上,只是要满足左括号和右括号的...原创 2020-02-14 11:38:02 · 204 阅读 · 0 评论 -
Leetcode 140. Word Break II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible ...原创 2020-02-14 11:17:40 · 86 阅读 · 0 评论 -
Leetcode 131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.Example:Input: “aab”Output:[[“aa”,“b”],[“a”,“a”,“b”]]...原创 2020-02-14 11:06:02 · 74 阅读 · 0 评论 -
Leetcode 17. Letter Combinations of a Phone Number
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is give...原创 2020-02-14 10:56:07 · 82 阅读 · 0 评论 -
Leetcode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[“((()))”,“(()())”,“(())()”,“()(())”,“()()()”...原创 2020-02-14 10:36:16 · 60 阅读 · 0 评论 -
Leetcode 46/47/31/60 Permutations 专题
Leetcode 46 PermutationsGiven a collection of distinct integers, return all possible permutations.Example:Input: [1,2,3]Output:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]求给定数组的所有排列,...原创 2020-02-14 10:30:58 · 114 阅读 · 0 评论 -
Leetcode 279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n.Example 1:Input: n = 12Output: 3Explanation: 12 = 4 + 4 + 4.Example 2:Inp...原创 2020-02-09 20:16:50 · 115 阅读 · 0 评论 -
Leetcode 127. Word Ladder
Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence from beginWord to endWord, such that:Only one letter can be changed at a tim...原创 2020-02-09 20:16:21 · 123 阅读 · 0 评论 -
Leetcode 130. Surrounded Regions
Given a 2D board containing ‘X’ and ‘O’ (the letter O), capture all regions surrounded by ‘X’.A region is captured by flipping all 'O’s into 'X’s in that surrounded region.Explanation:Surrounded r...原创 2020-02-09 18:58:22 · 175 阅读 · 0 评论 -
Leetcode 207/210. Course Schedule I/II
There are a total of n courses you have to take, labeled from 0 to n-1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair:...原创 2020-02-09 18:36:09 · 144 阅读 · 0 评论 -
Leetcode 329. Longest Increasing Path in a Matrix
Given an integer matrix, find the length of the longest increasing path.From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of...原创 2020-02-09 18:18:14 · 103 阅读 · 0 评论 -
Leetcode 200. Number of Islands
Given a 2d grid map of '1’s (land) and '0’s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume...原创 2020-02-09 17:47:52 · 231 阅读 · 0 评论 -
Leetcode tree Top Interview Questions 12道题总结
本篇文章是对leetcode tree和Top Interview Questions标签下12道array类型题目的总结Leetcode 104. Maximum Depth of Binary Tree此题需要求一颗二叉树的最大深度,可归结于树的遍历问题解:使用DFS,添加一个depth参数,每递归一层,depth++,并且每次求最大值Leetcode 230. Kth Smalles...原创 2020-02-03 21:30:52 · 114 阅读 · 0 评论 -
Leetcode 98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node’s key.The rig...原创 2020-02-03 20:51:03 · 97 阅读 · 0 评论 -
116/117. Populating Next Right Pointers in Each Node I/II
先看116Given a binary treestruct Node {int val;Node *left;Node *right;Node *next;}Populate each next pointer to point to its next right node. If there is no next right node, the next pointer sho...原创 2020-02-02 20:27:10 · 165 阅读 · 0 评论 -
Leetcode 124. Binary Tree Maximum Path Sum
Given a non-empty binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connection...原创 2020-02-02 20:03:34 · 88 阅读 · 0 评论 -
Leetcode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric:method 1这道题一开始思路就往复杂的方向思考,导致写的几个版本都有问题。看了答案...原创 2020-02-02 19:49:25 · 83 阅读 · 0 评论 -
Leetcode 108. Convert Sorted Array to Binary Search Tree
Given 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 defined as a binary tree in which the depth of the ...原创 2020-02-02 19:44:44 · 77 阅读 · 0 评论 -
Leetcode 230. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note:You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.method 1树的问题一般即树的遍历问题,考虑前序、中序、后...原创 2020-02-02 19:39:10 · 74 阅读 · 0 评论 -
Leetcode 94. Binary Tree Inorder Traversal +
本篇主要记录两个算法代码中序非递归算法class Solution {public: vector<int> inorderTraversal(TreeNode* root) { vector<int> ans; stack<TreeNode*> s; s.push(root); while (!s....原创 2020-02-02 19:31:10 · 73 阅读 · 0 评论 -
Leetcode 104. Maximum Depth of Binary Tree
Given 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.Note: A leaf is a node with no children....原创 2020-01-31 18:30:32 · 60 阅读 · 0 评论 -
Leetcode 3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Example 1:Input: “abcabcbb”Output: 3Explanation: The answer is “abc”, with the length of 3.Example 2:Input: ...原创 2020-01-30 18:32:12 · 173 阅读 · 0 评论 -
Leetcode 76. Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).Example:Input: S = “ADOBECODEBANC”, T = “ABC”Output: “BANC”(Note: T中可能会有...原创 2020-01-29 20:36:54 · 83 阅读 · 0 评论 -
leetcode 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.Example 1:Input: nums1 = [1,2,2,1], nums2 = [2,2]Output: [2,2]Example 2:Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]Output: [4,9]...原创 2020-01-17 18:38:33 · 94 阅读 · 0 评论 -
leetcode 344. Reverse String
Write a function that reverses a string. The input string is given as an array of characters char[].Do not allocate extra space for another array, you must do this by modifying the input array in-pla...原创 2020-01-15 18:01:35 · 80 阅读 · 0 评论 -
leetcode 15. 3Sum
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:The solution set must not contai...原创 2019-06-22 15:39:02 · 143 阅读 · 0 评论 -
leetcode 62. Unique Paths
A 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 trying to reach the bot...原创 2019-06-09 17:13:27 · 112 阅读 · 0 评论 -
leetcode weekly test 146
5130. Number of Equivalent Domino PairsGiven a list of dominoes, dominoes[i] = [a, b] is equivalent to dominoes[j] = [c, d] if and only if either (ac and bd), or (ad and bc) - that is, one domino can...原创 2019-07-21 14:43:23 · 181 阅读 · 0 评论 -
leetcode 238. Product of Array Except Self
Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Example:Input: [1,2,3,4]Output: [24...原创 2019-06-04 20:24:56 · 115 阅读 · 0 评论 -
Leetcode 289. Game of Life
According to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.”Given a board with m by n ...原创 2019-06-10 14:57:27 · 87 阅读 · 0 评论 -
leetcode 162. Find Peak Element
A peak element is an element that is greater than its neighbors.Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index.The array may contain multiple peaks, i...原创 2019-06-15 17:07:50 · 97 阅读 · 0 评论 -
leetcode 73. Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.Example 1:Input:[[1,1,1],[1,0,1],[1,1,1]]Output:[[1,0,1],[0,0,0],[1,0,1]]Example 2:Input:[...原创 2019-06-15 16:26:20 · 85 阅读 · 0 评论