package LeetCode;
import java.util.Arrays;
import java.util.HashMap;
/*要求: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.
*给一个数组然后还有一个目标值,输出相加等于目标值的两个数组的下标
*/
public class TwoSum {
/**
* 这个方案最简单,刚开始被系统判断为超时不通过,改进之后就通过
*
* @param nums
* @param target
* @return
*/
public static int[] twoSum1(int[] nums, int target) {
if (nums == null)
return null;
int[] result = new int[2];
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
result[0] = i;
result[1] = j;
}
}
}
return result;
}
/**
* 这种方法只是第一种方法变成了减法,但是通过,不知什么原因。
*
* @param nums
* @param target
* @return
*/
public static int[] twoSum2(int[] nums, int target) {
int[] answer = new int[2];
for (int i = 0; i < nums.length; ++i) {
answer[0] = i;
int b = target - nums[i];
for (int j = i + 1; j < nums.length; ++j) {
if (nums[j] == b) {
answer[1] = j;
return answer;
}
}
}
return null;
}
/**
* 这是hashMap的做法,时间复杂度变为o(n)
*
* @param nums
* @param target
* @return
*/
public static int[] twoSum3(int[] nums, int target) {
int[] answer = new int[2];
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; ++i) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; ++i) {
int b = target - nums[i];
if (map.containsKey(b) && i != map.get(b))
return new int[] { i, map.get(b) };
}
return answer;
}
/**
*
* @param nums
* @param target
* @return
*/
public static int[] twoSum4(int[] nums, int target) {
int numsLength = nums.length;
if (numsLength < 2) {
return nums;
}
int[] result = new int[2];
HashMap<Integer, Integer> numsInDictionary = new HashMap<>();
for (int i = 0; i < numsLength; i++) {
if (numsInDictionary.containsKey(target - nums[i])) {
result[1] = i;
result[0] = numsInDictionary.get(target - nums[i]);
break;
}
if (!numsInDictionary.containsKey(nums[i])) {
numsInDictionary.put(nums[i], i);
}
}
return result;
}
/**
*
* @param nums
* @param target
* @return
*/
public static int[] twoSum5(int[] nums, int target) {
if (nums == null)
return null;
int[] nums2 = Arrays.copyOf(nums, nums.length);
Arrays.sort(nums2);
int a = 0, b = 0;
int start = 0, end = nums2.length - 1;
// find two nums
while (start < end) {
int sum = nums2[start] + nums2[end];
if (sum < target)
start++;
else if (sum > target)
end--;
else {
a = nums2[start];
b = nums2[end];
break;
}
}
// find the index of two numbers
int[] res = new int[2];
for (int i = 0; i < nums.length; i++) {
if (nums[i] == a) {
res[0] = i;
break;
}
}
if (a != b) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == b) {
res[1] = i;
break;
}
}
} else {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == b && i != res[0]) {
res[1] = i;
break;
}
}
}
return res;
}
public static void main(String[] args) {
int[] n = new int[] { 3, 2, 4 };
System.out.println(Arrays.toString(twoSum5(n, 6)));
}
}
1 twoSum
最新推荐文章于 2021-11-15 11:03:37 发布