代码如下:
public class TwoSum {
public static int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> record = new HashMap<>(16);
for(int i = 0; i<nums.length;i++){
if(record.get(target - nums[i]) != null){
return new int[]{record.get(target - nums[i]),i};
}
record.put(nums[i],i);
}
return null;
}
public static void main(String[] args) {
int[] nums = {2, 7, 11, 15};
int target = 9;
System.out.println(Arrays.toString(twoSum(nums, target)));
}
}
更多:
一到三万元的小本生意 https://zhuanlan.zhihu.com/p/157978996
没人注意的暴利行业 https://zhuanlan.zhihu.com/p/157835002
月入2万的10个小生意 https://zhuanlan.zhihu.com/p/149634897
不起眼的暴利小生意 https://zhuanlan.zhihu.com/p/148417881