【LeetCode OJ】Two Sum
题目: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的更多相关文章
- 【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 ...
- 【LeetCode OJ】Path Sum
Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- 【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 ...
- 【LeetCode OJ】Sum Root to Leaf Numbers
# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...
- 【LeetCode OJ】Triangle
Problem Link: http://oj.leetcode.com/problems/triangle/ Let R[][] be a 2D array where R[i][j] (j < ...
- 【LeetCode OJ】Gas Station
Problem link: http://oj.leetcode.com/problems/gas-station/ We can solve this problem by following al ...
- 【LEETCODE OJ】Candy
Problem link: http://oj.leetcode.com/problems/candy/ Suppose we are given an array R[1..N] that are ...
随机推荐
- e809. 在菜单中使菜单项分开
A separator typically appears as a horizontal line. It is used to group related sets of menu items i ...
- 嵌入式开发之hi3519---i2c EEPROM
http://pdf1.alldatasheetcn.com/datasheet-pdf/view/163283/MICROCHIP/24LC024.html http://www.elecfans. ...
- Git -- 工作区 和 暂存区
Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 先来看名词解释. 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工 ...
- C# 获取web.config配置文件
.ConfigurationManager提供对客户端应用程序配置文件的访问. 其有两个属性1.ConnectionStrings 获取当前应用程序默认配置的 ConnectionStringsSec ...
- WiFidog 广告路由可修改功能更加智能化的几点看法
海蜘蛛Tomato出了mini版,这个对很多做WiFi营销的朋友来说,是一个福音,因为可以直接从FIR302B,一台30多块钱的路由直接刷成Hi-WiFi,而且界面这么漂亮 相信很多人已经对此界面OE ...
- 多目标线性规划求解方法及matlab实现
转载: https://blog.csdn.net/wzl1997/article/details/79120323
- unity3d 获取游戏对象详解
原文地址:http://www.xuanyusong.com/archives/2768 我觉得Unity里面的Transform 和 GameObject就像两个双胞胎兄弟一样,这俩哥们很要好,我能 ...
- 查看WEB服务器的连接数
查看WEB服务器的连接数 https://technet.microsoft.com/en-us/sysinternals/bb897437 tcpView
- 配置防盗链 访问控制Directory 访问控制FilesMatch
- Macbook pro安装MacOS系统
在app store 下载系统sierra; 打开磁盘工具,选择优盘,抹掉: 日志式,GUID分区: http://www.cnblogs.com/xiaobo-Linux/ 终端输入命令, sudo ...