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 ...
随机推荐
- html5前端杂记
首先是css的一些知识 毕竟自己懂得不多,但是一看资料.感觉似曾相识 <style> .red-text { color: red; } </style>//这里是css样式的 ...
- Python+selenium测试环境成功搭建,简单控制浏览器(firefox)接下来,继续学习其他浏览器上的测试环境搭建;学习Python语言,利用Python语言来写测试用例。加油!!!
Python+selenium测试环境成功搭建,简单控制浏览器(firefox)接下来,继续学习其他浏览器上的测试环境搭建:学习Python语言,利用Python语言来写测试用例.加油!!!
- DatePickerDialog和TimePickerDialog(基于对话框显示时间和日期)
public class MainActivity extends Activity implements android.view.View.OnClickListener{ private But ...
- glassfish中新建数据源(创建数据库连接池)
1.浏览器输入:http://localhost:4848 登录glassfish域管理控制台,默认的用户名和密码是amin和adminadmin.(也可以通过NetBeans的服务选项卡--服务器- ...
- caffe2:conda路径和权限问题
在使用conda之后,总是不能直接使用 conda install 命令,需要把codna添加到系统路径,取代默认Python. 在-/.bashrc中,添加 # added by Anaconda2 ...
- MFC获取各类指针句柄
最近有些人在问MFC编程一些要点,有一些句柄的获取.指针的获取是常见的问题,本文将对这些问题做以解释,参考了前人的笔录(见reference),希望能够帮助大家更方便地进行MFC程序开发. 一般我们使 ...
- scala学习(1)----map和flatMap的区别
转载:https://www.cnblogs.com/wbh1000/p/9846401.html 两者的区别主要在于action后得到的值 例子: import org.apache.spark.{ ...
- HDU - 5952 Counting Cliques(dfs搜索)
题目: A clique is a complete graph, in which there is an edge between every pair of the vertices. Give ...
- 关于ISIS协议 CSNP报文的周期更新理解
为何ISIS协议的CSNP报文在MA网络环境中是以周期更新然而在P2P网络环境中只更新一次? 个人通过视频及资料学习理解: 我们知道ISIS的CSNP报文类似OSPF中的DBD报文,作用就是用来确认彼 ...
- 基于服务器版centos7的Hadoop/spark搭建
前提说明: 1.Hadoop与spark是两个独立的框架,只安装spark也可独立运行,spark有自己的调度器(standalone模式): 2.在Hadoop的基础上安装spark就是为了使用ya ...