1. Two Sum

原创 2018年04月17日 13:42:17

原题链接

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

翻译:给定一个数组,和一个目标值,在原数组中找出两个值相加为目标值,答案唯一。

思路:两重循环暴力过。
AC代码:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> res;
        for(int i=0;i<nums.size();i++)
        {
            for(int j=i+1;j<nums.size();j++)
            {
                if(nums[i]+nums[j]==target)
                {
                    res.push_back(i);
                    res.push_back(j);
                    break;
                }
            }
        }
        return res;
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/rockywallet/article/details/79973658

leetcode (1) Two Sum js代码实现

Given an array of integers, find two numbers such that they add up to a specific target number. The...
  • sinat_27350883
  • sinat_27350883
  • 2015-12-19 21:22:48
  • 1085

1. Two Sum Ruby

Given an array of integers, find two numbers such that they add up to a specific target number. T...
  • hyperbolechi
  • hyperbolechi
  • 2015-06-18 10:01:51
  • 1265

LeetCode- 1. Two Sum - 思路详解-C++

给定义个数组,找到数组中的两个值之和为目标值,返回这两个数的下标,假设每一个数组都有一组特定的解。...
  • SJWL2012
  • SJWL2012
  • 2017-01-11 15:50:52
  • 150

leetcode【1+167 Two Sum 系列】【python】

1 Two Sum找到给定序列中两个数字的和是指定target,返回的是个list,包含两个数的index,从0开始。第一反应肯定是遍历,毕竟是数组题,遍历需要两遍,才能找到和,那么肯定是要优化的了。...
  • u014251967
  • u014251967
  • 2016-09-01 19:31:36
  • 457

1. Two Sum [easy] (Python)

题目链接https://leetcode.com/problems/two-sum/题目原文 Given an array of integers, return indices of the t...
  • coder_orz
  • coder_orz
  • 2016-07-26 20:19:37
  • 2361

1. Two Sum(重要!)

Given an array of integers, return indices of the two numbers such that they add up to a specific ta...
  • gao1440156051
  • gao1440156051
  • 2016-07-09 16:21:27
  • 1152

LeetCode #1 Two Sum C# Solution

LeetCode #1 Problem Given an array of integers, return indices of the two numbers such that they ...
  • daypig
  • daypig
  • 2016-04-03 14:00:12
  • 760

LeetCode 1 Two Sum 题解

Problem: Given an array of integers, find two numbers such that they add up to a specific target nu...
  • runningtortoises
  • runningtortoises
  • 2015-05-05 16:53:05
  • 1313

[LeetCode]1 Two Sum(C++,Python实现)

LeetCode OJ的第一题,题目描述如下:
  • Shiroh_ms08
  • Shiroh_ms08
  • 2014-05-16 21:17:33
  • 2200

【LeetCode】LeetCode——第1题:Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number. The ...
  • hujingshuang
  • hujingshuang
  • 2016-01-04 17:01:58
  • 2617
收藏助手
不良信息举报
您举报文章:1. Two Sum
举报原因:
原因补充:

(最多只允许输入30个字)