LeetCode Two Point & Array Problem 两点问题汇总

两点问题总结

两点问题很常与数组一同出现,假设是一个数组问题,且大体解答思路是数组内坐标的移动,例如遍历,这时可以考虑是两点问题
两点问题的关键:定义好两点的位置和走法

问题汇总

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个非负整数a1,a2,…,an,其中每个表示坐标(i,ai)处的点。 绘制n条垂直线,使得线i的两个端点在(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;
    }
}

定义了两个点,left和right,left从数组头开始,right从数组尾开始
容量等于:width * height(即left 和 right的高度较小值 * right 和 left 的横坐标距离)
然后逐步缩短横坐标距离,目的是尝试 减少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.

给定一个排序数组,删除重复的位置,使每个元素只出现一次并返回新的长度。
不要为另一个数组分配额外的空间,必须使用常量内存来做到这一点。

例如,
给定输入数组nums = [1,1,2]
您的函数应返回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 用于遍历数组,j用于记录不同的数的个数

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.

给定数组和值,原位删除该值的所有实例,并返回新长度。

不要为另一个数组分配额外的空间,必须使用常量内存来做到这一点。

可以更改元素的顺序。 没有什么你离开超出了新的长度。

例:
给定输入数组nums = [3,2,2,3],val = 3

您的函数应返回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用于遍历数组,j用于记录数组中不同于val的数的个数,且把这些数往前移动

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值