【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 ...
随机推荐
- 【转】【VS Code】配置文件Launch及快捷键
Ctrl+shift+p,然后输入launch,点击第一个选项即可配置. 之后选择More即可 具体配置可修改为: { "version": "0.2.0", ...
- 设置 sqlserver Profiler 只监控 EF的sql执行请求
当我们用EF执行语句的时候,可以使用 sqlserver Profiler来监控到底执行了哪些sql语句,但是默认他是监控全局的,我们只想监控Ef的语句,这里如下设置 这样就只会监控 EF产生的 sq ...
- Erlang HTTP client:ibrowse
ibrowse: https://github.com/cmullaparthi/ibrowse
- python调用ansible接口API执行命令
python版本:Python 2.6.6 ansible版本:ansible 2.3.1.0 下载地址:https://releases.ansible.com/ansible/ 调用脚本 ...
- css 阻止元素中的文本。双击选中
//firefox -moz-user-select: none; //chrome.safari -webkit-user-select: none; //ie -ms-user-select: n ...
- TensorFlow:tf.contrib.layers.xavier_initializer
xavier_initializer( uniform=True, seed=None, dtype=tf.float32 ) 该函数返回一个用于初始化权重的初始化程序 “Xavier” .这个初始化 ...
- linux任务计划 chkconfig工具 systemd管理服务 unit介绍 target介绍
linux任务计划 任务计划:特定时间备份数据,重启服务,shell脚本,单独的命令等等. 任务计划配置文件:cat /etc/crontab [root@centos7 ~]# cat /etc/c ...
- Android 知识梳理
说明:本篇博客只是一个知识整理,因为网上对于Android的知识介绍足够多,因此我不再写相关文章(主要是因为我写的不如人家好),所以所有文章均来自网络,不贴原文章,只提供连接,因此本文旨在减少你对相关 ...
- Link1123:转换到COFF期间失败:文件无效或损坏
当在编译VS项目时,出现如下错误: 这个错误,表明在连接阶段出错.COFF为Common Object File Format,通用对象文件格式,它的出现为混合语言编程带来方便 ...
- [译]Angular-ui 之 多命名视图(Multiple Named Views)
◄上一篇 (Nested States & Nested Views) 下一篇 (URL Routing) ► 你可以给你的视图命名,以便可以在单个模版当中拥有一个以上的 ui-vie ...