leetcode_1. Two Sum
2018-03-06 21:24
387 查看
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].
解题代码:class Solution {
public int[] twoSum(int[] nums, int target) {
int [] a= new int[2];
for ( int i =0 ;i < nums.length; i++){
for (int j=i+1; j<nums.length; j++){
if (nums[i]+nums[j]==target){
a[0]=i;
a[1]=j;
break;
}
}
}
return a;
}
}
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
解题代码:class Solution {
public int[] twoSum(int[] nums, int target) {
int [] a= new int[2];
for ( int i =0 ;i < nums.length; i++){
for (int j=i+1; j<nums.length; j++){
if (nums[i]+nums[j]==target){
a[0]=i;
a[1]=j;
break;
}
}
}
return a;
}
}
相关文章推荐
- LeetCode 001 Two Sum
- leetcode 1. Two Sum
- leetcode_Two Sum
- leetcode--1.Two Sum
- 明明的leetcode日常:1. Two Sum
- [LeetCode] Two Sum
- [LeetCode] Two Sum
- LeetCode1 Two Sum
- [Leetcode]1 Two Sum
- LeetCode-Array-1. Two Sum
- 【闲来刷 leetcode】 Two Sum | leetcode OJ
- LeetCode 1_Two Sum
- LeetCode-1-Two Sum-E
- [LeetCode][Java] Two Sum
- LeetCode 1.Two Sum
- LeetCode-two sum:python解答数组问题
- 【LeetCode-1】Two Sum
- 【leetcode】Two Sum
- Leetcode习题: Two Sum
- leetcode第1题 ,Two Sum