leetCode1

给定一个整数数组 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 }
原文地址:https://www.cnblogs.com/bigdata-stone/p/11443780.html