本题为LeetCode第1道题,为简单题
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。
你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。
示例:
1 | 给定 nums = [2, 7, 11, 15], target = 9 |
1 | /** |
大佬的解法:
1 | public static int[] twoSum_2(int[] nums,int target){ |
其实这个思路我之前也是有的,用结果循环减去数组内的值,判断数组内是否有差值,有的话不就直接找到了吗,不过,还是博主的眼界太低,只想着那几种找法,比如二分法….,转念一想,二分法需要数组有序,于是便打消了这个思路,转念用了暴力解法,还是太笨了。