[LeetCode] [TOP-1] [] and the two numbers

Title Description

  Given an integer array nums and a target value target, and ask you to identify the target value of the two integers in the array, and return to their array subscript. You can assume that each input corresponds to only one answer. However, you can not re-use the same array element.
Example
given nums = [2, 7, 11 , 15], target = 9, because nums [0] + nums [1 ] = 2 + 7 = 9, is returned [0, 1]

Ideas analysis

  1. Solutions can be violent
  2. The characteristic map may be utilized java hash table set, through the array, a hash table in memory element, and simultaneously determines (target value - the current element) whether the results obtained in the hash table, the hash table if these two subscripts is returned.
  3. Way to achieve their own hash table, the direct use of the array.

Java code

public class TOP001 {
    public int[] twoSum(int[] nums, int target) {
        return Solution3(nums, target);
    }
    
    /**
     * 解法一:暴力解法
     * @param nums
     * @param target
     * @return
     */
    public int[] Solution1(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    return new int[] { i, j };
                }
            }
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    /**
     * 解法二: 这里利用了java集合中map 哈希表的特性,遍历数组,将元素存入哈希表中, 并同时判断(目标值-当前元素)得到的结果是否在哈希表中,
     * 如果在哈希表中则返回这两个下标,
     * 
     * @param nums
     * @param target
     * @return
     */
    public int[] Solution2(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            int targetResult = target - nums[i];
            if (map.get(nums[i]) != null && nums[i] * 2 == target) {
                //此处针对于 2 +2 =4  4+4=8 这种 存在重复的情况,且重复的加起来正好是target
                return new int[] { map.get(targetResult), i };
            }
            if (map.containsKey(targetResult)) {
                return new int[] { map.get(targetResult), i };
            }
            map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    /**
     * 解法三:
     * 利用手动哈希的方法,和解法二思路相同
     * @param nums
     * @param target
     * @return
     */
    public static int[] Solution3(int[] nums, int target) {
        int indexArrayMax = 2047;
        int[] indexArrays = new int[indexArrayMax + 1];
        for (int i = 0; i < nums.length; i++) {
            int diff = target - nums[i];
            int index = diff & indexArrayMax;
            if (indexArrays[index] != 0) {
                return new int[] { indexArrays[index] - 1, i };
            }
            indexArrays[nums[i] & indexArrayMax] = i + 1;
        }
        throw new IllegalArgumentException("No two sum value");
    }
}

Code link

Offer to prove safety codes -Java

Guess you like

Origin www.cnblogs.com/haoworld/p/leetcodetop1-liang-shu-zhi-he.html