1. Two Sum 难度:Easy
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].
翻译:给出一个数组和一个目标和,从这个数组中找到两个数字之和为 target 的数,并记录下这两个数的下标 。
package pers.leetcode;
import java.util.HashMap;
import java.util.Map;
/**
* LeetCode 第一题 难易程度: Easy
*
* @author admin
* @date 2019/3/11 16:15
*/
public class TwoSum {
public static void main(String[] args) {
int[] a = {2, 7, 11, 15};
int target = 9;
int[] test = twoSum(a, target);
for (int i=0; i<test.length; i++){
System.out.println(test[i]);
}
}
/**
* 我的解法
*
* @param nums 给出的数组
* @param target 给定目标总和
*/
public static int[] twoSum(int[] nums, int target){
int[] result = new int[2];
if (nums == null || nums.length == 0){
return result;
}
for (int i=0; i<nums.length-1; i++){
for (int j=i+1; j<nums.length; j++){
if (target == (nums[i] + nums[j])){
result[0] = i;
result[1] = j;
break;
}
}
}
return result;
}
/**
* 快速解法
*
* @param nums 给出数组
* @param target 目标和
* @return 两数下标
*/
public static int[] leetcode(int[] nums, int target){
Map<Integer, Integer> others = new HashMap<Integer, Integer>(nums.length);
for (int i=0; i<nums.length; i++){
if (others.containsKey(target - nums[i])){
return new int[]{ others.get(target - nums[i]), i};
}
others.put(nums[i], i);
}
throw new IllegalArgumentException("No such solution");
}
}