TwoSum leetcode
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target)
{
vector<int> twoSum1(2);
map<int,int> mValueIdex;
map<int,int>::iterator it;
bool flag=0;
for(int i=0;i<nums.size();i++)
{
//两个一样.
if(nums[i]==target/2)
{
if(flag==0)
{
mValueIdex[nums[i]]=i;
flag=1;
}
else
{
twoSum1[0]=mValueIdex[nums[i]]+1;
twoSum1[1]=i+1;
return twoSum1; //0 0 1)?
}
}else
{
mValueIdex[nums[i]]=i;
it=mValueIdex.find(target-nums[i]);
if(it!=mValueIdex.end())
{
twoSum1[1]=mValueIdex[nums[i]]+1;//i是最后出现的
twoSum1[0]=mValueIdex[target-nums[i]]+1;
return twoSum1; //0 0 1)?
}
}
}
}
};
TwoSum leetcode的更多相关文章
- LeetCode初体验—twoSum
今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...
- leetcode — two-sum
package org.lep.leetcode.twosum; import java.util.Arrays; import java.util.HashMap; import java.util ...
- leetCode:twoSum 两数之和 【JAVA实现】
LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:t ...
- [leetcode]TwoSum系列问题
1.普通数组找两个数,哈希表建立数值和下标的映射,遍历时一边判断一边添加 /* 哇,LeetCode的第一题...啧啧 */ public int [] twoSum(int[] nums, int ...
- [LeetCode] TwoSum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- LeetCode——TwoSum
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- LeetCode #1 TwoSum
Description Given an array of integers, return indices of the two numbers such that they add up to a ...
- Leetcode 1——twosum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- leetcode题解 1.TwoSum
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
随机推荐
- 微信小程序打卡第五天
2018-02-1823:55:53大年初三 微信小程序已经学了5个夜晚了,没有很努力,只是简单地接触,感觉从今天开始有了突破的进展,很爽! 无意间发现一个很好的教程,也是一个老哥分享的,很给力 ht ...
- clipboard.min.js 复制表格内容
<script type="text/javascript" src="js/clipboard.min.js"></script> & ...
- Spartan6系列之GTP Transceiver的介绍与使用
1. 什么是GTP transceiver? GTP transceiver是FPGA里一种线速度达500Mb/sà6.6Gb/s的收发器,利用FPGA内部可编程资源可对其进行灵活地配置, ...
- java.lang.RuntimeException: java.lang.NullPointerException...的错误
先FQ,让电脑能登上谷歌,然后重新安装,应该就好了,我的是这样解决的.如果下次安装又报:java.lang.RuntimeException: java.lang.NullPointerExcepti ...
- QS之shell script
1 Invoke Mdoelsim In order to open Modelsim automatically, it is better to use a shell script to inv ...
- 常用Linux命令(长期更新)
有些命令如果不常用,老是记不住,每每用到总还要去查,特此将一些命令记录在此: (0)按指定时间删除文件 find target_dir -type f -mtime +3 -exec rm {} \; ...
- CAD使用GetXData读数据(网页版)
主要用到函数说明: MxDrawEntity::GetXData 返回实体的扩展数据. js代码实现如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 ...
- 【原】Python学习
1.常用模块介绍 #python -m SimpleHTTPServer 执行上面的命令就会在服务器当前目录下启动一个文件下载服务器,默认打开8000端口.这个时候,你只需要将IP和端口告诉客户端,即 ...
- 【原】SMTP发送邮件
1.下载class.phpmailer.php和class.smtp.php至公共库 2.编写发邮件的公共函数 function sendMail($param) { $config = C('THI ...
- 【转载】eclipse设置护眼色详细教程
先上一张效果图: 下面开始设置: 首先设置代码区的背景色: Window–>preference-->General-->Editors-->Test Editors ...