题目描述:
输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s。如果有多对数字的和等于s,则输出任意一对即可。
示例 1:
输入:nums = [2,7,11,15], target = 9
输出:[2,7] 或者 [7,2]
示例 2:
输入:nums = [10,26,30,31,47,60], target = 40
输出:[10,30] 或者 [30,10]
限制:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^6
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/he-wei-sde-liang-ge-shu-zi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
我的解:
/*
输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s。如果有多对数字的和等于s,则输出任意一对即可。
*/
public class LeetCood {
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
if(nums.length != 2) {
for (int i = 0; i != nums.length; i++) {//经典的双循环穷举
for (int j = 1; j != nums.length - 1; j++) {
if (nums[i] + nums[j] == target) {
result = new int[]{nums[i], nums[j]};
}
}
}
}else {
if (nums[0] + nums[1] == target)
result = new int[]{nums[0], nums[1]};
}
return result;
}
public static void main(String[] args){
LeetCood test = new LeetCood();
int[] myTest = new int[]{10,26,30,31,47,60};
System.out.println(myTest[0] + " " + myTest[1]);
int[] out = test.twoSum(myTest, 40);
System.out.println(out[0] +" "+ out[1]);
}
}
嘻嘻,超时啦。
正确的解法可以用双指针(二分)或者是Set。
双指针:
public int[] twoSunSolution(int[] nums, int target){
int l = 0; // 最左的数
int r = nums.length - 1; // 最右的数
int[] result = new int[2]; // 新建一个整型数组
while(r > l){ // 右边比左边大的时候正常进入循环
if (nums[r] + nums[l] != target){// 不相等就进入判断
if(nums[l] + nums[r] > target){// 判断如果两数之和大于target则说明是太大了,需要少一点
r--;//所以少一点 则减减
}else {//如果两数之和小于target 则说明需要大一点
l++;//所以大一点 则加加
}
}else {
result = new int[]{nums[l], nums[r]}; // 将数放进result内返回
break;
}
}
return result;
}
用Set:
public int[] twoSunSolutionTwo(int[] nums, int target){
HashSet<Integer> set = new HashSet<>();
int[] result = new int[2];
for(int num : nums){//增强for循环遍历
if (!set.contains(target - num)){// 将目标与当前选到的数相减,并用contains函数确认是否在set集合中
set.add(num);//不在 则将当前数放进集合
}else {
result = new int[]{num, target-num};//在则返回
}
}
return result;
}
新建一个int的Set集合:
HashSet<Integer> set = new HashSet<>();
增强for循环:
for(object agrs : args)
Set集合的增加和判断是否存在:
类名.contains();
类名.arr();
我们可以看到用穷举无论是空间开销还是时间开销都十分喜人(bushi嘻嘻。
而双指针则在时间开销上表现喜人,而Set集合在空间开销上表现十分喜人,我想在实际应用下还需要分场合选择合适的算法。
import java.util.HashSet;
/*
输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s。如果有多对数字的和等于s,则输出任意一对即可。
*/
public class LeetCood {
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
if(nums.length != 2) {
for (int i = 0; i != nums.length; i++) {
for (int j = 1; j != nums.length - 1; j++) {
if (nums[i] + nums[j] == target) {
result = new int[]{nums[i], nums[j]};
}
}
}
}else {
if (nums[0] + nums[1] == target)
result = new int[]{nums[0], nums[1]};
}
return result;
}
/*
二分法或者说双指针
思路,从左右两边向中间靠拢但是要设定一个条件,因为该题目是递增序列
*/
public int[] twoSunSolution(int[] nums, int target){
int l = 0; // 最左的数
int r = nums.length - 1; // 最右的数
int[] result = new int[2]; // 新建一个整型数组
while(r > l){ // 右边比左边大的时候正常进入循环
if (nums[r] + nums[l] != target){// 不相等就进入判断
if(nums[l] + nums[r] > target){// 判断如果两数之和大于target则说明是太大了,需要少一点
r--;//所以少一点 则减减
}else {//如果两数之和小于target 则说明需要大一点
l++;//所以大一点 则加加
}
}else {
result = new int[]{nums[l], nums[r]}; // 将数放进result内返回
break;
}
}
return result;
}
/*
用Set集合方法
*/
public int[] twoSunSolutionTwo(int[] nums, int target){
HashSet<Integer> set = new HashSet<>();
int[] result = new int[2];
for(int num : nums){//增强for循环遍历
if (!set.contains(target - num)){// 将目标与当前选到的数相减,并用contains函数确认是否在set集合中
set.add(num);//不在 则将当前数放进集合
}else {
result = new int[]{num, target-num};//在则返回
}
}
return result;
}
public static void main(String[] args){
LeetCood test = new LeetCood();
int[] myTest = new int[]{10,26,30,31,47,60};
System.out.println(myTest[0] + " " + myTest[1]);
int[] outOne = test.twoSum(myTest, 40);
System.out.println(outOne[0] +" "+ outOne[1]);
int[] outTwo = test.twoSunSolution(myTest, 40);
System.out.println(outTwo[0] +" "+ outTwo[1]);
int[] outThree = test.twoSunSolutionTwo(myTest, 40);
System.out.println(outThree[0] +" "+ outThree[1]);
}
}