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

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

题解:这个题目直接用暴力遍历就可以,代码如下:

 1 class Solution {
 2     public int[] twoSum(int[] nums, int target) {
 3         int[] res = new int[2];
 4         for(int i =0 ; i<nums.length;i++){
 5             for(int j = i+1;j<nums.length;j++){
 6                 if(nums[i]+nums[j]==target){
 7                     res[0]=i;
 8                     res[1]=j;
 9                 }
10             }
11         }
12         return res;
13     }
14 }

 

相关文章:

  • 2021-07-09
  • 2021-09-25
  • 2021-12-03
  • 2021-11-07
  • 2021-12-20
  • 2021-10-29
  • 2022-12-23
猜你喜欢
  • 2021-04-07
  • 2021-05-19
  • 2022-12-23
  • 2021-11-09
  • 2021-05-15
  • 2021-10-07
相关资源
相似解决方案