题目描述:

/*
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].
*/

这道题给了我们一个数组,还有一个目标数target,让我们找到两个数字,使其和为target。首先想到的就是暴力搜索,遍历所有的组合项,获得结果,思路比较简单,代码如下:
func func1(s []int, tag int) []int {
for i := ; i < len(s)-; i++ {
for j := i + ; j < len(s); j++ {
if s[i]+s[j] == tag {
return []int{i, j}
}
}
}
return nil
}
这个算法的时间复杂度是O(n^2)。虽然节省了空间,但是时间复杂度高。尝试用空间换时间,使用一个HashMap,建立数字和其坐标位置之间的映射,在遍历数组的时候,用target减去遍历到的数字,就是另一个需要的数字了,直接在HashMap中查找其是否存在即可,那么代码就可这样写:
func func2(s []int, tag int) []int {
hash := make(map[int]int)
for i := ; i < len(s); i++ {
hash[s[i]] = i
} for i := ; i < len(s); i++ {
temp := tag - s[i]
if _, ok := hash[temp]; ok {
if hash[temp] == i {
continue
}
return []int{i, hash[temp]}
}
}
return nil
}

这个算法的时间复杂度是O(n)。再想想,代码好像可以更加简洁一些,把两个for循环合并成一个:
func func3(s []int, tag int) []int {
hash := make(map[int]int, len(s))
for k, v := range s {
if j, ok := hash[tag-v]; ok {
return []int{j, k}
}
hash[v] = k
}
return nil
}
这样看起来就比较舒服ok了,完整的贴上来,运行一下试试吧。
package main

import (
"fmt"
) func main() {
nums := []int{, , , }
target :=
s := func1(nums, target)
//s := func2(nums, target)
//s := func3(nums, target)
fmt.Printf("nums[%d]+nums[%d]=%d+%d=%d\n", s[], s[], nums[s[]], nums[s[]], target)
fmt.Printf("return [%d,%d]", s[], s[])
} //暴力破解 时间复杂度O(n^2)
func func1(s []int, tag int) []int {
for i := ; i < len(s)-; i++ {
for j := i + ; j < len(s); j++ {
if s[i]+s[j] == tag {
return []int{i, j}
}
}
}
return nil
} //用map辅助查找 时间复杂度O(n)
func func2(s []int, tag int) []int {
hash := make(map[int]int)
for i := ; i < len(s); i++ {
hash[s[i]] = i
} for i := ; i < len(s); i++ {
temp := tag - s[i]
if _, ok := hash[temp]; ok {
if hash[temp] == i {
continue
}
return []int{i, hash[temp]}
}
}
return nil
}
//优化一下,把两个for循环合并成一个
func func3(s []int, tag int) []int {
hash := make(map[int]int, len(s))
for k, v := range s {
if j, ok := hash[tag-v]; ok {
return []int{j, k}
}
hash[v] = k
}
return nil
}
												

[LeetCode] 1.Two Sum 两数之和分析以及实现 (golang)的更多相关文章

  1. [LeetCode] 1. Two Sum 两数之和

    Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...

  2. [LeetCode]1.Two Sum 两数之和&&第一次刷题感想

    ---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...

  3. 【LeetCode】Two Sum(两数之和)

    这道题是LeetCode里的第1道题. 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会 ...

  4. [leetcode]1. Two Sum两数之和

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

  5. [LeetCode]1.Two Sum 两数之和(Java)

    原题地址:two-sum 题目描述: 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标. 你可以假设每 ...

  6. LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现

    1. Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up ...

  7. 【LeetCode】1. Two Sum 两数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...

  8. Leetcode:0002(两数之和)

    LeetCode:0002(两数之和) 题目描述:给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表.你可以假设除了数字 0 之外,这两 ...

  9. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

随机推荐

  1. datagrid的toolbar的两种实现方式

    datagrid的toolbar的两种实现方式 1.在html文件中,可以在设置toolbar="#tb",再在div中设置id="tb" <table ...

  2. flex布局之flex-grow和flex-shrink如何计算

    此文已由作者张含会授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 关于盒模型布局 如何实现两栏布局? (表格) 流式, 浮动, 定位 如何选择? 流式 > 浮动 > ...

  3. bash shell命令与监测的那点事(一)

    bash shell命令与监测的那点事之ps 学习LInux,不得不谈谈bash shell命令,介绍Linux命令行与Shell脚本的书有很多很多,bash shell命令也有很多,此次我们只谈谈有 ...

  4. VC++中PostMessage、SendMessage和PeekMessage之间的区别

    1, PostMessage只把消息放入队列,不管其他程序是否处理都返回,然后继续执行,这是个异步消息投放函数.而SendMessage必须等待其他程序处理消息完了之后才返回,继续执行,这是个同步消息 ...

  5. Event Loop详解

    1.进程,单线程与多线 进程: 运行的程序就是一个进程,比如你正在运行的浏览器,它会有一个进程. 线程: 程序中独立运行的代码段. 一个进程由单个或多个线程组成,线程是负责执行代码的. 2.单线程与多 ...

  6. Puppet单机实战之Nginx代理Tomcat

    author:JevonWei 版权声明:原创作品 blog:http://119.23.52.191/ --- 构建实战之Nginx代理Tomcat [root@node1 modules]# mk ...

  7. BZOJ 2818: Gcd(欧拉函数)

    GCDDescription 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 ...

  8. Unity3D 绘制扇形/环形

    using UnityEngine; using System.Collections; using System.Collections.Generic; public class Cone : M ...

  9. Canvas与Image互相转换示例以及利用该技术实现微信长按自动识别二维码功能

    现在扫描二维码已经很普遍,微信扫一扫即可,但是如果二维码是在自己的手机上呢?那就要用到微信里的一个功能了,手指长按二维码,会弹出自动识别的选项,点确定就可以看到二维码的内容了.那么怎么通过前端实现这个 ...

  10. 实例对比 Julia, R, Python,谁是狼语言?

    对于一个平台来说,使用者对技术本身是不敏感的,所以我们需要增加一些限制来减少集群的一些不可控情况,例如不断的写入新表/新数据却不记得删除,大量不按规范创建的表名等情况.与此同时应尽量让技术对用户透明, ...