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)?
				}
			}
		}
	}
};
时间: 2024-08-09 10:34:30

TwoSum leetcode的相关文章

ARTS汇总

第一周 - 2019/03/18~2019/03/22 ARTS-1 | Algorithm | TwoSum | leetcode ARTS-1 | Review | Brute Force | BF算法 | 蛮力法 ARTS-1 | Tips ARTS-1 | Share | JMeter录制通过SOCKS代理访问的网站 原文地址:https://www.cnblogs.com/jamesnpu/p/10562031.html

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a

LeetCode 1 # TwoSum

LeetCode 1 # TwoSum 很有意思的问题. Two Sum Total Accepted: 63448 Total Submissions: 350576 My Submissions Question Solution Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return in

leetcode #1 twoSum问题:简单实用哈希表

因为不是计算机专业的,算法基础有点差,所以最近开始在leetcode上刷刷题补一补. #1 问题链接:twoSum 问题描述: 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

[leetcode]TwoSum系列问题

1.普通数组找两个数,哈希表建立数值和下标的映射,遍历时一边判断一边添加 /* 哇,LeetCode的第一题...啧啧 */ public int [] twoSum(int[] nums, int target) { /* 两个数配合组成target 建立值和下标的映射,遍历数组时一边判断是否有另一半,一边添加新映射到哈希表 */ Map<Integer,Integer> map = new HashMap<>(); for (int i = 0; i < nums.len

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

LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:twoSum 两数之和 [JAVA实现] 方法一 使用双重循环两两相加判断是否等于目标值 public List<String> twoSum2(int[] arr, int sum) { if (arr == null || arr.length == 0) { return new ArrayL

LeetCode——TwoSum

题目: 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 t

LeetCode 之 TwoSum

题目: 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 t

LeetCode | TwoSum

def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ dict = {} result = [] for i in range(len(nums)): dict[nums[i]]= i print(dict) for i in range(len(nums)): if (dict.get(target-nu