LeetCode
LeetCode is the best platform to help you enhance your skills, expand your knowledge and prepare for technical interviews.
正在沿着IT树往上爬
这个作者很懒,什么都没留下…
展开
-
【Best Time to Buy and Sell Stock IV】
You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k.Find the maximum profit you can achieve. You may complete at most k transactions.Note: You may not engage in multiple transactions simult原创 2022-04-26 10:59:10 · 1000 阅读 · 0 评论 -
【Best Time to Buy and Sell Stock III】
You are given an array prices where prices[i] is the price of a given stock on the ith day.Find the maximum profit you can achieve. You may complete at most two transactions.Note: You may not engage in multiple transactions simultaneously (i.e., you must原创 2022-04-26 10:52:27 · 618 阅读 · 0 评论 -
【Best Time to Buy and Sell Stock II】
You are given an integer array prices where prices[i] is the price of a given stock on the ith day.On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immedia原创 2022-04-24 15:45:26 · 596 阅读 · 0 评论 -
【Best Time to Buy and Sell Stock】
You are given an array prices where prices[i] is the price of a given stock on the ith day.You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.Return the maximum profit原创 2022-04-24 14:30:04 · 479 阅读 · 0 评论 -
【Pascal‘s Triangle】
Given an integer numRows, return the first numRows of Pascal’s triangle.In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown:Example 1:Input: numRows = 5Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]Example 2原创 2022-04-19 17:31:52 · 691 阅读 · 0 评论 -
【Path Sum】
Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.A leaf is a node with no children.Example 1:Input: root = [5,4,8,11,null,13,4,7原创 2022-04-19 16:58:48 · 262 阅读 · 0 评论 -
【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 children.Example 1:Input: root = [3,9,20,null,null,15,7]Outpu原创 2022-04-18 17:43:39 · 232 阅读 · 0 评论 -
【Freedom Trail】
In the video game Fallout 4, the quest “Road to Freedom” requires players to reach a metal dial called the “Freedom Trail Ring” and use the dial to spell a specific keyword to open the door.Given a string ring that represents the code engraved on the oute转载 2022-04-18 15:36:04 · 105 阅读 · 0 评论 -
【Max Area of Island】
You are given an m x n binary matrix grid. An island is a group of 1’s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.The area of an island is the number of cells w原创 2022-04-12 16:07:02 · 76 阅读 · 0 评论 -
【Count Good Nodes in Binary Tree】
Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X.Return the number of good nodes in the binary tree.Example 1:Input: root = [3,1,4,3,null,1,5]Output: 4Explanatio原创 2022-04-11 17:07:52 · 388 阅读 · 0 评论 -
【Convert Sorted Array to Binary Search Tree】
Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than原创 2022-04-08 18:16:02 · 396 阅读 · 0 评论 -
【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 left and right subtrees of every node differ in height by no more than 1.Example 1:Input: root = [3,9,20原创 2022-04-08 11:00:40 · 87 阅读 · 0 评论 -
【Maximum Depth of Binary Tree】
Given the root of a binary tree, return its maximum depth.A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.Example 1:Input: root = [3,9,20,null,null,15,7]Output: 3Example 2原创 2022-04-06 18:14:18 · 426 阅读 · 0 评论 -
【Symmetric Tree】
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).Example 1:Input: root = [1,2,2,3,4,4,3]Output: trueExample 2:Input: root = [1,2,2,null,3,null,3]Output: falseConstraints:The number of原创 2022-04-02 18:26:53 · 412 阅读 · 0 评论 -
【Same Tree】
Given the roots of two binary trees p and q, 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 1:Input: p = [1,2,3], q = [1,2,3]原创 2022-04-02 16:09:18 · 488 阅读 · 0 评论 -
【一维数组构建二叉树】
class Solution { /** * 一维数组构建二叉树 * * @param objects * @return */ public TreeNode createBinaryTree(Object[] objects) { Queue<TreeNode> queue = new LinkedList<TreeNode>(); TreeNode head; if原创 2022-04-01 17:26:15 · 538 阅读 · 0 评论 -
【Binary Tree Inorder Traversal】
Given the root of a binary tree, return the inorder traversal of its nodes’ values.Example 1:Input: root = [1,null,2,3]Output: [1,3,2]Example 2:Input: root = []Output: []Example 3:Input: root = [1]Output: [1]Constraints:The number of nodes原创 2022-03-31 17:31:26 · 331 阅读 · 0 评论 -
【Merge Sorted Array】
You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.Merge nums1 and nums2 into a single array sorted in non-decreasing order.The f原创 2022-03-29 18:08:48 · 83 阅读 · 0 评论 -
【Remove Duplicates from Sorted List】
Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.Example 1:Input: head = [1,1,2]Output: [1,2]Example 2:Input: head = [1,1,2,3,3]Output: [1,2,3]Constrain原创 2022-03-29 14:58:09 · 194 阅读 · 0 评论 -
【 Climbing Stairs】
You are climbing a staircase. It takes n steps to reach the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?Example 1:Input: n = 2Output: 2Explanation: There are two ways to climb to the top.1. 1 st原创 2022-03-29 11:03:46 · 92 阅读 · 0 评论 -
【Add Two Numbers】-(Java)
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.You may assume the two number原创 2022-03-25 18:15:24 · 465 阅读 · 0 评论 -
【Add Binary】-(Java)
Given two binary strings a and b, return their sum as a binary string.Example 1:Input: a = "11", b = "1"Output: "100"Example 2:Input: a = "1010", b = "1011"Output: "10101"Constraints:1 <= a.length, b.length <= 10^4a and b consist only of原创 2022-01-19 11:15:09 · 107 阅读 · 0 评论 -
【Add Binary】-(Python)
Given two binary strings a and b, return their sum as a binary string.Example 1:Input: a = "11", b = "1"Output: "100"Example 2:Input: a = "1010", b = "1011"Output: "10101"Constraints:1 <= a.length, b.length <= 10^4a and b consist only of原创 2022-01-05 18:16:40 · 124 阅读 · 0 评论 -
【Plus One】-(Python)
You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any lea原创 2021-12-30 11:00:17 · 108 阅读 · 0 评论 -
【Plus One】-(Java)
You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any lea原创 2021-12-30 10:39:19 · 122 阅读 · 0 评论 -
【Length of Last Word】-(Python)
Given a string s consisting of some words separated by some number of spaces, return the length of the last word in the string.A word is a maximal substring consisting of non-space characters only.Example 1:Input: s = "Hello World"Output: 5Explanation原创 2021-12-23 17:57:20 · 114 阅读 · 0 评论 -
【Length of Last Word】-(Java)
Given a string s consisting of some words separated by some number of spaces, return the length of the last word in the string.A word is a maximal substring consisting of non-space characters only.Example 1:Input: s = "Hello World"Output: 5Explanation原创 2021-12-23 17:55:58 · 94 阅读 · 0 评论 -
【Maximum Subarray】-(Python)
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.A subarray is a contiguous part of an array.Example 1:Input: nums = [-2,1,-3,4,-1,2,1,-5,4]Output: 6Explanation: [原创 2021-12-23 17:35:28 · 197 阅读 · 0 评论 -
【Maximum Subarray】-(Java)
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.A subarray is a contiguous part of an array.Example 1:Input: nums = [-2,1,-3,4,-1,2,1,-5,4]Output: 6Explanation: [原创 2021-12-23 17:34:08 · 123 阅读 · 0 评论 -
【Search Insert Position】-(Python)
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You must write an algorithm with O(log n) runtime complexity.Example 1:Input:原创 2021-12-22 18:08:01 · 89 阅读 · 0 评论 -
【Search Insert Position】-(Java)
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You must write an algorithm with O(log n) runtime complexity.Example 1:Input:原创 2021-12-22 18:04:35 · 92 阅读 · 0 评论 -
【Implement strStr】-(Python)
Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Clarification:What should we return when needle is an empty string? This is a great question to ask during an interview.For the p原创 2021-12-20 17:19:48 · 72 阅读 · 0 评论 -
【Implement strStr】-(Java)
Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Clarification:What should we return when needle is an empty string? This is a great question to ask during an interview.For the p原创 2021-12-20 17:10:31 · 79 阅读 · 0 评论 -
【Remove Element】-(Java)
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed.Since it is impossible to change the length of the array in some languages, you must instead have the result原创 2021-12-10 11:48:11 · 97 阅读 · 0 评论 -
【Remove Element】-(Python)
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed.Since it is impossible to change the length of the array in some languages, you must instead have the result原创 2021-12-09 18:38:47 · 106 阅读 · 0 评论 -
【Remove Duplicates from Sorted Array】-(Python)
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.Since it is impossible to change the length of the array原创 2021-12-09 17:49:09 · 63 阅读 · 0 评论 -
【Remove Duplicates from Sorted Array】-(Java)
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.Since it is impossible to change the length of the array原创 2021-12-09 17:40:28 · 62 阅读 · 0 评论 -
【Merge Two Sorted Lists】-(Python)
You are given the heads of two sorted linked lists list1 and list2.Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.Return the head of the merged linked list.Example 1:Input: list1原创 2021-12-09 14:43:25 · 205 阅读 · 0 评论 -
【Merge Two Sorted Lists】-(Java)
You are given the heads of two sorted linked lists list1 and list2.Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.Return the head of the merged linked list.Example 1:Input: list1原创 2021-12-09 14:22:16 · 181 阅读 · 0 评论 -
【Valid Parentheses】-(Python)
Given a string s containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.An input string is valid if:Open brackets must be closed by the same type of brackets.Open brackets must be closed in the correct or原创 2021-12-08 14:24:44 · 105 阅读 · 0 评论