关闭

1.two sum

229人阅读 评论(0) 收藏 举报

1. Two Sum

  • Total Accepted: 282086
  • Total Submissions: 1088812
  • Difficulty: Easy

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].

UPDATE (2016/2/13):
The return format had been changed to zero-based indices. Please read the above updated description carefully.


我的提交:

public 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+1;
                    a[1]=j+1;
                }
            }
        }
        return a;
    }
}


0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

【LeetCode编程学习(C语言)】1.Two Sum

LeetCode编程之简单篇,两个数求和,采用穷举和hash对比
  • yake827
  • yake827
  • 2016-03-28 13:39
  • 4280

LeetCode题解--1. Two Sum(和为S的两个数字)

题目地址https://leetcode.com/problems/two-sum/ 描述给定一个整数数组,找出其中两个数满足相加等于你指定的目标数字。要求:这个函数twoSum必须要返回能够相加等于...
  • gatieme
  • gatieme
  • 2016-01-27 22:32
  • 3882

【LeetCode-面试算法经典-Java实现】【001-Two Sum(求两个数的和)】

给定一个整数数组,找出其中两个数满足相加等于你指定的目标数字。要求:这个函数twoSum必须要返回能够相加等于目标数字的两个数的索引,且index1必须要小于index2。请注意一点,你返回的结果(包...
  • DERRANTCM
  • DERRANTCM
  • 2015-07-16 06:39
  • 7421

【LeetCode编程学习(C语言)】1.Two Sum

LeetCode编程之简单篇,两个数求和,采用穷举和hash对比
  • yake827
  • yake827
  • 2016-03-28 13:39
  • 4280

LeetCode Two Sum时间复杂度O(n)解法尝试版本1

题目如下: Given an array of integers, return indices of the two numbers such that they add up to a ...
  • Hello_World_LVLcoder
  • Hello_World_LVLcoder
  • 2017-11-17 20:23
  • 60

LeetCode 1. Two Sum--数组中两元素相加为该数值,输出对应的两个索引

Given an array of integers, return indices of the two numbers such that they add up to a specific ta...
  • u010002184
  • u010002184
  • 2017-08-05 15:48
  • 594

[C++]LeetCode 1: Two Sum(无序数组求指定和)

Problem: Given an array of integers, find two numbers such that they add up to a specific target num...
  • emmaalways
  • emmaalways
  • 2015-04-28 20:16
  • 657

明明的leetcode日常:1. Two Sum

leetcode001:two sum
  • sallyxyl1993
  • sallyxyl1993
  • 2017-06-10 16:28
  • 144

LeetCode小白菜笔记[1]:Two Sum

LeetCode小白菜笔记[1]:Two SumTwo Sum [Easy]
  • edogawachia
  • edogawachia
  • 2017-12-09 18:22
  • 30

leetcode 刷题题解(c++) 1.Two Sum (hash表,排序+二分查找)

c++刷leetcode,有题目和算法代码,可直接在leetcode提交通过。
  • GOGO_YAO
  • GOGO_YAO
  • 2016-07-24 20:59
  • 542
    个人资料
    • 访问:71029次
    • 积分:1595
    • 等级:
    • 排名:千里之外
    • 原创:78篇
    • 转载:77篇
    • 译文:0篇
    • 评论:6条