LeetCode 1. Two Sum
1. Two Sum
Question
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 sameelement twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
Solution
- /**
- * @ClassName Solution
- * @Description TODO
- * @Author wangwenjie
- * @Date 2019/1/23 14:02
- **/
- public class Solution {
- public int[] twoSum(int[] nums, int target) {
- int [] res = new int[2];
- int num = nums.length;
- for (int i=0;i<num-1;i++){
- for (int j = i+1;j<num;j++){
- if (nums[i]+nums[j]== target){
- res[0] = i;
- res[1] = j;
- break;
- }
- }
- }
- return res;
- }
- }
posted on 2019-01-30 09:16 WenjieWangFlyToWorld 阅读(98) 评论(0) 编辑 收藏 举报