1. 两数之和

1.两数之和

📆Date: 2023年3月13日

✒️Author: SmartBoy

📓Classify:LeetCode

🔖Language: Java

✨题目描述:

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

你可以按任意顺序返回答案。

示例 1:

输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。

示例 2:

输入:nums = [3,2,4], target = 6
输出:[1,2]

示例 3:

输入:nums = [3,3], target = 6
输出:[0,1]

提示:

  • 2 <= nums.length <= 104
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • 只会存在一个有效答案

**进阶:**你可以想出一个时间复杂度小于 O(n2) 的算法吗?

🎉解题思路:

可以选择暴力穷举的方法或HashMap检索的方法

详细见代码

🥇Java源码:

package LeetCodePro.Problem3;

import java.util.Arrays;
import java.util.Scanner;

/**
* @Auther:Xust_SmartBoy
* @Date:2023/3/13 20:39
* @Description:LeetCodePro.Problem3
* @User:SmartBoy
* @VERSON:
* 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标。
*
* 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
*
* 你可以按任意顺序返回答案。
*
*
*
* 示例 1:
*
* 输入:nums = [2,7,11,15], target = 9
* 输出:[0,1]
* 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
* 示例 2:
*
* 输入:nums = [3,2,4], target = 6
* 输出:[1,2]
* 示例 3:
*
* 输入:nums = [3,3], target = 6
* 输出:[0,1]
*
*
* 提示:
*
* 2 <= nums.length <= 104
* -109 <= nums[i] <= 109
* -109 <= target <= 109
* 只会存在一个有效答案
*
*
* 进阶:你可以想出一个时间复杂度小于 O(n2) 的算法吗?
*/

public class Problem3 {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.println("请输入数组的长度:");
       int n = scanner.nextInt();
       System.out.println("请输入一个整数数组:");
       int[] nums = new int[n];
       for (int i = 0; i < n; i++) {
           nums[i] = scanner.nextInt();
       }
       System.out.println("请输入目标值:");
       int target = scanner.nextInt();
       Solution1 solution1 = new Solution1();
       Solution2 solution2 = new Solution2();
       Solution3 solution3 = new Solution3();
       int[] result1 = solution1.twoSum(nums,target);
       int[] result2 = solution2.twoSum(nums,target);
       int[] result3 = solution3.twoSum(nums,target);

       System.out.println(Arrays.toString(result3));
   }
}

package LeetCodePro.Problem3;

/**
* @Auther:Xust_SmartBoy
* @Date:2023/3/13 19:17
* @Description:LeetCodePro.Problem1
* @User:SmartBoy
* @VERSON:暴力穷举
*/

public class Solution1 {
   public int[] twoSum(int[] nums, int target) {
       int[] result = new int[2];
       for (int i = 0; i < nums.length; i++) {
           for (int j = i + 1; j < nums.length; j++) {
               if (nums[i] + nums[j] == target){
                   result[0] = i;
                   result[1] = j;
               }
           }
       }
       return result;
   }
}

package LeetCodePro.Problem3;

import java.util.HashMap;
import java.util.Map;

/**
* @Auther:Xust_SmartBoy
* @Date:2023/3/13 19:33
* @Description:LeetCodePro.Problem1
* @User:SmartBoy
* @VERSON:HashMap检索
*/

public class Solution2 {
   public int[] twoSum(int[] nums, int target) {
       /*key 为元素值,value 为每个元素对应的下标*/
       Map<Integer,Integer> map = new HashMap<>();
       int[] result = new int[2]; // 定义返回数组
       for (int i = 0; i < nums.length; i++) {
           int another = target - nums[i]; // 用another存储目标值与所指数组值的插值
           Integer anotherIndex = map.get(another); // 得到插值的HashMap的key
           if (anotherIndex != null){ // key位置的元素已存在
               result[0] = anotherIndex;
               result[1] = i;
               break;
           }
           else
               map.put(nums[i],i); // key位置元素为空,将数组i位置元素值存入key,下标存为value
       }
       return result;
   }

}

package LeetCodePro.Problem3;

import java.util.HashMap;
import java.util.Map;

/**
* @Auther:Xust_SmartBoy
* @Date:2023/3/13 19:38
* @Description:LeetCodePro.Problem1
* @User:SmartBoy
* @VERSON:
*/

public class Solution3 {
   public int[] twoSum(int[] nums, int target) {
       Map<Integer,Integer> map = new HashMap<>();
       int[] result = new int[2];
       for (int i = 0; i < nums.length; i++) {
           if (map.containsKey(target - nums[i])){
               result[0] = map.get(target - nums[i]);
               result[1] = i;
           }
           map.put(nums[i],i);
       }
       return result;
   }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用以下代码实现: #include <stdio.h> int main() { int a, b, diff; printf("请输入两个整数:\n"); scanf("%d %d", &a, &b); diff = a - b; printf("两数之差为:%d\n", diff); return 0; } ### 回答2: 要用C语言输入两个数并求两数之差,可以使用以下代码实现: ```c #include <stdio.h> int main() { int num1, num2, difference; printf("请输入第一个数:"); scanf("%d", &num1); printf("请输入第二个数:"); scanf("%d", &num2); difference = num1 - num2; printf("两数之差为:%d\n", difference); return 0; } ``` 首先,我们声明了三个变量`num1`、`num2`和`difference`,分别用来存储输入的两个数和它们的差。 然后,通过`printf`函数提示用户输入第一个数,并通过`scanf`函数将用户输入的值存储到`num1`变量中。接着,再次使用`printf`和`scanf`函数获取并存储第二个数到`num2`变量中。 然后,通过`difference = num1 - num2;`计算两数之差,并将结果存储到`difference`变量中。 最后,使用`printf`函数输出计算得到的差值。 以上就是用C语言输入两个数并求两数之差的实现代码,希望能帮到你。 ### 回答3: 用C语言求两个数的差可以通过以下步骤完成: 1. 首先,需要在程序中引入C语言的输入输出库文件,头文件为<stdio.h>。这样才能使用scanf和printf函数。 2. 在程序的主函数中定义两个变量,用于存储输入的两个数。可以使用int类型来定义这两个变量,如int num1, num2;。 3. 使用scanf函数接收用户的输入。可以通过scanf("%d %d", &num1, &num2);来依次接收并保存用户输入的两个数。 4. 使用一个变量来保存两个数之差,比如定义一个int类型的变量diff。通过计算num1 - num2来得到两个数的差,并将其赋值给diff。 5. 最后,使用printf函数将两个数的差输出到屏幕上。可以使用printf("两数之差为:%d\n", diff);来输出差值。 整个程序的代码如下所示: ```c #include<stdio.h> int main() { int num1, num2, diff; printf("请输入两个数:\n"); scanf("%d %d", &num1, &num2); diff = num1 - num2; printf("两数之差为:%d\n", diff); return 0; } ``` 以上就是用C语言求两个数之差的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值