
leetcode
wineandchord
这个作者很懒,什么都没留下…
展开
-
leetcode 121. Best Time to Buy and Sell Stock
文章目录C++JavaPythonGoSay you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maxi原创 2020-08-23 12:23:56 · 154 阅读 · 0 评论 -
leetcode 160. Intersection of Two Linked Lists
文章目录C++JavaPythonGoWrite a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:begin to intersect at node c1.Example 1:Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [原创 2020-08-23 10:51:53 · 172 阅读 · 0 评论 -
leetcode 75. Sort Colors (快排/双指针)
文章目录QuickSortC++JavaPythonGoTwo passC++One pass with two pointersC++JavaPythonGoGiven an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.原创 2020-08-23 10:00:35 · 160 阅读 · 0 评论 -
leetcode 74. Search a 2D Matrix (二分)
文章目录C++JavaPythonGoWrite 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 from left to right.The first integer of each row is greater than the last integer of t原创 2020-08-23 09:22:18 · 102 阅读 · 0 评论 -
leetcode 73. Set Matrix Zeroes
文章目录C++JavaPythonGoGiven an m x n matrix. If an element is 0, set its entire row and column to 0. Do it in-place.Follow up:A straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O(m + n) space, but still not the原创 2020-08-22 23:03:08 · 99 阅读 · 0 评论 -
leetcode 72. Edit Distance(动态规划)
文章目录C++JavaPythonGoGiven two words word1 and word2, find the minimum number of operations required to convert word1 to word2.You have the following 3 operations permitted on a word:Insert a characterDelete a characterReplace a characterExample 1:Inp原创 2020-08-20 13:14:57 · 140 阅读 · 0 评论 -
leetcode 71. Simplify Path(栈)
文章目录C++JavaPythonGoGiven an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path.In a UNIX-style file system, a period . refers to the current directory. Furthermore, a double period … moves the director原创 2020-08-20 10:50:52 · 315 阅读 · 0 评论 -
leetcode 70. Climbing Stairs (DP)
文章目录C++JavaPythonGoYou are climbing a stair case. It takes n steps to reach to 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: 2Output: 2Explanation: There are two ways to climb原创 2020-08-20 10:29:59 · 164 阅读 · 0 评论 -
leetcode 68. Text Justification
文章目录C++JavaPythonGoGiven an array of words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.You should pack your words in a greedy approach; that is, pack as many words as y原创 2020-08-20 09:46:56 · 196 阅读 · 1 评论 -
leetcode 67. Add Binary (二进制加法)
文章目录C++JavaPythonGoGiven 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: “100”Example 2:Input: a = “1010”, b = “1011”Outp原创 2020-08-20 09:12:32 · 153 阅读 · 0 评论 -
leetcode 66. Plus One (大整数加一)
Given a non-empty array of digits representing a non-negative integer, increment one to the integer.The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.You may assum原创 2020-08-19 09:24:11 · 141 阅读 · 0 评论 -
leetcode 65. Valid Number (合法的浮点数)
文章目录DFA (Deterministic Finite Automata)JavaPythonValidate if a given string can be interpreted as a decimal number.Some examples:“0” => true" 0.1 " => true“abc” => false“1 a” => false“2e10” => true" -90e3 " => true" 1e" => f原创 2020-08-18 22:50:30 · 386 阅读 · 0 评论 -
leetcode 215. Kth Largest Element in an Array (数组中的第 k 个最大元素)
文章目录直接排序 O(nlogn)+O(1)O(n\log n)+O(1)O(nlogn)+O(1)JavaC++PythonGo最大堆/最小堆 O(nlogk)+O(k)O(n\log k)+O(k)O(nlogk)+O(k)JavaC++PythonGoPartitation O(n)∼O(n2)+O(1)O(n)\sim O(n^2)+O(1)O(n)∼O(n2)+O(1)JavaC++PythonGoQuickSelect O(n)∼O(n2)+O(1)O(n)\sim O(n^2)+O(1)O原创 2020-08-13 16:53:06 · 147 阅读 · 0 评论 -
leetcode 64. Minimum Path Sum (简单 DP)
文章目录空间 O(n2)O(n^2)O(n2)C++JavaPythonGo空间 O(n)O(n)O(n)C++JavaPythonGoGiven 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 either down o原创 2020-08-13 09:27:45 · 122 阅读 · 0 评论 -
leetcode 63. Unique Paths II (简单DP)
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 bottom-right corner of the grid (marked ‘Finish’ in the dia原创 2020-08-12 10:43:39 · 108 阅读 · 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 bottom-right corner of the grid (marked ‘Finish’ in the dia原创 2020-08-12 10:23:07 · 89 阅读 · 0 评论 -
leetcode 61. Rotate List (链表循环移动 k 位)
Given a linked list, rotate the list to the right by k places, where k is non-negative.Example 1:Input: 1->2->3->4->5->NULL, k = 2Output: 4->5->1->2->3->NULLExplanation:rotate 1 steps to the right: 5->1->2->3->原创 2020-08-12 10:11:53 · 164 阅读 · 0 评论 -
leetcode 60. Permutation Sequence (n 位数的第 k 个排列)
The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order, we get the following sequence for n = 3:“123”“132”“213”“231”“312”“321”Given n and k, return the kth permutation sequence.Note:原创 2020-08-12 10:02:34 · 136 阅读 · 0 评论 -
leetcode 59. Spiral Matrix II (模拟)
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.Example:Input: 3Output:[[ 1, 2, 3 ],[ 8, 9, 4 ],[ 7, 6, 5 ]]模拟就行了class Solution {public: vector<vector<int>> generateMatrix(原创 2020-08-11 20:16:16 · 80 阅读 · 0 评论 -
leetcode 146. LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.put(key,原创 2020-08-11 00:35:10 · 80 阅读 · 0 评论 -
leetcode 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ’ ', return the length of last word (last word means the last appearing word if we loop from left to right) in the string.If the last word does not exist, return 0.Note: A原创 2020-08-10 22:19:13 · 82 阅读 · 0 评论 -
leetcode 124. Binary Tree Maximum Path Sum (DFS)
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 connections. The path must contain at least one node and does not原创 2020-08-07 09:20:36 · 105 阅读 · 0 评论 -
leetcode 57. Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Example 1:Input: intervals = [[1,3],[6,9]], newInterval = [2,5原创 2020-08-07 07:57:57 · 91 阅读 · 0 评论 -
leetcode 206. Reverse Linked List 翻转链表
Reverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLFollow up:A linked list can be reversed either iteratively or recursively. Could you implement both?迭代:/** * Definition for si原创 2020-08-06 11:42:19 · 99 阅读 · 0 评论 -
leetcode 69. Sqrt(x) 整数平方根(二分/牛顿迭代法)
Easy13691930Add to ListShareImplement 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原创 2020-08-05 13:32:32 · 218 阅读 · 0 评论 -
leetcode 56. Merge Intervals
Given a collection of intervals, merge all overlapping intervals.Example 1:Input: [[1,3],[2,6],[8,10],[15,18]]Output: [[1,6],[8,10],[15,18]]Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].Example 2:Input: [[1,4],[4,5]]Ou原创 2020-08-05 10:32:05 · 87 阅读 · 0 评论 -
leetcode 543. Diameter of Binary Tree (DFS)
Given 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 a tree. This path may or may not pass through the root.Example:Given a binary tree1原创 2020-08-04 09:01:11 · 104 阅读 · 0 评论 -
leetcode 55. Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index.Example 1:Input: nums原创 2020-08-01 18:27:40 · 91 阅读 · 0 评论 -
leetcode 54. Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.Example 1:Input:[[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ]]Output: [1,2,3,6,9,8,7,4,5]Example 2:Input:[[1, 2, 3, 4],[5, 6, 7, 8],[9,10,11,12]]原创 2020-08-01 18:12:06 · 85 阅读 · 0 评论 -
leetcode 53. Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Example:Input: [-2,1,-3,4,-1,2,1,-5,4],Output: 6Explanation: [4,-1,2,1] has the largest sum = 6.Follow up:If you h原创 2020-08-01 17:30:46 · 88 阅读 · 0 评论 -
leetcode 52. N-Queens II
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return the number of distinct solutions to the n-queens puzzle.Example:Input: 4Output: 2Explanation: There are tw原创 2020-07-26 11:14:01 · 69 阅读 · 0 评论 -
leetcode 51. N-Queens 搜索/回溯
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.Each solution contains a distinct board configuration of the n-原创 2020-07-26 11:07:23 · 82 阅读 · 0 评论 -
leetcode 50. Pow(x, n) 快速幂
Implement pow(x, n), which calculates x raised to the power n (xn).Example 1:Input: 2.00000, 10Output: 1024.00000Example 2:Input: 2.10000, 3Output: 9.26100Example 3:Input: 2.00000, -2Output: 0.25000Explanation: 2-2 = 1/22 = 1/4 = 0.25Note:-100.原创 2020-07-26 10:43:43 · 79 阅读 · 0 评论 -
leetcode 49. Group Anagrams 哈希
Medium3641187Add to ListShareGiven an array of strings, group anagrams together.Example:Input: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”],Output:[[“ate”,“eat”,“tea”],[“nat”,“tan”],[“bat”]]Note:All inputs will be in lowercase.The order of yo原创 2020-07-26 10:38:44 · 108 阅读 · 0 评论 -
leetcode 48. Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotat原创 2020-07-26 10:21:22 · 78 阅读 · 0 评论 -
leetcode 47. Permutations II 搜索
Given a collection of numbers that might contain duplicates, return all possible unique permutations.Example:Input: [1,1,2]Output:[[1,1,2],[1,2,1],[2,1,1]]class Solution {public: vector<vector<int>> permuteUnique(vector<int>原创 2020-07-26 10:15:01 · 71 阅读 · 0 评论 -
leetcode 46. Permutations 搜索
Given 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]]DFSclass Solution {public: vector<vector<int>> permute(vector<int>&a原创 2020-07-26 10:05:04 · 64 阅读 · 1 评论 -
leetcode 45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to reach the last index in the minimum number of jumps.Exam原创 2020-07-26 09:53:25 · 87 阅读 · 0 评论 -
leetcode 44. Wildcard Matching 贪心
Given an input string (s) and a pattern §, implement wildcard pattern matching with support for ‘?’ and ‘*’.‘?’ Matches any single character.‘*’ Matches any sequence of characters (including the empty sequence).The matching should cover the entire input原创 2020-07-26 09:30:47 · 106 阅读 · 0 评论 -
leetcode 43. Multiply Strings 大数乘法
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.Example 1:Input: num1 = “2”, num2 = “3”Output: “6”Example 2:Input: num1 = “123”, num2 = “456”Output: “56088”Note:原创 2020-07-26 09:13:47 · 87 阅读 · 0 评论