题目: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. 用OpenGL实现跳跃的立体小球

    一.目的 掌握OpenGL中显示列表对象的使用方法. 二.示例代码 Github地址 #include "stdafx.h" #include <GL/glut.h> ...

  2. elasticsearch系列五:搜索详解(查询建议介绍、Suggester 介绍)

    一.查询建议介绍 1. 查询建议是什么? 查询建议,为用户提供良好的使用体验.主要包括: 拼写检查: 自动建议查询词(自动补全) 拼写检查如图: 自动建议查询词(自动补全): 2. ES中查询建议的A ...

  3. unity-----------------------四元数与欧拉旋转方法

    转:http://blog.csdn.net/treepulse/article/details/49281295 Transfrom.eulerAngles public float yRotati ...

  4. linq join 左连接 leftjoin 多个on条件 where 条件

    var haveChange = from newScore in newScoreList join oldScore in oldScoreList on new{newScore.ExamId, ...

  5. python模块之psutil详解

    一.psutil模块: 1.psutil是一个跨平台库(http://pythonhosted.org/psutil/)能够轻松实现获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等)信息 ...

  6. 如何找回Ucenter创始人密码,账号无需修改

    UCenter 创始人的密码非常重要,忘记或丢失后,就不能进入 UCenter 进行用户和数据的管理,也会对站点造成安全隐患.由于 UCenter 的密码是采用两次 md5 加一个随机数的形式加密的, ...

  7. EJB与JPA的关系

    转自:http://www.cnblogs.com/o-andy-o/archive/2012/04/17/2453537.html JPA是基于Java持久化的解决方案,主要是为了解决ORM框架的差 ...

  8. qt creator如何实现转到槽功能

    ui_mainwindow.h .

  9. VC++ 链接错误LINK : fatal error LNK1104: cannot open file "*.lib"

    问题描述: 运行VC++编译时经常出现 Linking… LINK : fatal error LNK1104: cannot open file “*.lib” Error executing li ...

  10. 前端可视化编程:liveReload安装,sublime 3

    需要插件  sublime text3:View in Browser:LiveReload chrome:liveReload 配置方法 一:sublime text3 sublime 3下载地址: ...