Leetcode
记录刷leetcode的的点点滴滴。想清楚道理,提高算法水平,欢迎大家批评指正。
bwcxljsm
这个作者很懒,什么都没留下…
展开
-
84. Largest Rectangle in Histogram
84. Largest Rectangle in HistogramGiven n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,原创 2020-10-03 10:33:35 · 181 阅读 · 0 评论 -
84. Largest Rectangle in Histogram
84. Largest Rectangle in HistogramGiven n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Example:Input: [2,1,5,6,2,3]Output: 10题目大意给出一个直方图,求可以组成原创 2020-10-02 14:50:35 · 172 阅读 · 0 评论 -
402. Remove K Digits
402. Remove K DigitsGiven a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.Note:The length of num is less than 10002 and will be ≥ k.The given num does not contain any原创 2020-09-28 22:49:26 · 186 阅读 · 0 评论 -
880 Decoded String at Index
880 Decoded String at IndexAn encoded string S is given. To find and write the decoded string to a tape, the encoded string is read one character at a time and the following steps are taken:If the character read is a letter, that letter is written onto原创 2020-09-26 20:54:38 · 153 阅读 · 0 评论 -
456. 132 Pattern
456. 132 PatternGiven an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j].Return true if there is a 132 pattern in nums, otherwise retu原创 2020-09-25 09:56:44 · 152 阅读 · 0 评论 -
173 Binary Search Tree Iterator
173 Binary Search Tree IteratorImplement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Example:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上原创 2020-09-12 11:12:28 · 108 阅读 · 0 评论 -
897——Increasing Order SearchTree
题目Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child.大意给出一个树,重新排列,使得最左的节点是树的根...原创 2018-12-19 20:40:59 · 194 阅读 · 0 评论 -
1.twosum
第一次刷leedcode,恶补一下STL,慢慢学吧。这天可真冷,嘤嘤嘤。虽然知道思路,但由于对STL几乎不了解,还是做了半天,废话不多说,看题吧。 题意:给出一个vector和一个目标的数,从vector里选出两个数,使它们的和为target,找出这两个数的位置。保证只有一个解。思路:将vector转化成map,map的键值为它的位置,即i。然后在map里面寻找target-(*m).原创 2018-01-29 22:45:41 · 133 阅读 · 0 评论 -
766. Toeplitz Matrix
基础题,居然也卡了一会。题意:给出一个二维数组,然后判断二维数组左边和上边的数从左上到右下的数是否相等,如果满足就返回true,不满足返回false。点击打开链接用一个二维的vector,首先判断竖的第一列,从上往下,依次判断。从左上到右下,数组时matrix[i][0]到matrix[i+j][j],注意i+j和j的范围,因此i+jclass Solution {publi原创 2018-01-30 16:18:21 · 131 阅读 · 0 评论 -
7. Reverse Integer
虽然看起来很简单,但是还是有一点得注意。逆转之后可能比int类型大,会溢出,此时应该输出0。题目大意:Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321原创 2018-01-31 22:04:22 · 97 阅读 · 0 评论 -
775. Global and Local Inversions
We have some permutation A of [0, 1, ..., N - 1], where N is the length of A.The number of (global) inversions is the number of i with 0 and A[i] > A[j].The number of local inversions is原创 2018-02-01 19:02:19 · 178 阅读 · 0 评论 -
769. Max Chunks To Make Sorted
Given an array arr that is a permutation of [0, 1, ..., arr.length - 1], we split the array into some number of "chunks" (partitions), and individually sort each chunk. After concatenating them, th原创 2018-02-02 16:29:30 · 326 阅读 · 0 评论 -
768. Max Chunks To Make Sorted II
惭愧,知道思路居然搞了那么久,中间就是一个数组名字搞错了,搞了半天不对劲。This question is the same as "Max Chunks to Make Sorted" except the integers of the given array are not necessarily distinct, the input array could be up to原创 2018-02-03 16:10:42 · 207 阅读 · 0 评论 -
747. Largest Number At Least Twice of Others
In a given integer array nums, there is always exactly one largest element.Find whether the largest element in the array is at least twice as much as every other number in the array.If it is,原创 2018-02-04 14:26:52 · 161 阅读 · 0 评论 -
746. Min Cost Climbing Stairs
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top原创 2018-02-05 11:25:52 · 240 阅读 · 0 评论 -
724. Find Pivot Index
Given an array of integers nums, write a method that returns the "pivot" index of this array.We define the pivot index as the index where the sum of the numbers to the left of the index is equal t原创 2018-02-06 13:55:26 · 90 阅读 · 0 评论 -
717. 1-bit and 2-bit Characters
We have two special characters. The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11).Now given a string represented by several bits.原创 2018-02-07 21:02:38 · 83 阅读 · 0 评论 -
718. Maximum Length of Repeated Subarray
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays.Example 1:Input:A: [1,2,3,2,1]B: [3,2,1,4,7]Output: 3Explanation: The repeated subarray with...原创 2018-02-08 20:02:59 · 119 阅读 · 0 评论 -
9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the intege...原创 2018-02-09 19:13:46 · 92 阅读 · 0 评论 -
719. Find K-th Smallest Pair Distance
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.Example 1:Input:nums = [1,3,1]k = 1...原创 2018-02-10 19:48:50 · 158 阅读 · 0 评论 -
674. Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).Example 1:Input: [1,3,5,4,7]Output: 3Explanation: The longest continuous increasing subse...原创 2018-02-11 20:00:13 · 121 阅读 · 0 评论 -
714. Best Time to Buy and Sell Stock with Transaction Fee
Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer feerepresenting a transaction fee.You may complete as many tra...原创 2018-02-12 15:53:01 · 115 阅读 · 0 评论 -
561. Array Partition I
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as poss...原创 2018-02-13 21:10:09 · 106 阅读 · 0 评论 -
670. Maximum Swap
Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get.Example 1:Input: 2736Output: 7236Explanation: Sw...原创 2018-02-14 16:14:31 · 109 阅读 · 0 评论 -
665. Non-decreasing Array
Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.We define an array is non-decreasing if array[i] <= array[i + 1] holds for eve...原创 2018-02-15 14:14:50 · 119 阅读 · 0 评论 -
643. Maximum Average Subarray I
Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value.Example 1:Input: [1,12,-5,-...原创 2018-02-16 13:30:23 · 182 阅读 · 0 评论 -
628. Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product.Example 1:Input: [1,2,3]Output: 6Example 2:Input: [1,2,3,4]Output: 24Note:The length of the given...原创 2018-02-17 09:31:28 · 137 阅读 · 0 评论 -
611. Valid Triangle Number
【LeetCode】611.Valid Triangle Number解题报告tags: Array题目地址:https://leetcode.com/problems/valid-triangle-number/description/ 题目描述: Given an array consists of non-negative integers, your task is to count t...转载 2018-02-18 21:47:24 · 92 阅读 · 0 评论 -
605. Can Place Flowers
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die.Given ...原创 2018-02-19 12:19:52 · 111 阅读 · 0 评论 -
581. Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.You need to find th...原创 2018-02-20 11:19:32 · 109 阅读 · 0 评论 -
566. Reshape the Matrix
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.You're given a matrix represented by a two-dimensi...原创 2018-02-21 11:27:40 · 107 阅读 · 0 评论 -
661. Image Smoother
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surro...原创 2018-02-23 14:17:04 · 89 阅读 · 0 评论 -
565. Array Nesting
A zero-indexed array A of length N contains all integers from 0 to N-1. Find and return the longest length of set S, where S[i] = {A[i], A[A[i]], A[A[A[i]]], ... } subjected to the rule below.Suppose ...原创 2018-02-23 21:47:26 · 78 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Could yo...原创 2018-02-24 11:59:25 · 100 阅读 · 0 评论 -
414. Third Maximum Number
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).Example 1:Input: [3, 2, 1]Out...原创 2018-02-27 11:51:52 · 97 阅读 · 0 评论 -
495. Teemo Attacking
In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo's attacking ascending time series towards Ashe and the poisoning tim...原创 2018-02-27 12:28:19 · 87 阅读 · 0 评论 -
238. Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O(n).F...原创 2018-02-28 11:26:48 · 89 阅读 · 0 评论 -
229. Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.题意:给出一个数组,找出出现次数大于n/3的所有数,要求空间复杂度为O(1)。思路:一、用map,key为...原创 2018-03-01 11:07:40 · 97 阅读 · 0 评论 -
118. Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]题意:打印相应行数的杨辉三角。思路:找规律,动态规划即可。代码:class...原创 2018-03-02 13:46:39 · 77 阅读 · 0 评论 -
119. Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?题意:打印杨辉三角的第k行,要求空间复杂度为O(k)。思...原创 2018-03-04 21:26:45 · 85 阅读 · 0 评论