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 ...
随机推荐
- 【C#】 Method invocation is skipped
相信大家看到这个标题也是一头雾水了. 这个问题主要是我在项目中遇到了一个问题, 然后我通过搜索引擎搜索的关键词进而找到了answer, 我先描述一下我遇到的问题: 做项目的时候我发现log时常没有输出 ...
- Nuget CsvHelper 的使用
CsvHelper:nuget地址 csv导出类||生成类 public class CSVHeader { public string head1 { get; set; } public stri ...
- jquery 获取元素(父节点,子节点,兄弟节点),元素筛选
一, js 获取元素(父节点,子节点,兄弟节点) var test = document.getElementById("test"); var parent = test.p ...
- 跳跳虎回家(国庆10.1模拟赛T2)
题目: [题目描述] 跳跳虎在外面出去玩忘了时间,现在他需要在最短的时间内赶回家. 跳跳虎所在的世界可以抽象成一个含有 n 个点的图(点编号从 1 到 n ),跳跳虎现在在 1 号点,跳跳虎的家在 n ...
- VirtualBox-- 虚拟机网络设置2--主机与虚拟机互相访问且均上外网
转载自:http://blog.sina.com.cn/s/blog_7de9d5d80100t2uw.html VirtualBox中有4中网络连接方式:NATBridged AdapterIn ...
- 深入浅析Spring的AOP实现原理
转载来源:https://www.jb51.net/article/81788.htm AOP(Aspect-OrientedProgramming,面向切面编程),可以说是OOP(Object-Or ...
- 面试题中关于String的常见操作
题目1: 将用户输入的一段话,每个单词的首字母大写, 每个单词之间的空格调整为只有一个,遇到数字,将数字与后一个单词用下划线 "_" 进行连接 题目2:将 i @@ am @@@ ...
- Jdbc -Statement
Java提供了 Statement.PreparedStatement 和 CallableStatement三种方式来执行查询语句: PreparedStatement是用于执行参数化查询 预编译s ...
- hibernate事务规范写法
@Test public void testTx() { SessionFactory sessionFactory = null; Session session = null; Transacti ...
- Uncaught TypeError: (intermediate value)(...) is not a function 上一个方法结束没有加分号; 代码解析报错
Uncaught TypeError: (intermediate value)(...) is not a function 别忽略了, 第一个方法后面的结束 分号; 不起眼的,引来麻烦, 哎,规 ...