You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
const sum = (arr, target) => {
let result = [];
arr.some((num, index) => {
const i = arr.findIndex(n => n !== num && n + num === target);
const found = i >= 0;
if (found) {
result = [].concat([i, index])
}
return found;
})
return result;
}
classSolution {
public:
vector<int> twoSum(vector<int>& A, int target) {
unordered_map<int, int> m;
for (int i = 0; i < A.size(); ++i) {
int t = target - A[i];
if (m.count(t)) return { m[t], i };
m[A[i]] = i;
}
return {};
}
};
function twoSum(nums, target) {
let map = {}
let arr = []
arr.forEach((item, index) => {
map[item] = index
})
for (let i = 0; i < nums.length; i++) {
let v = target - nums[i]
if (map[v]) {
arr = [i, map[v]]
break
}
}
return arr
}
function sum(arr, target) {
let i = 0,
k = arr.length - 1
while (i < k) {
if (arr[i] + arr[k] > target) {
k --
} else if (arr[i] + arr[k] < target) {
i ++
} else {
return [i, k]
}
}
return false
}
Activity
[-]两数之和[/-][+]leetcode:两数之和[/+]JerryWen1994 commentedon Apr 3, 2020
[-]leetcode:两数之和[/-][+]字节&leetcode:两数之和[/+]sisterAn commentedon Apr 4, 2020
解题思路:
map = new Map()numsnums[i]的差值,即k = target - nums[i],判断差值在map中是否存在map.has(k)为false) ,则将nums[i]加入到map中(key为nums[i], value为i,方便查找map中是否存在某值,并可以通过get方法直接拿到下标)map.has(k)),返回[map.get(k), i],求解结束nums中没有符合条件的两个数,返回[]时间复杂度:O(n)
代码实现:
leetcode
liwuhou commentedon Apr 6, 2020
uuRRx0 commentedon Apr 6, 2020
[-]字节&leetcode:两数之和[/-][+]字节&leetcode1:两数之和[/+]Aiyoweioo commentedon Apr 26, 2020
用一个hash来检查是否已经存在过该元素,用一次循环。要返回的是数组下标的索引
pb1314you commentedon Apr 29, 2020
function getTargetFromArray(nums, target){
let findIndex = [];
nums.map((num,i)=>{
let anotherNum = target-num;
let _findIndex = nums.findIndex(item =>item === anotherNum)
if(_findIndex>-1 && _findIndex!=i&&findIndex.length==0){
findIndex = [i, _findIndex]
}
})
return findIndex;
}
lyy199212 commentedon May 13, 2020
let getSumIndex = ()=>{
for (let index = 0; index < nums.length; index++) {
const element = nums[index];
let newArr = nums;
newArr[index] = '';//随便写个值占位
if(nums.indexOf(target-element)>-1){
return [index,nums.indexOf(target-element)]
}
}
}
chenjingtian commentedon May 15, 2020
babybrotherzb commentedon May 22, 2020
guanping1210 commentedon May 26, 2020
思路:
1、循环遍历一次,用目标值减去当前下标的值,得出一个减数值
2、判断这个减数值,是否存在,不存在或者是当前下标的值,则退出当前循环,进行下一次循环
BertieGo commentedon Jun 13, 2020
scottdao commentedon Jun 23, 2020
function(nums, target){
let cache = {};
for(let i = 0, len = nums.length; i<len; i++){
let ele = target - nums[i];
if(cache[ele] !== undefined){
return [cache[ele], i]
}
cache[nums[i]] = i;
}
}
xingorg1 commentedon Jun 23, 2020
请问用map实现和用纯对象实现,对比来说map实现会有什么优势吗
2456844450 commentedon Jun 27, 2020
the time complexity of map calling method is O(1), so using map makes the total time complexity very small.
Been101 commentedon Jul 11, 2020
AnranS commentedon Nov 16, 2020
Eason-Wu commentedon Jan 27, 2021
azl397985856 commentedon Mar 3, 2021
题目地址(1. 两数之和)
https://leetcode-cn.com/problems/two-sum
题目描述
前置知识
公司
思路
最容易想到的就是暴力枚举,我们可以利用两层 for 循环来遍历每个元素,并查找满足条件的目标元素。不过这样时间复杂度为 O(N^2),空间复杂度为 O(1),时间复杂度较高,我们要想办法进行优化。
这里我们可以增加一个 Map 记录已经遍历过的数字及其对应的索引值。这样当遍历一个新数字的时候就去 Map 里查询 target 与该数的差值是否已经在前面的数字中出现过。如果出现过,就找到了答案,就不必再往下继续执行了。
关键点
代码
Go Code:
CPP Code:
复杂度分析
更多题解可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
oakland commentedon Apr 28, 2021
@sisterAn 请教下 hash 为什么用 Map,而不用 Object 呢?
painting-alt commentedon Oct 8, 2021
XW666 commentedon Feb 9, 2022
tianshiyang commentedon Jan 15, 2023
function sum(arr, target) {
let i = 0,
k = arr.length - 1
while (i < k) {
if (arr[i] + arr[k] > target) {
k --
} else if (arr[i] + arr[k] < target) {
i ++
} else {
return [i, k]
}
}
return false
}