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的更多相关文章

  1. LeetCode初体验—twoSum

    今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...

  2. leetcode — two-sum

    package org.lep.leetcode.twosum; import java.util.Arrays; import java.util.HashMap; import java.util ...

  3. leetCode:twoSum 两数之和 【JAVA实现】

    LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:t ...

  4. [leetcode]TwoSum系列问题

    1.普通数组找两个数,哈希表建立数值和下标的映射,遍历时一边判断一边添加 /* 哇,LeetCode的第一题...啧啧 */ public int [] twoSum(int[] nums, int ...

  5. [LeetCode] TwoSum

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

  6. LeetCode——TwoSum

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

  7. LeetCode #1 TwoSum

    Description Given an array of integers, return indices of the two numbers such that they add up to a ...

  8. Leetcode 1——twosum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  9. leetcode题解 1.TwoSum

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

随机推荐

  1. python自动化--语言基础一数据类型及类型转换

    Python中核心的数据类型有哪些?变量(数字.字符串.元组.列表.字典) 什么是数据的不可变性?哪些数据类型具有不可变性数据的不可变是指数据不可更改,比如: a = () #定义元组 #a[]= # ...

  2. [转] Redis在windows下安装过程

    转载自(http://www.cnblogs.com/M-LittleBird/p/5902850.html) 一.下载windows版本的Redis 去官网找了很久,发现原来在官网上可以下载的win ...

  3. 北大ACM(POJ1017-Packets)

    Question:http://poj.org/problem?id=1017 问题点:贪心. Memory: 224K Time: 32MS Language: C++ Result: Accept ...

  4. Stack frame

    http://en.citizendium.org/wiki/Stack_frame In computer science, a stack frame is a memory management ...

  5. sharepoint services

    I have got solution for authentication to share point web service I have use fedAuth Cookie and rtfa ...

  6. C# HttpWebRequest Post Get 请求数据

    Post请求 1 //data 2 string cookieStr = "Cookie信息"; 3 string postData = string.Format("u ...

  7. CAD控件:QT开发使用控件入门

    1. 环境搭建: 3 1.1. 安装Qt 3 1.2. 安装Microsoft Windows SDK的调试包 6 2. QT中使用MxDraw控件 7 1.3. 引入控件 7 3. 打开DWG文件 ...

  8. libevent reference Mannual III--working with events

    FYI: http://www.wangafu.net/~nickm/libevent-book/TOC.html Working with events Libevent’s basic unit ...

  9. UVA - 247 Calling Circles(Floyd求传递闭包)

    题目: 思路: 利用Floyd求传递闭包(mp[i][j] = mp[i][j]||(mp[i][k]&&mp[k][j]);),当mp[i][j]=1&&mp[j][ ...

  10. Crossword Answers UVA - 232

    题目大意 感觉挺水的一道题.找出左面右面不存在或者是黑色的格子的白各,然后编号输出一横向单词和竖向单词(具体看原题) 解析 ①找出各个格子的编号 ②对每个节点搜索一下 ③输出的时候注意最后一个数据后面 ...