题目原文:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
题目大意:
给出一个数组和一个目标值target,寻找两个下标i和j,使得nums[i]+nums[j]=target。
题目分析:
这道题先无脑水过去。复杂度O(n2)
源码:(language:java)
public class Solution {
public int[] twoSum(int[] nums, int target) {
for(int i = 0;i<nums.length;i++) {
for(int j=i+1;j<nums.length;j++) {
if(nums[i]+nums[j]==target)
return new int[]{i,j};
}
}
return new int[]{-1,-1};
}
}
成绩:
42ms,beats 19.97%,众数6ms,35.54%
Cmershen的碎碎念:
这道题不知道何时被改成Easy了,其实可以用HashMap做,应该能在线性时间解决。