-
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. You may imagine that num[-1] = num[n] = -∞. For example, in array …
Read More -
Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 思路:由于有正,有负,还有0.。。所以比最大子串之和要复杂一些。。。 dp[i].max表示到当前位置的最大乘积。 dp[i].min表示到当前位置的最小乘积。 dp[i].max = …
Read More -
Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. 题意:把连续的数连续表示 思路:模拟。注意有负数,注意有-2147483648这种数据。 本来还想着,可能是leetcode加数据的审核机制太松,导致被人加了奇怪的数据。。。 结果发现出题人和加数据的人是一个人啊? 不给数据范围,加这种奇怪的数据很有意思? 分分钟卡掉你的标程 …
Read More -
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, the subarray [4,3] has the minimal length under the problem constraint 思路:尺取即可。。好久没写,竟然调了半 …
Read More -
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个数,要求找出出现此处大于n/3的。。。 思路:之前做过一个找出n个数出现此处大于n/2的题目,思想是“非吾族类,其心必异”。。 这道题类似。。。容易知道题目要求的数最多有2个,最少有0个。。。 由于最多两个“族类”,在更新的时候,要判断是不是友军的人...毕竟朋友妻不可欺嘛(什么鬼 最后记得扫一遍,check一下,检查出现此处是 …
Read More -
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. 题意:一个数组,由0,1,2组成,现在要求升序排列 思路:无脑做法就是计数排序,扫两遍,时间复杂 …
Read More -
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not …
Read More -
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. 思路: 排序,然后two pointer,复杂度 O(n^2) /* *********************************************** Author :111qqz Created …
Read More -
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution set must not contain duplicate quadruplets. 思路: O(n^2)枚举两个元素,变成2-sum问题,总体复杂度O(n^3) hash的解法以后补 /* …
Read More -
Given an array S of n integers, are there elements a, b, c in S 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 contain duplicate triplets. 思路:排序O(nlgn),然后枚举一个元素O(n),对于每个元素,在剩下的区间中 two pointer O(n) 整体复杂度 O(n^2)。 hash的解法以后补。 /* …
Read More