1. 两数之和 Two Sum

题目描述

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

You may assume that each input would have exactly one solution, and you may not use the same element twice.

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

自解_暴力法

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> ans(2);
for (int i = 0; i < nums.size() - 1; i++){
for (int j = i + 1; j < nums.size(); j++){
if (nums[i] + nums[j] == target){
ans[0] = i;
ans[1] = j;
return ans;
}
}
}
return ans;
}
};
  • 与官方题解1类似,暴力法
  • 时间复杂度:\(O(n^2)\),对于每个元素,试图通过遍历数组的其余部分来寻找它所对应的目标元素。
  • 空间复杂度:\(O(1)\)

注意

  1. error: control reaches end of non-void function:控制到达非void函数的结尾。即本应带有返回值的函数到达结尾后可能并没有返回任何值

    本题中有可能找不到目标值,所以需要在循环检测外也返回一个值,否则会出现此错误

  2. 想要返回vector时,如果是在调用函数内部创建的vector,会出问题。解决方法:将需要返回的vector引用值作为参数传进函数,在函数内部使用引用。而函数类型设置为bool

    参考:vector作为函数返回值

参考官方题解_map

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int, int> index;
vector<int> ans(2);
for (int i = 0; i < nums.size(); i++){
if (index.find(target - nums[i]) != index.end()){
ans[0] = index.find(target - nums[i]) -> second;
ans[1] = i;
return ans;
}
else{
index.insert(pair<int,int>(nums[i],i));
}
}
return ans;
}
};
  • 时间复杂度:\(O(n\log(n))\),将元素与索引放入map,利用map进行查找。由于C++的map是基于红黑树实现的,查找的时间复杂度约为\(\log(n)\),所以时间复杂度为 \(O(n\log(n))\)。
  • 空间复杂度:\(O(n)\),所需的额外空间取决于map中存储的元素数量,该表中存储了 \(n\) 个元素,但C++的map基于红黑树实现,该树的一个节点在不保存数据时,占用16字节的空间,包括一个父节点指针,左右孩子指针,还有一个枚举值(标示红黑的,相当于平衡二叉树中的平衡因子),可见,map还是很耗内存的。

    参考:C++ map详解

参考官方题解_unordered_map

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> index;
for (int i = 0; i < nums.size(); i++){
auto it = index.find(target - nums[i]);
if (it != index.end()){
return vector{it->second, i};
}
else{
index[nums[i]] = i;
}
}
return vector<int>();
}
};
  • 时间复杂度:\(O(n)\),unordered_map查找复杂度\(O(1)\),最坏情况\(O(n)\)
  • 空间复杂度:\(O(n)\)

注意

  1. unordered_map

    • C++ 11标准中加入了unordered系列的容器。unordered_map记录元素的hash值,根据hash值判断元素是否相同。
    • map相当于java中的TreeMap,unordered_map相当于HashMap。
    • 无论从查找、插入上来说,unordered_map的效率都优于hash_map,更优于map;而空间复杂度方面,hash_map最低,unordered_map次之,map最大。

    参考:详细介绍C++STL:unordered_map

  2. vector 初始化,可直接用{}包含元素,题目中需要返回的vector可以直接在返回时定义

    参考:vector 的六种创建和初始化方法

最新文章

  1. MMORPG大型游戏设计与开发(服务器 AI 基础接口)
  2. Linux 下测试串口的命令microcom
  3. SQL Server时间粒度系列----第4节季、年时间粒度详解
  4. mysql查看表使用的数据库引擎
  5. iOS小技巧总结,绝对有你想要的
  6. 《zw版&#183;Halcon-delphi系列原创教程》 Halcon分类函数&#183;简明中文手册 总览
  7. 当前标识(NT AUTHORITY\NETWORK SERVICE)没有对“C:\WINDOWS\Microsoft.NET\Frame
  8. robots.txt文件配置和使用方法详解
  9. LR(0)语法分析
  10. 利用序列化的方式实现C#深复制和浅复制
  11. 函数 flst_get_first
  12. npm 好用工具 for 前端
  13. OFbiz--HelloWorld
  14. ORACLE 更改username
  15. IDEA mybatis mapper类跳转到xml文件
  16. MySql事务的隔离级别及作用
  17. vue的插槽slot
  18. pandas DataFrame 索引(iloc 与 loc 的区别)
  19. 固定尺寸内存块的缓冲队列类及C++实现源代码
  20. Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting

热门文章

  1. [Go] 基础系列二:channel的关闭和广播
  2. Linux - /bin/sh^M: bad interpreter: No such file or directory
  3. 2018-2019-2 20165234 《网络对抗技术》 Exp7 网络欺诈防范
  4. 使用java写js中类似setTimeout的代码
  5. office web apps 在线问答预览
  6. 原创 linux 爬虫拨号服务器完整设置
  7. C# 将文本写入到文件
  8. c#添加资源
  9. ObjectAnimator简单示例
  10. connections java.net.BindException: Address already in use_解决方案