国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

LeetCode Two Point & Array Problem 兩點(diǎn)問題匯總

2019-11-11 00:34:50
字體:
供稿:網(wǎng)友

兩點(diǎn)問題總結(jié)

兩點(diǎn)問題很常與數(shù)組一同出現(xiàn),假設(shè)是一個數(shù)組問題,且大體解答思路是數(shù)組內(nèi)坐標(biāo)的移動,例如遍歷,這時可以考慮是兩點(diǎn)問題 兩點(diǎn)問題的關(guān)鍵:定義好兩點(diǎn)的位置和走法

問題匯總

1、Leet Code OJ 11. Container With Most Water

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 slant the container and n is at least 2.

給定n個非負(fù)整數(shù)a1,a2,…,an,其中每個表示坐標(biāo)(i,ai)處的點(diǎn)。 繪制n條垂直線,使得線i的兩個端點(diǎn)在(i,ai)和(i,0)。 找到兩條線,其與x軸一起形成容器,使得容器包含最多的水。

注意:您不能傾斜容器,n至少為2。

代碼 第一種做法是:

public class Solution { public int maxArea(int[] height) { int max = 0; for(int i=0; i<height.length; i++){ for(int j=0; j<i; j++){ int w, h; if(height[i] > height[j]) w = height[j]; else w = height[i]; h = i - j; if(w * h > max) max = w * h; } } return max; }}

但是這種解法,大概是230ms,會超時

第二種做法:

public class Solution { public int maxArea(int[] height) { int max = 0, left = 0, right = height.length - 1; for(int i=0; i<height.length; i++){ max = Math.max(max, Math.min(height[left], height[right]) * (right - left) ); if(height[left] < height[right]) left++; else right--; } return max; }}

定義了兩個點(diǎn),left和right,left從數(shù)組頭開始,right從數(shù)組尾開始 容量等于:width * height(即left 和 right的高度較小值 * right 和 left 的橫坐標(biāo)距離) 然后逐步縮短橫坐標(biāo)距離,目的是嘗試 減少width,增加height,會不會得到更大的容量

2. Leet Code OJ 26. Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example, Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length.

給定一個排序數(shù)組,刪除重復(fù)的位置,使每個元素只出現(xiàn)一次并返回新的長度。 不要為另一個數(shù)組分配額外的空間,必須使用常量內(nèi)存來做到這一點(diǎn)。

例如, 給定輸入數(shù)組nums = [1,1,2] 您的函數(shù)應(yīng)返回length = 2,num的前兩個元素分別為1和2。

代碼:

public int removeDuplicates(int[] A) { if (A.length==0) return 0; int j=0; for (int i=0; i<A.length; i++) if (A[i]!=A[j]) A[++j]=A[i]; return ++j; }

i 用于遍歷數(shù)組,j用于記錄不同的數(shù)的個數(shù)

3. Leet Code OJ 27. Remove Element

Given an array and a value, remove all instances of that value in place and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

Example: Given input array nums = [3,2,2,3], val = 3

Your function should return length = 2, with the first two elements of nums being 2.

給定數(shù)組和值,原位刪除該值的所有實(shí)例,并返回新長度。

不要為另一個數(shù)組分配額外的空間,必須使用常量內(nèi)存來做到這一點(diǎn)。

可以更改元素的順序。 沒有什么你離開超出了新的長度。

例: 給定輸入數(shù)組nums = [3,2,2,3],val = 3

您的函數(shù)應(yīng)返回length = 2,num的前兩個元素為2。

代碼

public class Solution { public int removeElement(int[] nums, int val) { int j =0; for(int i=0; i<nums.length; i++){ if(val != nums[i]){ int tmp = nums[j]; nums[j++] = nums[i]; nums[i] = tmp; } } return j; }}

i用于遍歷數(shù)組,j用于記錄數(shù)組中不同于val的數(shù)的個數(shù),且把這些數(shù)往前移動


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 永德县| 大渡口区| 元阳县| 绍兴市| 旌德县| 马公市| 吉首市| 德令哈市| 常山县| 正镶白旗| 庆城县| 上林县| 西乌| 上杭县| 民县| 驻马店市| 环江| 正镶白旗| 于田县| 抚顺市| 马边| 高安市| 米易县| 朝阳县| 蒙自县| 玛纳斯县| 湖南省| 仪陇县| 舟曲县| 漳平市| 施甸县| 息烽县| 彭泽县| 德惠市| 额尔古纳市| 西乡县| 调兵山市| 铅山县| 肇庆市| 荃湾区| 定西市|