题目描述:

/*
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. flex遭遇text-overflow:hidden,white-space:nowrap

    最近在项目中遇到使用flex的时候,在flex-item元素中使用text-overflow:hidden:white-space:nowrap:进行省略文字的操作. 发现flex-item失控了,长 ...

  2. laravel5.2总结--集合

          类(Laravel集合基类) Illuminate\Support\Collection 类提供一个流畅.便利的封装来操控数组数据,官方提供了很多辅助函数,方便对数据进行各种处理,Coll ...

  3. Linux之ubuntu系统操作学习笔记

    1,swp分区:当内存不够时用swp分区顶替内存 2,语言环境检查  locale –a:可以明白系统支持什么语言 3,安装软件: apt-cache search(软件):搜索软件 apt-cach ...

  4. 理解机器为什么可以学习(三)---Theory of Generalization

    前边讨论了我们介绍了成长函数和break point,现在继续讨论m是否成长很慢,是否能够取代M. 成长函数就是二分类的排列组合的数量.break point是第一个不能shatter(覆盖所有情形) ...

  5. Spring整合hibernate -声明事务管理

     目录 1 sessionFactory 注入HibernateTransactionManager 2 XML配置的配置 3 添加annotation-driven 4 引入JAR包 5在servi ...

  6. LAMP总四部分

    第一部分 1. 安装mysqlcd /usr/local/src/ 免安装编译二进制的包wget http://syslab.comsenz.com/downloads/linux/mysql-5.1 ...

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

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

  8. 文件处理.Windows.Fastcopy.3.50.x64.文件复制简体中文破解版(验证版)

    摘要:Fastcopy 3.50 + x64 绿色汉化中文版由知索网汉化发布.Fastcopy 是一款来自日本的最快的文件拷贝工具.磁盘间相互拷贝文件是司空见惯的事情,通常情况都是利用 WINDOWS ...

  9. js setInterval 启用&停止

    以下面例子为说明: <title></title> <script src="Scripts/jquery-1.4.1-vsdoc.js" type= ...

  10. 解决jsp在ios小屏手机下面滑动不流畅的问题

    今天做好的静态文件发给后台改成jsp之后,发现原本流畅滑动的页面在iphone5下面变得一卡一卡的. 之后加上了 -webkit-overflow-scrolling: touch; 这个属性之后,成 ...