题目地址:
中文:https://leetcode-cn.com/problems/two-sum/
英文:https://leetcode.com/problems/two-sum/
题目描述:
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9 Output: [0,1] Output: Because
nums[0] + nums[1] == 9, we return [0, 1].
思路:
思路1:
简单的暴力循环求解,两层循环求解,见题解思路1。
题解:
//思路1
class Solution {
public int[] twoSum(int[] nums, int target) {
int len = nums.length;
int b[] = new int[2];
for(int i=0;i<len;i++){
for(int j=i+1;j<len;j++){
if(nums[i]==target - nums[j]) {
b[0] = i;
b[1] = j;
break;
}
}
}
return b;
}
}