LeetCode_TwoSum、C++解法
来源:互联网 发布:php 提现到银行卡 编辑:程序博客网 时间:2024/06/06 00:44
时间复杂度O(n),利用C++STL库函数就可以解决问题;
当然网上还有一种解法,排序+二分,时间复杂度也可以是O(n),这里就不写了
这里要特别说明的是:
本来我是想用hash_map来写这个程序,但是发现VS2017报错,hash_map is deprecated and will be removed
意思是:hash_map已经被弃用
确实在MSDN的API文档里面有这句话This API is obsolete. The alternative is unordered_map Class.
AC代码如下
std::vector<int> twoSum(std::vector<int>& nums, int target) { std::vector<int> res; std::unordered_map<int, int> hashs; for (int i = 0; i < nums.size(); i++) { if (hashs.end() != hashs.find(target - nums[i])) { res.push_back(hashs[target - nums[i]]); res.push_back(i); return res; } hashs.insert(std::make_pair(nums[i], i)); } return res;}
阅读全文
0 0
- LeetCode_TwoSum、C++解法
- 汉诺塔解法(C/C++)
- 蚂蚁问题解法----C版本
- 迷宫问题C语言解法
- 汉诺塔解法C语言实现
- 数独C语言解法
- C语言 8皇后解法
- 小学奥数题C语言解法
- 汉若塔游戏非递归解法(c#)
- 某公司C笔试题 [个人解法]
- 关于约瑟夫环的c语言解法
- 约瑟夫环问题(Josephus)的C解法
- C语言求素数的不同解法
- N皇后问题c语言解法
- 2014NEUQ图灵杯(C语言解法)
- C语言经典弱智问题解法整理
- 数独的解法c语言
- ai2813-画家问题-回溯解法C语言
- 数组的总结
- newton插值
- 旋转,位移
- 【python】numpy中的高效分块操作np.stride_tricks.as_strided
- log4j配置和springboot中Logback配置
- LeetCode_TwoSum、C++解法
- html 学习笔记
- 从零开始前端学习[41]:html5中新增加的一些智能表单
- 我的第一个博客----浅谈人生观价值观
- Recyclerveiw 简单讲解
- codeforces 870C Maximum splitting
- python产生随机数
- css 学习笔记
- Activity 屏幕旋转引起的销毁与重启