LeetCode(1)--TwoSum

来源:互联网 发布:淘宝女装店铺名称 编辑:程序博客网 时间:2024/06/06 07:38

Question:

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].

个人思路:

1、根据题目可知所给定的数组不一定是有序数组,所以先对数组排序;2、取出排序后数组的第一个元素(nums[0])与target相比较,如果nums[0]>=target,说明该数组中不可能有两个元素之和为target;3、如果nums[0]<target,则寻找数组中第一个大于target的元素下标index;此时能够满足条件的数组范围为[0,index-1];4、定义left=0;right=index-1;计算nums[left] + nums[right]    4.1、如果nums[left] + nums[right] > target,则right减一;    4.2、如果nums[left] + nums[right] < target,则left加一;    4.3、如果nums[left] + nums[right] = target,则返回nums[left] 和 nums[right]在原始数组中的下标。

代码实现如下:

  /**    * 获取数组中任意两个元素之和等于target的下标    * @param nums 数组    * @param target 目标值    * @return 两个元素的下标    */   public static int[] twoSum(int[] nums, int target) {       // 复制原始数组       int[] temp = Arrays.copyOf(nums, nums.length);       // 先对数组排序       Arrays.sort(temp);       // 数组最小值大于target说明数组的任意两个元素之和不会等于target       if (target <= temp[0]) {           return null;       }       // 寻找数组中大于等于target的第一个元素下标       int index = 0;       int length = temp.length;       for (int i = 0; i < length; i++) {           if (temp[i] >= target) {               index = i - 1;               break;           }       }       // 在小于target的有序数组中寻找元素下标       int left = 0;       int right = index;       int[] result = new int[2];       while (left < right) {           if (temp[left] + temp[right] > target){               right--;           } else if (temp[left] + temp[right] < target) {               left++;           } else if (temp[left] + temp[right] == target) {               result[0] = getIndex(nums,temp[left]);               result[1] = getIndex(nums,temp[right]);               Arrays.sort(result);               return result;           }       }       return null;   }   /**    * 获取数组中某元素的下标    * @param nums 数组    * @param num 具体元素    * @return 元素在数组中的下标    */   public static int getIndex(int[] nums, int num) {       int index = 0;       while (nums[index] != num) {           index++;       }       return index;   }

LeetCode的参考答案:

/** * 该方法很妙,先将数组元素添加至Map集合,直到在集合中找到与当前元素和为target的元素,并返回下标 * @param nums * @param target * @return */public int[] twoSum(int[] nums, int target) {    Map<Integer, Integer> map = new HashMap<>();    for (int i = 0; i < nums.length; i++) {        int complement = target - nums[i];        if (map.containsKey(complement)) {            return new int[] { map.get(complement), i };        }        map.put(nums[i], i);    }    throw new IllegalArgumentException("No two sum solution");}/** * 该方法属于蛮力法,一个个去试,直到满足条件 * @param nums * @param target * @return */public int[] twoSum(int[] nums, int target) {    for (int i = 0; i < nums.length; i++) {        for (int j = i + 1; j < nums.length; j++) {            if (nums[j] == target - nums[i]) {                return new int[] { i, j };            }        }    }    throw new IllegalArgumentException("No two sum solution");}

总结:

自己思考问题缺少全局观,限定自己的思维在具体的步骤上,解决方案不够灵活,总觉得数组要在有序的情况下才能很好地操作;也说明自己见得太少,想法太少。
原创粉丝点击