在数组中找到 2 个数之和等于给定值的数字,结果返回 2 个数字在数组中的下标。

1. 解法1 时间复杂度 O(n^2)

直接两次遍历所有节点,进行求和比较

代码如下:

func twoSum(nums []int, target int) []int {
res := make([]int, 2, 2)
for i:= 0;i<len(nums);i++{
for j:=i+1;j<len(nums);j++{
if nums[i]+nums[j]==target{
res[0] =i
res[1]=j
}
}
}
return res
}

2.解法2: 时间复杂度O(n)

只需要遍历一次所有元素,用 哈希表(键值对表)进行存储即可: 只要数字总和,那么每遍历一次数组,就可以算出他的求和的另一个数字的值,在接下来的遍历中如果找到了就成功得到答案,否则返回Nil

字典就是哈希表的实现。

代码如下:

func twoSum(nums []int, target int) []int {
data := make(map[int]int)
for i := 0; i < len(nums); i++ {
another := target - nums[i]
if _, ok := data[another]; ok {
return []int{data[another], i}
}
data[nums[i]]=i
}
return nil
}

语法熟悉

Go多重赋值

if _, ok := m[another]; ok {

首先执行;前的内容,在Go中,根据键访问map会返回两个值,第一个值是这个键对应的值,第二个值是该键是否存在,如果存在返回True,否则False。接下根据OK是否为真判断是否进入后续语句。

return []int{data[another], i }

如果存在,就说明当前位置的元素的另一半存在于这个哈希表中,就访问成功了,那么找到满足要求的两个数,需要返回他俩的下标,这个哈希表中键是元素,值是下标,就返回即可。

如果没有找到另一半,就将这个元素加入到哈希表中,由于是在哈希表中查找,那么哈希表中的元素必然在原Array中位置靠前,所以应先返回哈希表中的元素。

1. Two Sum Go实现的更多相关文章

  1. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  2. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. [LeetCode] Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  10. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. Solo 独立开发者社区

    Solo 独立开发者社区是一个致力于帮助独立开发者发展的社区平台.平台上聚集了大量的独立开发者,他们在这个平台上分享着自己的开发经验.心得和技巧,同时也可以从其他人的分享中获得启发和帮助. 这个社区提 ...

  2. 解决方案 | vbnet的msgbox 窗口最前置,topmost属性设置

    For that you can use the TopMost Property of MsgBox (Number 262144) MsgBox("Hello there", ...

  3. 解码 xsync 的 map 实现

    解码 xsync 的 map 实现 最近在寻找 Go 的并发 map 库的时候,翻到一个 github 宝藏库,xsync (https://github.com/puzpuzpuz/xsync) . ...

  4. oeasy教您玩转vim - 29 - # 垂直翻页

    ​ 垂直翻页 回忆上节课内容 我们上次了解了横向滚动的相关信息 横滚幅度 - sidescroll 横滚留位 - sidescrolloff 自动换行 - wrap g j.g k 可以逐行上下移动 ...

  5. CF1359A 题解

    洛谷链接&CF 链接 题目简述 共有 \(T\) 组数据. 对于每组数据给出 \(n,m,k\),表示 \(k\) 名玩家打牌,共 \(n\) 张牌,\(m\) 张王,保证 \(k \mid ...

  6. selenium启动Chrome配置参数问题

    每次当selenium启动chrome浏览器的时候,chrome浏览器很干净,没有插件.没有收藏.没有历史记录,这是因为selenium在启动chrome时为了保证最快的运行效率,启动了一个裸浏览器, ...

  7. .NET周刊【7月第4期 2024-07-28】

    国内文章 .NET 高性能缓冲队列实现 BufferQueue https://mp.weixin.qq.com/s/fUhJpyPqwcmb3whuV3CDyg BufferQueue 是一个用 . ...

  8. 【Shiro】07 散列算法 & 凭证配置

    [散列算法概述] 用于生成数据的摘要信息,不可逆算法,用于存储密码或者密文数据. 常见散列算法类型:MD5.SHA 一般进行散列时提供一个"盐",即系统知道的"干扰数据& ...

  9. 人工智能(AI)未来之方向:努力培养人才、科研创新!

    地址: https://baijiahao.baidu.com/s?id=1801824912676717630&wfr=spider&for=pc 人工智能(AI)未来之方向 1. ...

  10. 计算机类的短周期的SCI期刊

    <Human-centric Computing and Information Sciences> 韩国人办的,Open Access,周期短,费用高,SCI二区,水毕业可用. 以下引自 ...