题目:Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

 class Solution
{
public:
vector<int> twoSum(vector<int> &numbers, int target)
{
vector<int> ret(, -);
map<int, int> m;
for (int i = ; i < numbers.size(); i++)
{
if (m.find(target - numbers[i]) == m.end())
m[numbers[i]] = i;
else
{
ret[] = m[target - numbers[i]] + ;
ret[] = i + ;
return ret;
} }
}
};

  Java版本:

public class Solution
{
public int[] twoSum(int[] nums, int target)
{
int reslut[]=new int[2];
boolean tag=false;
for(int i=0;i<nums.length-1;i++)
{
int j=i+1;
while(j<nums.length)
{
if(nums[i]+nums[j]==target)
{
reslut[0]=i+1;
reslut[1]=j+1;
tag=true;
break;
}
else
{
j++;
}
}
if(tag)
break;
}
return reslut;
}
}

【LeetCode OJ】Two Sum的更多相关文章

  1. 【LeetCode OJ】Path Sum II

    Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...

  2. 【LeetCode OJ】Path Sum

    Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...

  3. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  4. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  5. 【LeetCode OJ】Binary Tree Maximum Path Sum

    Problem Link: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ For any path P in a bina ...

  6. 【LeetCode OJ】Sum Root to Leaf Numbers

    # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...

  7. 【LeetCode OJ】Triangle

    Problem Link: http://oj.leetcode.com/problems/triangle/ Let R[][] be a 2D array where R[i][j] (j < ...

  8. 【LeetCode OJ】Gas Station

    Problem link: http://oj.leetcode.com/problems/gas-station/ We can solve this problem by following al ...

  9. 【LEETCODE OJ】Candy

    Problem link: http://oj.leetcode.com/problems/candy/ Suppose we are given an array R[1..N] that are ...

随机推荐

  1. Assets/FollowDestination.cs(6,13): error CS0246: The type or namespace name `NavMeshAgent' could not be found. Are you missing `UnityEngine.AI' using directive?的解决方案

    问题的出现与描述 在Unity中创建一个NPC,使它一直跟踪一个目标Destination,C#脚本代码如下,错误信息描述如下 using System.Collections; using Syst ...

  2. Linux操作系统的安装

    一.介绍 目的:通过本文了解并掌握Linux系统安装的过程 软件环境 Linux系统:CentOS7.3 虚拟机:VM12 主机系统:Windows8.0 二.安装虚拟机 首先,需要下载VMware ...

  3. oc总结 --oc基础语法相关知识

    m是OC源文件扩展名,入口点也是main函数,第一个OC程序: #import <Foundation/Foundation.h> int main(int argc, const cha ...

  4. c# WebBrowser获取cookie

    private void BtnOpenUrl_Click(object sender, EventArgs e) { if (txtUrl.Text != "") { Myweb ...

  5. 通配符的匹配很全面, 但无法找到元素 'context:property-placeholder'

    解决方案就是如下: xmlns:context="http://www.springframework.org/schema/context" 同时在xsi:schemaLocat ...

  6. Linq“条件排序”

    StockQuantities.OrderBy(u=>u.Status==null) 该排序先排结果为0(false)的,再排结果为1(true)的 使用场景: 一个对象有上传时间(可以为空)和 ...

  7. Windows的VNC客户端连接Linux无法复制粘贴

    问题描述 在Windows里使用VNC客户端远程桌面连接Linux,Linux里的文字信息复制之后无法粘贴到Windows中 解决办法 在Linux中执行命令 vncconfig -nowin& ...

  8. pycharm环境下:同文件夹下文件(.py)之间的调用,出现红线问题

    只要将pycharm下打开项目后: 将你运行文件(.py)的项目设置为根目录,就不会出现红色线:

  9. OpenGL 遮挡查询

    原文地址:http://www.linuxidc.com/Linux/2015-02/114036.htm 在一个场景中,如果有有些物体被其他物体遮住了不可见.那么我们就不需要绘制它.在复杂的场景中, ...

  10. 大数据学习笔记01-HDFS-集群安装

    安装 下载 Hadoop,以2.7.5版本为例 在虚拟机上创建目录bigdata,即执行mkdir bigdata 上传到master机器节点的目录~/bigdata下(可以用FileZilla等ft ...