1、LeetCode TwoSum

249 阅读1分钟

/**

* n待查找数组,两数目标之和

* 返回这2个数对应的下标

*/

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

复杂度 O(n)