Leetcode11 14 15

11.盛最多水的容器

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

思路:根据短板原理,容器的最大蓄水量由端点值的最小值确定。本题可以使用单个索引的解法暴力解题,但会超出时间限制。故而采取双索引的方法解题,只需从端点值较小的一段向里缩进一格,依次计算蓄水量与当前最大蓄水量进行比较即可。
代码如下:

public class Solution
{
    public int MaxArea(int[] height)
    {
        int i = 0;
        int j = height.Length - 1;
        int temp = 0;
        int max=0;
        while(i<j)
        {
            temp=(j-i)*Math.Min(height[i],height[j]);
            if (temp > max)
                max = temp;
            if (height[i] < height[j])
                i++;
            else
                j--;
        }
        return max;
    }
}

14.最长公共前缀

题目描述:编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 “”。

思路:以字符串组的第一个字符串的前缀为标准,依次判断之后的字符串是否有该前缀。
代码如下:

public class Solution
{
    public string LongestCommonPrefix(string[] strs)
    {
        if(strs.Length==0)
            return string.Empty;
        if (string.IsNullOrEmpty(strs[0]))
            return string.Empty;
        string temps=string.Empty;
        int i,j;
        for (i = 0; i < strs[0].Length; i++)
        {
            temps=strs[0].Substring(0,i+1);
            for (j = 1; j < strs.Length; j++)
            {
                if (strs[j].Length < temps.Length || strs[j][i] != strs[0][i])
                    break;
            }
            if (j < strs.Length)
            {
                temps = strs[0].Substring(0, i);
                break;
            }
        }
        return temps;
    }
}

15.三数之和

*题目描述:给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组
*
思路:先将数组排序以减少不必要的循环,使用三个索引,一个索引从头遍历到尾,它的相反数作为目标数(和为0),另外两个索引一个是第一个索引之后数字的头,另一个是尾,由于已经排好了序,如果这两个数之和大于或小于目标数,可以将这两个索引对应移动一个单位,直到找到三数之和为0。判断列表中是否有与这三个数重复的组,没有则加入。
代码如下:

public class Solution
{
    public IList<IList<int>> ThreeSum(int[] nums)
    {
            IList<IList<int>> ilst = new List<IList<int>>();
            Array.Sort(nums);
            if (nums.Length==0||nums[0] > 0)
                return ilst;
            for (int i = 0; i < nums.Length - 2; i++)
            {
                int l = i + 1;
                int r = nums.Length - 1;
                while (l < r)
                {
                    if (nums[l] + nums[r] < (-1)*nums[i])
                        l++;
                    else if (nums[l] + nums[r] > (-1)*nums[i])
                        r--;
                    else
                    {
                        if (ilst.Count == 0)
                            ilst.Add(new List<int>() { nums[i], nums[l], nums[r] });
                        else
                        {
                            int j;
                            for (j = ilst.Count-1; j >-1; j--)
                            {
                                if (ilst[j][0] == nums[i] && ilst[j][1] == nums[l] && ilst[j][2] == nums[r])
                                    break;
                            }
                            if(j==-1)
                                ilst.Add(new List<int>() { nums[i], nums[l], nums[r] });
                        }
                        l++;
                    }
                }
            }
            return ilst;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值