给定一个整数数组 nums 和一个目标值 target, 请你在该数组中找出和为目标值的那 两个 整数, 并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。 但是,数组中同一个元素不能使用两遍。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]
通过一:暴力for for
class SolutionTest {
public int[] twoSum(int[] nums, int target) {
for(int i=0;i < nums.length-1;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");
}
}
通过二:两遍哈希表 HashMap
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
for(int i=0;i<nums.length;i++) {
hm.put(nums[i], i);
}
for(int i=0;i<nums.length;i++) {
int complete = target - nums[i];
if(hm.containsKey(complete) && hm.get(complete)!=i) {
return new int[] {i,hm.get(complete)};
}
}
throw new IllegalArgumentException("No two sum solution");
}
}
通过三:一遍HashMap
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
for(int i=0;i<nums.length;i++) {
int complete = target - nums[i];
if(hm.containsKey(complete)) {
return new int[] {hm.get(complete),i};
}
hm.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}
}
通过四
class Solution {
public int[] twoSum(int[] nums, int target) {
int a = 2047;
int[] max = new int[a+1];
for(int i=0;i<nums.length;i++) {
int complete = target - nums[i];
int dex = complete&a;
if(max[dex]!=0) {
return new int[] {max[dex]-1,i};
}
max[nums[i]&a] = i+1;
}
throw new IllegalArgumentException("No two sum solution");
}
}
通过五
class Solution {
int size = 2048;
int[] map = new int[size];
int length = 2047;
int index;
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
index = nums[i] & length;
if (map[index] != 0) {
return new int[]{map[index] - 1, i};
} else {
map[(target - index) & length] = i + 1;
}
}
throw new IllegalArgumentException("No two sum solution");
}
}
通过四改:
按要求扩大数组,就可以适合更大更多范围的数。
class Solution {
public int[] twoSum(int[] nums, int target) {
int a = 4095; //只改这。2047-》4095 0111 1111 1111 -》 1111 1111 1111
int[] max = new int[a+1];
for(int i=0;i<nums.length;i++) {
int complete = target - nums[i];
int dex = complete&a;
if(max[dex]!=0) {
return new int[] {max[dex]-1,i};
}
max[nums[i]&a] = i+1;
}
throw new IllegalArgumentException("No two sum solution");
}
}
不通过。
java.lang.ArrayIndexOutOfBoundsException: Index -3 out of bounds for length 2048
最后执行的输入:
[-3,4,3,90]
0
数据有负数的。
class Solution {
public int[] twoSum(int[] nums, int target) {
int a = 2047;
int[] max = new int[a+1];
for(int i=0;i<nums.length;i++) {
int complete = target - nums[i];
int dex = complete&a;
if(max[dex]!=0) {
return new int[] {max[dex]-1,i};
}
max[nums[i]] = i+1;
}
throw new IllegalArgumentException("No two sum solution");
}
}
不通过
原因:会出现负数 数组越界。Index -2 out of bounds for length 2048
相减后产生了负数。
class Solution {
public int[] twoSum(int[] nums, int target) {
int a = 2047;
int[] max = new int[a+1];
for(int i=0;i<nums.length;i++) {
int complete = target - nums[i];
if(max[complete]!=0) {
return new int[] {max[complete]-1,i};
}
max[complete] = i+1;
}
throw new IllegalArgumentException("No two sum solution");
}
}
不通过。
原因:数组不是排序好的
class Solution {
public int[] twoSum(int[] nums, int target) {
for(int i=nums.length-1; ;i--) {
for(int j=0;j<i;j++) {
if(nums[i]+nums[j] == target) {
return new int[] {j,i};
}
if(nums[i]+nums[j] > target) {
break;
}
}
}
}
}