LeetCode
个人LeetCode刷题笔记
乐观的大鹏
一个被java吊打了n次的渣渣鹏
展开
-
LeetCode: 4. Median of Two Sorted Arrays
一、最容易想到的办法是把两个数组合并,然后取出中位数。但是合并有序数组的操作是 O(m+n) 的,不符合题意。看到题目给的 log 的时间复杂度,很容易联想到二分搜索。请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。你可以假设 nums1 和 nums2 不会同时为空。二、去中位进行两个数组移动。原创 2023-09-18 21:29:19 · 386 阅读 · 0 评论 -
LeetCode-1. Two Sum
顺序扫描数组,对每一个元素,在map中找能组合给定值的另一个数字,如果找到了,直接返回 2 个数字的下标即可。如果找不到,就把这个数字存入 map 中,等待扫到另一个数字的时候,再取出来返回结果。在数组中找到 2 个数之和等于给定值的数字,结果返回 2 个数字在数组中的下标。原创 2023-09-14 22:03:17 · 338 阅读 · 0 评论 -
leetcode-131 Palindrome Partitioning
Given a strings, partitionssuch that every substring of the partition is a palindrome.Return all possible palindrome partitioning ofs.Example:Input:"aab"Output:[ ["aa","b"], ["a","a"...原创 2019-06-13 10:59:37 · 129 阅读 · 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.Example:X X X XX O O...原创 2019-06-12 19:26:00 · 122 阅读 · 0 评论 -
leetcode-129 Sum Root to Leaf Numbers
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which represents the number123.Find the tota...原创 2019-06-10 12:04:23 · 99 阅读 · 0 评论 -
leetcode 128 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Your algorithm should run in O(n) complexity.Example:Input:[100, 4, 200, 1, 3, 2]Output: 4Ex...原创 2019-06-06 15:25:49 · 105 阅读 · 0 评论 -
leetcode-125 Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.Note:For the purpose of this problem, we define empty string as valid palindrome.Examp...原创 2019-06-05 17:12:56 · 122 阅读 · 2 评论 -
leetcode-124 Binary Tree Maximum Path Sum
Given anon-emptybinary 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 connectio...原创 2019-06-05 16:47:17 · 151 阅读 · 0 评论 -
leetcode-123 Best Time to Buy and Sell Stock III
Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete at mosttwotransactions.Note:You may not e...原创 2019-06-05 09:01:56 · 146 阅读 · 0 评论 -
leetcode-122 Best Time to Buy and Sell Stock II
Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy on...原创 2019-06-03 20:04:55 · 77 阅读 · 0 评论 -
leetcode-121 Best Time to Buy and Sell Stock
Say you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock),...原创 2019-06-03 16:46:26 · 87 阅读 · 0 评论 -
leetcode-120 Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [6,5...原创 2019-05-31 17:17:07 · 92 阅读 · 0 评论 -
leetcode-119 Pascal's Triangle II
Given a non-negativeindexkwherek≤33, return thekthindex row of the Pascal's triangle.Note that the row index starts from0.In Pascal's triangle, each number is the sum of the two numbe...原创 2019-05-31 11:33:42 · 105 阅读 · 0 评论 -
leetcode-118 Pascal's Triangle
Given a non-negative integernumRows, generate the firstnumRowsof Pascal's triangle.In Pascal's triangle, each number is the sum of the two numbers directly above it.Example:Input: 5Output:...原创 2019-05-31 11:17:02 · 97 阅读 · 0 评论 -
leetcode-117 Populating Next Right Pointers in Each Node II
Given 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 point...原创 2019-05-31 08:58:45 · 148 阅读 · 0 评论 -
leetcode-116 Populating Next Right Pointers in Each Node
You are given aperfect binary treewhereall leaves are on the same level, and every parent has two children. The binary tree has the following definition:struct Node { int val; Node *left; ...原创 2019-05-30 21:32:20 · 109 阅读 · 0 评论 -
leetcode-115 Distinct Subsequences
Given a stringSand a stringT, count the number of distinct subsequences ofSwhich equalsT.A subsequence of a string is a new string which is formed from the original string by deleting some (ca...原创 2019-05-30 20:32:39 · 120 阅读 · 0 评论 -
leetcode-114 Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example, given the following tree: 1 / \ 2 5 / \ \3 4 6The flattened tree should look like:1 \ 2 \ ...原创 2019-05-30 09:06:18 · 92 阅读 · 0 评论 -
leetcode-113 Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.Note:A leaf is a node with no children.Example:Given the below binary tree andsum = 22,...原创 2019-05-29 19:25:51 · 79 阅读 · 0 评论 -
leetcode-112 Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.Note:A leaf is a node with no children.Example:...原创 2019-05-28 20:29:59 · 94 阅读 · 0 评论 -
leetcode-111 Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.Note:A leaf is a node with no childre...原创 2019-05-28 20:08:15 · 108 阅读 · 0 评论 -
leetcode-110 Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as:a binary tree in which the depth of the two subtrees ofeverynode never diff...原创 2019-05-28 19:50:39 · 159 阅读 · 0 评论 -
leetcode-109 Convert Sorted List to Binary Search Tree
Given a singly linked list 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 ...原创 2019-05-28 17:32:31 · 108 阅读 · 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...原创 2019-05-28 16:38:32 · 83 阅读 · 0 评论 -
leetcode-107 Binary Tree Level Order Traversal II
Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree[3,9,20,null,null,15,7...原创 2019-05-27 15:31:35 · 88 阅读 · 0 评论 -
leetcode-106 Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.For example, giveninorder =[9,3,15,20,7]postorder = [9...原创 2019-05-27 15:02:13 · 104 阅读 · 0 评论 -
leetcode-105 Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.For example, givenpreorder =[3,9,20,15,7]inorder = [9,3...原创 2019-05-27 12:18:00 · 74 阅读 · 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 childre...原创 2019-05-26 13:04:27 · 98 阅读 · 0 评论 -
leetcode-103 Binary Tree Zigzag Level Order Traversal
Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tre...原创 2019-05-26 12:59:12 · 93 阅读 · 0 评论 -
leetcode-102 Binary Tree Level Order Traversal
Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree[3,9,20,null,null,15,7], 3 / \ 9 20 ...原创 2019-05-24 17:13:59 · 148 阅读 · 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: 1 / \ 2 2 / \ / \3 4 4 3...原创 2019-05-24 13:57:01 · 102 阅读 · 0 评论 -
leetcode-100 Same Tree
Given two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical and the nodes have the same value.Example ...原创 2019-05-24 10:32:22 · 87 阅读 · 0 评论 -
leetcode-99 Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Example 1:Input: [1,3,null,null,2] 1 /3 \ 2Output: [3,1,null,nul...原创 2019-05-24 10:21:50 · 113 阅读 · 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 keysless thanthe node's key. The ...原创 2019-05-23 17:50:32 · 91 阅读 · 0 评论 -
leetcode-97 Interleaving String
Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2.Example 1:Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"Output: trueExample 2:Input: s1 = "aabcc", s2 = "...原创 2019-05-23 16:26:31 · 101 阅读 · 0 评论 -
leetcode-96 Unique Binary Search Trees
Givenn, how many structurally uniqueBST's(binary search trees) that store values 1 ...n?Example:Input: 3Output: 5Explanation:Given n = 3, there are a total of 5 unique BST's: 1 ...原创 2019-05-23 14:56:51 · 76 阅读 · 0 评论 -
leetcode-95 Unique Binary Search Trees II
Given an integern, generate all structurally uniqueBST's(binary search trees) that store values 1 ...n.Example:Input: 3Output:[ [1,null,3,2], [3,2,null,1], [3,1,null,null,2], [2,1,3...原创 2019-05-22 20:39:56 · 131 阅读 · 0 评论 -
leetcode-94 Binary Tree Inorder Traversal
Given a binary tree, return theinordertraversal of its nodes' values.Example:Input: [1,null,2,3] 1 \ 2 / 3Output: [1,3,2]Follow up:Recursive solution is trivial, could y...原创 2019-05-22 16:41:49 · 78 阅读 · 0 评论 -
leetcode-93 Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations.Example:Input: "25525511135"Output: ["255.255.11.135", "255.255.111.35"]class Solutio...原创 2019-05-22 16:22:35 · 93 阅读 · 0 评论 -
leetcode-92 Reverse Linked List II
Reverse a linked list from positionmton. Do it in one-pass.Note:1 ≤m≤n≤ length of list.Example:Input: 1->2->3->4->5->NULL, m = 2, n = 4Output: 1->4->3->2->5-...原创 2019-05-21 20:18:55 · 91 阅读 · 0 评论