1.两数之和(Two Sum) C++
暴力法可解决,速度很慢。
解决办法:哈希表
知识点:
- map的构造
- 遍历map使用迭代器,判断条件
- 插入 pair<int,int>
- 寻找key是否存在
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int,int> m;
map<int,int>::iterator itr;
vector<int> ans;
for(int i=; i<nums.size(); i++)
{ if(i == )
{
m.insert(pair<int,int>(nums.at(i),i));
continue;
} itr = m.find(target-nums.at(i));
if(itr != m.end())
{
ans.push_back(itr->second);
ans.push_back(i);
return ans;
}
m.insert(pair<int,int>(nums.at(i),i));
}
}
};
1.两数之和(Two Sum) C++的更多相关文章
- 领扣-1/167 两数之和 Two Sum MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- [Swift]LeetCode1 .两数之和 | Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- 两数之和 Two Sum
给定一个整数数列,找出其中和为特定值的那两个数. 你可以假设每个输入都只会有一种答案,同样的元素不能被重用. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 n ...
- c++谭浩强教材教学练习例题1.2 求两数之和 为什么sum=a+b;sum的值为65538
第一章 #include <iostream>using namespace std; int main(){ int a,b,sum; sum=a+b; cin>>a> ...
- LeetCode 1:两数之和 Two Sum
题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中 ...
- LeetCode 653. 两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)
653. 两数之和 IV - 输入 BST 653. Two Sum IV - Input is a BST 题目描述 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定 ...
- LeetCode_#1_两数之和 Two Sum_C++题解
1. 两数之和 Two Sum 题目描述 Given an array of integers, return indices of the two numbers such that they ad ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- LeetCode 371. Sum of Two Integers (两数之和)
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
随机推荐
- java web项目在linux部署、启动,查看系统配置常用的linux命令总结
本文为博主原创,未经允许不得转载: 以下为在工作中常用的linux命令进行了总结,主要在项目安装及启动,及编辑部署文件时应用较多1.gz文件是一种压缩文件. 以·tar.gz为扩展名的是一种压缩文件, ...
- js判断数字、整数、字符串、布尔,特殊方法
整数: function isInteger(obj) { return Math.floor(obj) === obj } isInteger(3) // true isInteger(3.3) / ...
- 【译】第44节---EF6-存储过程映射
原文:http://www.entityframeworktutorial.net/entityframework6/code-first-insert-update-delete-stored-pr ...
- 【译】第4节---简单的Code First示例
原文地址:http://www.entityframeworktutorial.net/code-first/simple-code-first-example.aspx 假设我们要为XYZ学校创建一 ...
- 实现一个简单的flux
前言 众所周知,React跟Flux是一对好基友. 其中,市场流行的Flux有Redux,Mobx,Reflux. 其中,用法最简单的是Reflux. 其数据流思路如下: +---------+ +- ...
- SVN服务器搭建和使用教程
安装SVNserver 点击Next下一步,如下: 然后再点击Next项,下一步,如下: 点击[Next] 如下: Location是指VisualSVN Server的安装目录,Repository ...
- 【BZOJ】1052: [HAOI2007]覆盖问题
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1052 大概自己YY了个贪心然后过了... 二分答案,考虑如何check: 找到一个最小的矩 ...
- 《spring boot 实战》读书笔记
前言:虽然已经用spring boot开发过一套系统,但是之前都是拿来主义,没有系统的,全面的了解过这套框架.现在通过学习<spring boot实战>这本书,希望温故知新.顺便实现自己的 ...
- win10,python3.6,django2.0.3,项目基本命令
1.django-admin startproject project_name(创建项目) 2.python manage.py startapp appname(创建应用) 3.python ma ...
- App.Config自定义配置节点
配置文件: <?xml version="1.0" encoding="utf-8"?> <configuration> <con ...