[LeetCode] 1.Two Sum 两数之和分析以及实现 (golang)
题目描述:
/*
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)的更多相关文章
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- [LeetCode]1.Two Sum 两数之和&&第一次刷题感想
---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...
- 【LeetCode】Two Sum(两数之和)
这道题是LeetCode里的第1道题. 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会 ...
- [leetcode]1. Two Sum两数之和
Given an array of integers, return indices of the two numbers such that they add up to a specific t ...
- [LeetCode]1.Two Sum 两数之和(Java)
原题地址:two-sum 题目描述: 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每 ...
- 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 ...
- 【LeetCode】1. Two Sum 两数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...
- Leetcode:0002(两数之和)
LeetCode:0002(两数之和) 题目描述:给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表.你可以假设除了数字 0 之外,这两 ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
随机推荐
- privoxy+ss5实现 HTTP 代理协议转socks5代理
一.系统准备资源 二.ss5安装部署 1.SOCK5代理服务器部署环境准备 IP:10.0.0.100 官网: http://ss5.sourceforge.net/ 下载 yum - ...
- IOS开发学习笔记027-UITableView 使用模型对象
1.模型对象 2.单组数据的显示 1.模型对象 继续优化上一个程序 上一次用到字典,但是坏处多多.这里将这些数据封装到类中. 这就是MVC中得模型,模型就是数据的显示结构 新建一个类,添加几个属性和一 ...
- BugKu 杂项-这么多数据包
前边的都是些无关紧要,只要有点网络的基础我想应该都能懂,往下看,一直到NO104,这是在干什么? 源ip138一直向目标ip159发送syn握手包,想要建立连接,其实就是端口扫描,原理就是,想和你某个 ...
- springboot集成pagehelper插件
1.在pom.xml中引入依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifact ...
- 在控制器“xxxx”上找不到与该请求匹配的操作
Message:"找不到与请求 URI"http://localhost:8091/Api/CommonApi/SelectBind/GetBudCategoryListByCID ...
- Sentinel系统监控Redis主从节点
author:JevonWei 版权声明:原创作品 blog:http://119.23.52.191/ --- 构建Sentinel监控Redis的主节点架构 拓扑结构结构 拓扑环境 master ...
- 【bzoj1189】[HNOI2007]紧急疏散evacuate BFS最短路+动态加边网络流
题目描述 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一扇门,人们可以 ...
- ionic2 jpush
ionic2 为ionic2调用极光插件提供符合angular2及TS的调用方式 install 先安装官方的cordova插件 $ cordova plugin add jpush-phonegap ...
- root Android 模拟器
参考文档:http://blog.csdn.net/xbalien29/article/details/22661479 本文以2.3.3版本系统为目标. 一 准备工作 首先我们需要准备3样工具:su ...
- JavaScript Array 对象的方法,比如push和unshift
https://www.runoob.com/jsref/jsref-obj-array.html js数组与字符串的相互转换 一.数组转字符串 需要将数组元素用某个字符连接成字符串,示例代码如下: ...