leetCode Q1:Two Num(java)

1、题目链接:https://leetcode.com/problems/two-sum/description/

2、题目内容(楼主直接用中文描述了):

给一个一维数组,一个目标值target,要求返回数组里其中两个数的和为target的对应的下标值。

例如:

 

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

3、思路:

 

题目很简单,用蛮力法遍历两次即可。不过楼主不知道是哪根筋抽了,想着如果先排个序,再遍历数组效率会提高。为什么呢?因为楼主觉得如果先排个序,前面小的加上后面大的若还没得到和为target并且还超过了target的值,说明内循环后面的剩下的值不用再遍历了。嗯,咋一看好像还蛮有道理的。

但!是!

我排个序的复杂度忘了考虑进去。楼主用的是冒泡法(其他的忘得差不多了,冒泡最熟),复杂度本来就达到了O(n^2)。如果不用我这种方法直接用蛮力法,复杂度也是O(n^2),相当于楼主在增加复杂度(- -)。而且楼主排个序之后,因为题目要求是返回下标值,所以楼主还得额外增加空间复杂度的开销,多加一个数组来复制题目给的数组,最后再遍历一次得到对应的下标值(- -路越走越弯了)。

虽然如此,还是记录一下本楼主第一次Accepted的代码(用上面排序后的思路),以后少走弯路……

下面贴上楼主第一次Accepted的代码,以及修改后的简单粗暴的蛮力法,还有两个leetCode里面效率较高的算法(楼主自己实现了一下,做了一丢丢不起眼的注释,最后一个是压轴,建议看)。

4、楼主第一次Accepted的代码(建议跳过):

 

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] answerValue = new int[2];
        int[] answerIndex = new int[2];
        int[] nums1 = new int[nums.length];
        
        for(int i = 0; i < nums.length; i++) {
            nums1[i] = nums[i];
        }
        
        //sort nums:from small to large
        for(int i = 0; i < nums.length-1; i++) {
            for(int j = 0; j < nums.length-1-i; j++) {
                int t = 0;
                if(nums[j] > nums[j+1]) {
                    t = nums[j];
                    nums[j] = nums[j+1];
                    nums[j+1] = t;
                }
            }
        }
        
        for(int i = 0; i < nums.length-1; i++) {
            for(int j = i+1; j < nums.length; j++) {
                if(nums[i] + nums[j] == target) {
                    answerValue[0] = nums[i];
                    answerValue[1] = nums[j];
                    i = nums.length;
                    break;
                } else if(nums[i] + nums[j] > target) {
                    break;
                }
            }
        }
        
        for(int i = 0; i < nums1.length; i++) {
            if(nums1[i] == answerValue[0] && answerIndex[0] == 0) {
                answerIndex[0] = i;
            } else if(nums1[i] == answerValue[1] && answerIndex[1] == 0) {
                answerIndex[1] = i;
            }
        }
        
        if(answerIndex[0] > answerIndex[1]) {
            int t = answerIndex[0];
            answerIndex[0] = answerIndex[1];
            answerIndex[1] = t;
        }
        return answerIndex;
    }
}

(时间复杂度:O(n^2),空间复杂度:O(n)_by我增加的nums1的开销)

 

运行结果:


(可以看到运行时间206ms,打败了0.45%的java代码提交(也就是说楼主被99.55%的人打败了))

5、楼主修改后简单粗暴的蛮力法:

 

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

(时间复杂度:O(n^2), 空间复杂度:O(1))
运行结果:

 

(可以看到运行时间43ms,打败了22.53%的人,比第一个好多了 !)

6、leetCode高效率算法1(hashMap实现):

 

//use hashMap
class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap();
        for(int i = 0; i < nums.length; i++) {
            //putting the values to the positions of key can use Map.containsKey() to find the values
            map.put(nums[i], i);
        }
        
        for(int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if(map.containsKey(complement) && map.get(complement) != i) {
                return new int[]{i, map.get(complement)};
            }
        }
        return null;
    }
}

(时间复杂度:O(n),空间复杂度:O(n)_byMap数组的开销)
运行结果:

 

(可以看到运行时间10ms,打败了51.94%的人,效率明显上升有没有!)

7、leetCode压轴算法(用一个数组实现快速定位,比较烧脑,建议沉下心看):

 

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] map = new int[22222];
        int size = 5;   //防止不要让map的index出现-x的情况。给的值可能有负值。
        for (int i = 0; i < nums.length-1; i++) {
          map[nums[i]+size] = i + 1;
          int diff = target - nums[i + 1] + size;
          if (diff < 0) continue;
          int d = map[diff];
          if (d > 0)
              return new int[]{d - 1, i + 1};
        }
        return null;
    }
}

(时间复杂度:O(n),空间复杂度:O(n))

 

这个比较烧脑,楼主贴上运行结果后再讲一下楼主的理解过程:


(可以看到运行时间4ms,打败了99.87%的人,效率蹭蹭地往上升有没有!!!)

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值