在数组中找到 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. oeasy教您玩转vim - 25 - 更多颜色

    ​ 更多颜色 回忆上节课内容 我们上次深入了配色方案 定义了自己的配色方案 oeasy 建立了自己的配色 oeasy 在状态栏应用了自己的配色 ​ 明确能用的颜色 先胡乱地尝试一下修改颜色代码 hi ...

  2. Java JVM——13. 垃圾回收相关算法

    1.生存还是死亡? 在堆里存放着几乎所有的 Java 对象实例,在 GC 执行垃圾回收之前,首先需要区分出内存中哪些是存活对象,哪些是已经死亡的对象.只有被标记为己经死亡的对象,GC 才会在执行垃圾回 ...

  3. QT 的 ModelView

    QApplication a(argc, argv); QDirModel model;    //QDirModel,   问文件目录树 QTreeView tree;    QListView l ...

  4. 如何正确使用@Bulider与<T>返回数据

    @Data @ToString @Builder @AllArgsConstructor public class PageResult<T> implements Serializabl ...

  5. SpringBoot2.7 霸王硬上弓 Logback1.3 → 不甜但解渴

    开心一刻 一大早,她就发消息质问我 她:你给我老实交代,昨晚去哪鬼混了? 我:没有,就哥几个喝了点酒 她:那我给你打了那么多视频,为什么不接? 我:不太方便呀 她:我不信,和你哥们儿喝酒有啥不方便接视 ...

  6. 【Vue】自己编写排名组件

    一.需求分析 这里我是用Echarts的柱状图,倒置下y轴x轴就实现了,然后产品说跟UI不一致 我一看UI这种又给我整不会了,然后想拿Echarts改改参数搞定,同事和群友都是说自己做,不要用Echa ...

  7. 【RabbitMQ】03 订阅模式

    Pub / Sub 订阅模式 特点是 一条消息可以给多个消费者接收了 首先创建订阅模式生产者发生一些代码变动: package cn.dzz.pubSub; import com.rabbitmq.c ...

  8. 【Mybatis】14 缓存

    1.什么是缓存? - 缓存是指把经常需要读写的数据,保存到一个高速的缓冲区中,这个行为叫缓存 - 也可以是指被保存在高速缓冲区的数据,也叫缓存 2.Mybatis缓存 Mybatis中分为一级缓存和二 ...

  9. 家务机器人(人形机器人)—— Mobile ALOHA: Your Housekeeping Robot

    项目地址: https://mobile-aloha.github.io/ 演示视频地址: https://www.youtube.com/watch?v=HaaZ8ss-HP4 论文地址: http ...

  10. conda报错、anconda报错:requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

    anconda报错,报错信息: requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 不能使用c ...