Leetcode-01-Tow SUM
题目链接:https://leetcode.com/problems/two-sum/
简述:
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].
题目很简单,给定一串数组,和一个目标值,要你从数组中找出两个数相加等于目标值。
首先给出最优解,时间复杂度为O(n):
class Solution {
public:vector<int> twoSum(vector<int>& nums, int target) {//Key is the number and value is its index in the vector.unordered_map<int, int> hash;vector<int> result;for (int i = 0; i < nums.size(); i++) {int numberToFind = target - nums[i];//if numberToFind is found in map, return themif (hash.find(numberToFind) != hash.end()) {//+1 because indices are NOT zero basedresult.push_back(hash[numberToFind] );result.push_back(i ); return result;}//number was not found. Put it in the map.hash[nums[i]] = i;}return result;}
};
(ps:这代码不是我想出来的,代码来源:https://leetcode.com/problems/two-sum/discuss/13/Accepted-C%2B%2B-O(n)-Solution)
然后分享我的解题思路:
很自然的用暴力方法过了一遍,复杂度O():
class Solution {
public:vector<int> twoSum(vector<int>& nums, int target) {for(int i=0;i<nums.size()-1;i++)for (int j = i + 1; j < nums.size(); j++)if (nums[i] + nums[j] == target)return {i,j}; return {};}
};
然后发现70%的人时间比我快。所以推测这个肯定有更好的办法。花了一分钟想了一下没有思路,打开讨论看别人的代码,看了热度最高的两个(上面最优解)
思路:,遍历数组a,计算a[i]与target的差值,把差值作为key在map里查找value,若找到则输出{i,value},否则把差值与下标i作为key-value存入map。
数组遍历一遍,map遍历一遍,不还是O()么?于是查了一下最优解里unordered_map里的find函数,unordered_map存储是哈希所以find复杂度是O(1),完毕;
优先使用map(或者unordered_map)的find函数而非algorithm里的find函数: https://www.cnblogs.com/daocaorenblog/p/5685412.html
其他问题:
CE:复制代码的时候少了花括号。
WA:target为0的时候没有返回。
AC1:爆力O();
AC2:优化了下O(),滑稽。
AC3:最优解代码。
以前写过一点编程题都是int main(),这里的用函数封装。
vector现学的。
unordered_map现学的。
哈希是学过的,但是刷题又不会手写个哈希,以至于哈希思想也不会想到,所以熟练运用ST很重要。
Leetcode-01-Tow SUM相关推荐
- leetcode之Tow Sum两数之和的三种思路
双重循环.桶排序.HashMap 题目链接:两数之和 1.双重循环,最基本的方法,速度慢O(n^2),但无需新空间. public int[] twoSum(int[] nums, int targe ...
- leetcode 304. Range Sum Query 2D - Immutable |304. 二维区域和检索 - 矩阵不可变(二维前缀和问题)
题目 https://leetcode.com/problems/range-sum-query-2d-immutable/ 题解 本题是 medium 难度,二维前缀和问题.相似题目有: Easy: ...
- 【动态规划】LeetCode 377. Combination Sum IV
LeetCode 377. Combination Sum IV Solution1: 我的未能AC的答案 题目描述的和前几道题差不多,但实际上不能用DFS来做(会超时),要用动态规划,还是记录一下吧 ...
- 【DFS】LeetCode 39. Combination Sum
LeetCode 39. Combination Sum Solution1: DFS,这个套路要熟记啊! class Solution { public:vector<vector<in ...
- LeetCode 167.Two Sum II 解题报告
LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...
- leetcode 1005 Maximize Sum Of Array After K Negations
leetcode 1005 Maximize Sum Of Array After K Negations 1.题目描述 2.解题思路 3.Python代码 1.题目描述 给定一个整数数组 A,我们只 ...
- [勇者闯LeetCode] 1. Two Sum
[勇者闯LeetCode] 1. Two Sum Description Given an array of integers, return indices of the two numbers s ...
- [勇者闯LeetCode] 112. Path Sum
[勇者闯LeetCode] 112. Path Sum Description Given a binary tree and a sum, determine if the tree has a r ...
- two sum python_Python | Leetcode 之 Two Sum
说来惭愧,到现在才开始刷Leetcode,但迟到总比不到好. 题目:Given an array of integers, return indices of the two numbers such ...
- 数据结构线段树介绍与笔试算法题-LeetCode 307. Range Sum Query - Mutable--Java解法
此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 线段树(Segme ...
最新文章
- 交叉分解(Cross decomposition)、典型关联分析(Canonical Correlation Analysis,CCA)、偏最小二乘回归PLS
- C# 调用C++ dll
- GAC中的所有的Assembly都会存放在系统目录%winroot%/assembly下面
- 防蓝光膜真的能阻挡蓝光,减小辐射吗?
- 实现类似add(1)(2)(3)的效果
- L2-018 多项式A除以B(模拟)
- 虚拟机模拟搭建Redis集群环境
- std::future、std::promise、std::packaged_task、std::async
- 使用POJO对象绑定请求参数
- Centos 7 环境下将修改后的MAC地址永久保存
- 编辑视频贴纸软件_视频贴纸软件介绍
- 若依管理系统RuoYi-Vue(前后端分离版)项目启动教程
- c语言统计输入文本不同字母单词数,统计文本中单词的个数
- 【转】你会休息吗?掌握最高效的休息方式----褪墨
- git clean 命令详解
- “问渠哪得清如许?为有源头活水来” – 提高技术源头数据的质量成为技术信息化热点
- “Windows找不到文件‘chrome‘”问题处理办法
- 计算机科学增刊是cscd吗,核心期刊增刊算核心吗?
- 同济计算机考研专业课,同济大学计算机考研难不难
- linux sed替换大小写,linux sed 批量替换字符串
热门文章
- 租传奇手游服务器网站,传奇手游服务端
- python爬虫爬当当网_爬虫实战一:爬取当当网所有 Python 书籍
- nginx网页地址重定向
- Leetcode 1599. Maximum Profit of Operating a Centennial Wheel (python)
- 中科创达怎么样-是外包公司吗-智能网联汽车和智能物联网推动业务快速增长
- 代理软件cntlm之下载、安装、配置及使用
- 数字电视专业术语--DTV名词扫盲
- 2021年全球未来50强排行榜:16家公司来自中国,美国仅信息技术行业就有16家公司上榜(附年榜TOP50详单)
- 接口转换器故障与解决办法
- 史上最全小型水库雨水情测报及大坝安全监测系统实施方案-花1小时让您秒变专家