Two Sum

原题 https://leetcode.com/problems/two-sum/description/

题意: 给定一个整数数组array,以及一个目标整数target,数组中有两个数的和刚好等于目标整数target,找出这两个数在数组中的下标

约定:(1)数组中只有一对这样的整数满足条件 (2)数组中的每个值只能使用一次

例子:

给定:array = [2, 7, 11, 15], target = 9,
因为 array[0] + array[1] = 2 + 7 = 9,
返回 [0, 1].



标签: array、整数、sum、数组、数在、面试
猜你感兴趣的圈子:
LeetCode交流圈
  • hadoop迷
    2017-07-29 22:27:11 1楼#1层

    暴力法:

    public class Solution {
        public static int[] twoSum(int[] nums, int target) {
            int[] result = new int[2];
            for (int i = 0; i < nums.length; i++) {
                for (int j = 0; j < nums.length; j++) {
                    if (i!=j&&nums[i] + nums[j] == target) {
                        result[0] = i;
                        result[1] = j;
                        return result;
                    }
                }
            }
    
            return null;
        }
    
        public static void main(String[] args) {
            int[] nums = new int[]{2, 7, 11, 15};
            int target = 9;
            int[] result = twoSum(nums, target);
            System.out.println("[" + result[0] + "," + result[1] + "]");
        }
    }

  • Tony
    2017-08-08 22:40:06 2楼#1层

    先排序,后一次遍历,时间复杂度O(NlogN)

    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;
    
    public class Solution {
        //总时间复杂度O(nlogn)
        public static int[] twoSum(int[] nums, int target) {
            int[] result = new int[2];
            int len = nums.length;
    
            //(1)保存原有数组的下标索引,时间复杂度O(n)
            Map<Integer, Integer> indexMap = new HashMap<Integer, Integer>();
            //数组中有重复的情况,如:nums=[3,3] target=6
            Map<Integer, Integer> duplicateMap = new HashMap<Integer, Integer>();
            for (int i = 0; i < len; i++) {
                if (indexMap.containsKey(nums[i])) {
                    duplicateMap.put(nums[i], i);
                } else {
                    indexMap.put(nums[i], i);
                }
            }
    
            //(2) 排序 O(nlogn)
            Arrays.sort(nums);
    
            //(3)一次遍历O(n)
            int tmp;
            for (int i = 0, j = len - 1; i < j; ) {
                tmp = nums[i] + nums[j];
                if (tmp == target) {
                    if (nums[i] == nums[j]) {
                        result[0] = indexMap.get(nums[i]);
                        result[1] = duplicateMap.get(nums[j]);
    
                        if (result[0] > result[1]) {
                            tmp = result[0];
                            result[0] = result[1];
                            result[1] = tmp;
                        }
                    } else {
                        result[0] = indexMap.get(nums[i]);
                        result[1] = indexMap.get(nums[j]);
    
                        if (result[0] > result[1]) {
                            tmp = result[0];
                            result[0] = result[1];
                            result[1] = tmp;
                        }
                    }
                    return result;
                } else if (tmp > target) {
                    j--;
                } else {
                    i++;
                }
    
            }
            return null;
        }
    
        public static void main(String[] args) {
            int[] nums = new int[]{3, 3};
            int target = 6;
            int[] result = twoSum(nums, target);
            System.out.println("[" + result[0] + "," + result[1] + "]");
        }
    }

  • Tony
    2017-08-08 22:51:29 3楼#1层

    两次遍历:

    import java.util.HashMap;
    import java.util.Map;
    
    public class Solution {
        //总时间复杂度O(n),空间复杂度O(n)
        public static int[] twoSum(int[] nums, int target) {
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
            for (int i = 0; i < nums.length; i++) {
                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;
        }
    
        public static void main(String[] args) {
            int[] nums = new int[]{3, 3};
            int target = 6;
            int[] result = twoSum(nums, target);
            System.out.println("[" + result[0] + "," + result[1] + "]");
        }
    }

  • Tony
    2017-08-08 22:52:43 4楼#1层

    一次遍历:

    import java.util.HashMap;
    import java.util.Map;
    
    public class Solution {
        //总时间复杂度O(n),空间复杂度O(n)
        public static int[] twoSum(int[] nums, int target) {
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
            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);
            }
            return null;
        }
    
        public static void main(String[] args) {
            int[] nums = new int[]{3, 3};
            int target = 6;
            int[] result = twoSum(nums, target);
            System.out.println("[" + result[0] + "," + result[1] + "]");
        }
    }

  • 回复
隐藏