LeetCode(七)

题目

给你 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

示例 1:

在这里插入图片描述

输入:[1,8,6,2,5,4,8,3,7]
输出:49
示例2
输入:height = [1,1]
输出:1
示例3
输入:height = [4,3,2,1,4]
输出:16
一, C语言
int maxArea(int* height, int heightSize){
    int sum=0;
    int sum1=0;
    for(int i=0;i<heightSize-1;i++){
        for(int j=i+1;j<heightSize;j++){
            if(height[i]>=height[j])
                sum1=height[j]*(j-i);
            if(height[i]<height[j])
                sum1=height[i]*(j-i);
            if(sum1>sum){
                    sum=sum1;
                }
            
        }
    }
return sum;
}

在这里插入图片描述
这就相当尴尬!!!暴力计算看来是不行了

方法2
int maxArea(int* height, int heightSize){
    int i,j;
    int max = -1,sum;
    i = 0;
    j = heightSize - 1;

    while(i < j){
        sum = 0;    
        if(height[i] <= height[j]){
            sum = (j - i) * height[i];
            i++;
        }else{
            sum = (j - i) * height[j];
            j--;
        }
        if(sum > max)
            max = sum;
    }
    return max;
}

在这里插入图片描述

二,Java
class Solution {
    public int maxArea(int[] height) {
    int sum;
    int max=-1;
    int i=0;
    int j=height.length-1;
     while(i<j){
         if(height[i]<=height[j]){
             sum=(j-i)*height[i];
             i++;
             }

        else
           { sum=(j-i)*height[j];
            j--;}
        if(sum>max){
            max=sum;
        }
     }

return max;
    }
}

在这里插入图片描述

解题思路:运用双指针

设置双指针 i,jj分别位于容器壁两端,根据规则移动指针,并且更新面积最大值 max,直到 i == j 时返回max。

三,Python
class Solution(object):
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        i,j,sum=0,len(height)-1,0
        while i<j:
            if height[i]<=height[j]:
                 sum=max(sum,(j-i)*height[i])
                 i+=1
            else:
                sum=max(sum,(j-i)*height[j])
                j-=1
        return sum
           

在这里插入图片描述

题目

给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。

注意:答案中不可以包含重复的三元组。

示例1
输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]
示例2
输入:nums = []
输出:[]
示例3
输入:nums = [0]
输出:[]
一,Java
class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> ls = new ArrayList<>();
 
        for (int i = 0; i < nums.length - 2; i++) {
            if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) {  // 跳过可能重复的答案
 
                int l = i + 1, r = nums.length - 1, sum = 0 - nums[i];
                while (l < r) {
                    if (nums[l] + nums[r] == sum) {
                        ls.add(Arrays.asList(nums[i], nums[l], nums[r]));
                        while (l < r && nums[l] == nums[l + 1]) l++;
                        while (l < r && nums[r] == nums[r - 1]) r--;
                        l++;
                        r--;
                    } else if (nums[l] + nums[r] < sum) {
                        while (l < r && nums[l] == nums[l + 1]) l++;   // 跳过重复值
                        l++;
                    } else {
                        while (l < r && nums[r] == nums[r - 1]) r--;
                        r--;
                    }
                }
            }
        }
        return ls;
    }
}

在这里插入图片描述

二,Python
class Solution(object):
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        n=len(nums)
        res=[]
        if(not nums or n<3):
            return []
        nums.sort()
        res=[]
        for i in range(n):
            if(nums[i]>0):
                return res
            if(i>0 and nums[i]==nums[i-1]):
                continue
            L=i+1
            R=n-1
            while(L<R):
                if(nums[i]+nums[L]+nums[R]==0):
                    res.append([nums[i],nums[L],nums[R]])
                    while(L<R and nums[L]==nums[L+1]):
                        L=L+1
                    while(L<R and nums[R]==nums[R-1]):
                        R=R-1
                    L=L+1
                    R=R-1
                elif(nums[i]+nums[L]+nums[R]>0):
                    R=R-1
                else:
                    L=L+1
        return res

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值