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].
个人代码数组
public int[] twoSum(int[] nums, int target) { //找出最值肯定堆大小 int min = nums[0], max = nums[0]; for (int i = 0; i < nums.length; i++) { if (nums[i] > max) { max = nums[i]; } else if (nums[i] < min) { min = nums[i]; } } String heap[] = new String[max - min + 1]; for (int i = 0; i < heap.length; i++) { heap[i] = ""; } //遍历nums , 初始化堆 存放nums下标i for (int i = 0; i < nums.length; i++) { if (heap[nums[i] - min] == "") { heap[nums[i] - min] = "" + i; } else { heap[nums[i] - min] = heap[nums[i] - min] + "," + i; } } int result[] = new int[2]; //遍历堆 for (int i = 0; i < heap.length; i++) { if (heap[i] != "" && (target - i - 2 * min) < heap.length && heap[target - i - 2 * min] != "") { if (heap[i].contains(",")) { String[] split = heap[i].split(","); result[0] = Integer.valueOf(split[0]).intValue(); result[1] = Integer.valueOf(split[1]).intValue(); break; } else { result[0] = Integer.valueOf(heap[i]).intValue(); result[1] = Integer.valueOf(heap[target - i - 2 * min]).intValue(); break; } } } return result; }
初始化一个堆,好比数组a【1,2,7,11】找9, 那么会初始化一个11长度的数组b,对于输入数组12711, 标记 b对应下标为127 11的值分别为0,1,2,3(即在a数组中的下标)。 而后遍历b, 只要一个数和他相对于9的差数都被标记过,好比2,7直接返回两个下标便可。
调试过程当中发现了重数{3,3},负数等状况, 作了一些调整。
复杂度: 时间O(n) 空间O(n)测试
9ms 优于75% (cn站成绩是8ms,优于92% 说明总站可能样本什么更全一些,故之后都在总站提交)调试
二次循环hash表,解决暴力方法中过多遍历的问题,而且节省大量空间。
第一遍初始化hash表,
第二遍匹配。code
经过2,能够看见能够同时初始化与匹配,匹配中即返回,不然继续初始化。
一次遍历便可。ci
2.3的代码见官网https://leetcode.com/problems/two-sum/solution/element
3理论上已是最优的了,耗时6ms,可是我发现官网居然还有更吊的leetcode
class Solution { public int[] twoSum(int[] nums, int target) { int t=4096; int bitMode=t-1; int[] temp=new int[t]; int length=nums.length; int firstValue=nums[0]; for(int i=1;i<length;i++){ int different=target-nums[i]; int current=nums[i]; if(different==firstValue) { return new int[] {0,i}; } int differentIndex=temp[different&bitMode]; if(differentIndex!=0){ return new int[] {differentIndex,i}; } int currentIndex=current&bitMode; temp[currentIndex]=i; } return null; } }
这个使用了位与来模拟,
可是我使用该数据测试,发现该解法是错的。
int nums[] = {-1,0,1,2, 4097, 8193};
int target = 8194;get