1. Two Sum(Easy)

题目描述

原题

Description:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

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

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

原题翻译

描述

给定一个整数数组,返回两个数字的索引,使它们相加到特定目标。
假设每个输入仅有一个解决方案,且不能两次使用相同的元素。

例如
给定 nums = [2, 7, 11, 15], target = 9。
由于 nums[0] + nums[1] = 2 + 7 = 9,
返回 [0, 1]。

解法一(mine)

主要思想

Exm?怎么会这么简单?我真的不是在做《C语言程序设计》的期中考试题?

但细细想来,也确实配得上Easy的难度,两层for循环,即可轻松解决。

时间复杂度O(n^2)

源码

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

解法二

大佬的答案…渐渐自闭…

主要思想

  1. 创建一个 HashMap<Integer, Integer> 类型的map,key 存放数值,value 存放下标。
  2. 循环遍历数组nums,并把key-value放入map,判断在 map 中是否存在 target-nums[i]:
    (1)若存在,直接将两个下标存入 result 数组,并返回;
    (2)若不存在,将 nums[i] 的值和下标放入 map,进入下一次循环。
    时间复杂度:O(n)

源码

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

Warning:这个解法虽然看起来完美,但它仅仅打败了71.76%的回答…

解法三

巨佬的答案,打败了99%的回答…深度自闭…

主要思想

(1) 复制一份原数组,对新数组排序,再使用快速查找算法得到答案。

(2) 如果拿到了答案,遍历原序数组,得到索引值,并加入结果数组。

源码

public class Solution {
    public int[] twoSum(int[] nums, int target) {
        int low = 0;
        int high = nums.length-1;
        int[] result = new int[2];
        int[] aux = Arrays.copyOf(nums, nums.length);
        Arrays.sort(nums);
        while(low < high) {
            int sum = nums[low] + nums[high];
            if(sum == target) {
                result[0] = nums[low];
                result[1] = nums[high];
                break;
            }else if(sum > target) {
                high--;
            } else {
                low++;
            }
        }
        int index = 0;
        int[] finalRes = new int[2];
        for(int i=0; i<aux.length; i++) {
            if(aux[i] == result[0] || aux[i] == result[1]) {
                finalRes[index] = i;
                index++;
            }
        }
        return finalRes;
    }
}

解法四

让我们来看一下剩下的1%写了些什么?

主要思想

抱歉…在写下这篇笔记的时候,我依然没看懂…(如果有大神看到,请救救我…)

源码

public class Solution {
    public int[] twoSum(int[] nums, int target) {
	int max = 2048;
	int[] indexes = new int[max];
	int bitMode = --max;
	int first = nums[0];
	for (int i = 1; i < nums.length; i++) {
	  int difference = target - nums[i];
	  if (difference == first) {
		return new int[]{0, i};
	  }
	  int index = indexes[difference & bitMode];
	  if(index != 0) {
		return new int[]{index, i};
	  }
	  indexes[nums[i]&bitMode] = i;
	}
	return new int[0];
    }
}

猜你喜欢

转载自blog.csdn.net/gaoxuekun/article/details/86664993