leetcode数据结构入门 第一天 数组

1.两数之和

public int[] twoSum(int[] nums, int target) {
    int[] res = new int[2];
    if(nums == null || nums.length == 0){
        return res;
    }
    Map<Integer, Integer> map = new HashMap<>();
    for(int i = 0; i < nums.length; i++){
        int temp = target - nums[i];
        if(map.containsKey(temp)){
            res[1] = i;
            res[0] = map.get(temp);
        }
        map.put(nums[i], i);
    }
    return res;
}

这道题要求时间复杂度小于O(n²),所以不能用暴力循环,使用map一边遍历数组一边将nums中的值存进map中,得到时间复杂度为O(n)

15.三数之和

拿到这道题,会有一个想法,首先遍历整个数组,将这道题转化成求target=-nums[i],类似于两数之和的办法。但这道题用这种做法还需要另外注意一个地方,针对重复问题,其实使用hashset就可以解决,但是会造成超时问题,所以,在使用hashset的基础上,使用i > 0 && nums[i] == nums[i - 1],来进行剪枝,对于[-1,-1,-1,-1,2]这种情况,在取第二个-1的时候,就可以跳过遍历,以此来减少运行时间。

public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> re = new ArrayList<List<Integer>>();
        for (int i = 0; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            HashMap<Integer, Integer> map = new HashMap();
            for (int j = i + 1; j < nums.length; j++) {
                int target = -nums[i] - nums[j];
                if (map.containsKey(target)) {
                    re.add(Arrays.asList(nums[i], target, nums[j]));
                } else {
                    map.put(nums[j], j);
                }
            }
        }
        HashSet set = new HashSet(re);
        re.clear();
        re.addAll(set);
        return re;
    }

还有另外一种思路,排序后三元组的第一个元素nums[i]必然是小于等于0的,接着从i的右边第一个元素指针为left,最后一个元素指针为right,若nums[i] + nums[left] + nums[right]大于0,则right左移,小于0,left右移,直到等于0。现在还有一个问题就是重复问题,当找到nums[i] + nums[left] + nums[right]=0时,若nums[right] == nums[right - 1]右指针左移,nums[left] == nums[left + 1]左指针右移。

public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) {
                return result;
            }
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int left = i + 1;
            int right = nums.length - 1;
            while (right > left) {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum > 0) {
                    right--;
                } else if (sum < 0) {
                    left++;
                } else {
                    result.add(Arrays.asList(nums[i], nums[left], nums[right]));
                    while (right > left && nums[right] == nums[right - 1]) right--;
                    while (right > left && nums[left] == nums[left + 1]) left++;
                    right--;
                    left++;
                }
            }
        }
        return result;
    }

217.存在重复元素

public boolean containsDuplicate(int[] nums) {
        HashMap a = new HashMap();
        for(int i=0;i< nums.length;i++){
            if(a.get(nums[i])==null){
                a.put(nums[i],i);
            }else{
                return true;
            }
        }
        return false;
    }

这是一道简单的哈希表问题,还可以利用set()去除重复元素,再根据数组长度和原来对比,小于则代表原数组含有重复元素。

return new Set(nums).size < nums.length;

还可以利用HashSet元素不可重复性

public boolean containsDuplicate(int[] nums) {
        Set set = new HashSet();
        for (int i = 0; i < nums.length; i++)
            if (!set.add(nums[i]))
                return true;
        return false;
    }

也可以利用Arrays.sort函数对数组进行排序

public boolean containsDuplicate(int[] nums) {
        Arrays.sort(nums);
        for (int i = 1; i < nums.length; i++)
            if (nums[i] == nums[i-1])
                return true;
        return false;
    }

53.最大子序和

public int maxSubArray(int[] nums) {
        int[] dp = new int[nums.length];
        dp[0] = nums[0];
        int max = dp[0];
        for(int i=1;i< nums.length;i++){
            dp[i] = Math.max(dp[i-1]+nums[i],nums[i]); //递推公式
            max = Math.max(max,dp[i]);
        }
        return max;
    }

这是一道动态规划题,一般动态规划题先要找他的递推公式dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]),这也是分治法的思想。或者有更简单的办法,之前的和为正数则保留,否则重新赋值。

public int maxSubArray(int[] nums) {
        int res = nums[0];
        int sum = 0;
        for (int num : nums) {
            if (sum > 0)
                sum += num;
            else
                sum = num;
            res = Math.max(res, sum);
        }
        return res;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值