1. 两数之和 leedcode java代码分析

题目描述

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]

1 暴力法

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int n = nums.length;
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                if (nums[i] + nums[j] == target) {
                    return new int[]{i, j};
                }
            }
        }
        return new int[0];
    }
}

2 HashMap

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int desired = target - nums[i];
            if (map.containsKey(desired)) {
                return new int[] {map.get(desired), i};  // 直接返回匿名数组
            } else {
                map.put(nums[i], i);
            }
        }
        return new int[2];
    }
}

这个int[] 后面的{}我想了好久,开始想歪了,原来就是返回一个匿名数组

2.1 为什么推荐使用Map map = new HashMap() 而不是 HashMap map = new HashMap() ?

Map map = new HashMap();
Map是一个接口,HashMap是具体的实现类。
由于接口是类的蓝图,是一个抽象的概念,不能被实例化,因此接口需要由具体的类来实现。
这条代码指明:由HashMap类来实现接口Map中描述的方法。

HashMap map = new HashMap();
声明一个HashMap类型的map,由HashMap类实现。

为什么更推荐第一种用接口的声明方式?
这个问题等同于为什么要在编程中使用接口,而不是直接使用实现类。其实这就是面对对象编程(OOP)的思想精髓。简单来说就是:上层接口描述的功能不变,下层的具体实现可以不断修改替换。上层的调用者只用知道map的功能,不必关心map的具体实现。

例如,某天开发人员开发出一个各方面性能都优于HashMap的SuperMap类,则map可以直接改成由SuperMap来实现:Map map = new SuperMap()。对于外部调用者来说,使用的还是那个map,殊不知底层实现的升级已经让他们用上了优化版的map。如果一开始就定义map为HasMap类型,无法做出这样的优化,很明显 HashMap map = new SuperMap() 是条错误的代码。这就是使用接口声明的好处,增加系统灵活性,隔离性等。

转自:https://blog.csdn.net/qq_45477595/article/details/106087948

3 双指针方法

3.1如果数组有序可以直接用

 public int[] twoSum(int[] nums, int target) {
        if (nums == null || nums.length <= 1) return new int[2];
        int left = 0;
        int right = nums.length - 1;
        while (left < right) {
            int sum = nums[left] + nums[right];
            if (sum < target) {
                left++;
            } else if (sum > target) {
                right--;
            } else {
                return new int[] {left, right};
            }
        }
        return new int[2];
    }

3.2无序

如果无序还得先排序好,在排序前还要记录原来的数组下表比较麻烦。

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> ans;
        vector<int> temp;
        temp=nums;
        int n=temp.size();
       sort(temp.begin(),temp.end());
       int i=0,j=n-1;
       while(i<j){  
           if(temp[i]+temp[j]>target)j--;
          else if(temp[i]+temp[j]<target)i++;
          else break; 
       }
       if(i<j){
      for(int k=0;k<n;k++){
          if(i<n&&nums[k]==temp[i]){
              ans.push_back(k);
              i=n;
          }
         else if(j<n&&nums[k]==temp[j]){
              ans.push_back(k);
              j=n;
          }
          if(i==n&&j==n)return ans;
      }
      }
        return ans;
    }
};

作者:yun-yu-chen
链接:https://leetcode-cn.com/problems/two-sum/solution/san-chong-fang-fa-bao-li-shuang-zhi-zhen-ha-xi-san/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

转自:https://leetcode-cn.com/problems/two-sum/solution/javashi-pin-jiang-jie-xi-lie-two-sum-by-sean-kuang/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值