Leetcode 1. Two Sum(hash+stl)
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> tm;
int a = , b = ;
int len = nums.size();
multimap<int,int> hash;
for(int i = ; i < len; i++){
hash.insert(pair<int,int>(nums[i],i));
}
for(int i = ; i < len; i++){
multimap<int,int>::iterator s;
s=hash.find(target-nums[i]);
if(s== hash.end()){
continue;
}
if(i != (*s).second){
a = i;
b = (*s).second;
break;
}
}
tm.push_back(a);
tm.push_back(b);
return tm;
}
};
Leetcode 1. Two Sum(hash+stl)的更多相关文章
- LeetCode 1. Two Sum (c++ stl map)
题目:https://leetcode.com/problems/two-sum/description/ stl map代码: class Solution { public: vector< ...
- LeetCode 437. Path Sum III (STL map前缀和)
找遍所有路径,特判以根为起点的串即可. 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tr ...
- LeetCode one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [leetCode][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
随机推荐
- cocos2dx基础篇(21) 进度条CCProgressTimer
[3.x] (1)去掉 "CC" (2)CCProgressTimerType 改为强枚举 ProgressTimer::Type:: // RADIAL //扇形进度计时器 BA ...
- ActiveMQ学习教程/2.简单示例
ActiveMQ学习教程(二)——简单示例 一.应用IDEA构建Maven项目 File->New->Module...->Maven->勾选->选择->Next ...
- 【Go语言】map在goroutine通信中的使用问题
简介 本篇文章的主要内容是解决go语言map在使用中遇到的两个问题,对于初学者是不可避免的坑 一.cannot assign to struct field 当map中存在struct类型的成员,如果 ...
- 2019第二周总结.Java
本学期开始学习Java课程了,首先我先说说学习Java的感觉吧,它不像C语言程序设计,但是又有语言开发的共同点.学Java语言重点是面向对象的程序设计,更加的适应生活需要和计算机开发的需要. 总的来讲 ...
- Spring源码深度解析
Spring源码分析 Spring Web入门及源码学习 [Spring源码分析]Bean加载流程概览 Spring Framework 实现原理与源码解析系统 Spring源码分析--水门 Spri ...
- webpack4下url-loader打包图片问题
webpack.condig.js: const path = require('path'); //导入插件 const VueLoaderPlugin = require('vue-loade ...
- 嘉馨学姐又双叒叕来吃包子了 QDUOJ 模拟 尺度法
嘉馨学姐又双叒叕来吃包子了 QDUOJ 模拟 尺度法 点我进入OJ题目详情 题意 给你一串数,让你求长度最长的子串,这个字串满足里面没有重复出现的数字. 解题思路 使用一个标记数组,来标记每个数的第一 ...
- ETL工具Kettle使用以及与Java整合实现数据清洗
本文主要讲述kettle的使用和与Java整合,具体下载与安装请自行百度! kettle有两种脚本方式:转换和工作,工作中可以添加转换.以下以转换为例. 1.新建一个转换, 2.在工作中经常用到的是表 ...
- http协议是无状态协议,它的无状态指的是什么,如何解决这种情况
http是无状态的协议,也是不安全的协议, 它的无状态是指对于事务处理没有记忆能力,缺少状态意味着后续的操作需要前面的信息. 解决办法:1,通过cookie解决,2,通过session会话保存.
- CenterOS7中解决No package mysql-server available.
CenterOS7中解决No package mysql-server available. 1.使用yum install -y mysql-server报错如下: [root@heyong_jd ...