Two Sum
2017-02-28 16:33
141 查看
问题描述:
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, and you may not use the same element twice.
Example:
代码:
public class Solution {
public int[] twoSum(int[] nums, int target) {
int i,j,count=0;
int[] sum=new int[2];
for(i=0;i<nums.length-1;i++){
for(j=i+1;j<nums.length;j++){
if(nums[i]+nums[j]==target){
sum[count]=i;
count++;
sum[count]=j;
break;
}
}
}
return sum;
}
}
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, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
代码:
public class Solution {
public int[] twoSum(int[] nums, int target) {
int i,j,count=0;
int[] sum=new int[2];
for(i=0;i<nums.length-1;i++){
for(j=i+1;j<nums.length;j++){
if(nums[i]+nums[j]==target){
sum[count]=i;
count++;
sum[count]=j;
break;
}
}
}
return sum;
}
}
相关文章推荐
- [LeetCode] Two Sum
- leetcode -- Two Sum
- LeetCode-Two Sum
- Two Sum 数组中找两个之和为sum@LeetCode
- LeetCode:Two Sum
- LeetCode 001 Two Sum
- LeetCode Two Sum
- 【LeetCode】Two Sum
- Two Sum
- Leetcode 线性表 Two Sum
- leetcode之Two Sum
- 【LeetCode with Python】 Two Sum
- LeetCode 之 two Sum寻找两个相加之和为给定值的两个数
- [LeetCode]Two Sum
- Two Sum[leetcode]
- Leetcode-Two Sum
- [LeetCode]Two Sum 【Vector全局指针的使用】
- LeetCode[Hash Table]: Two Sum
- 和大神们学习每天一题(leetcode)-Two Sum
- 1 Two Sum