当前位置:网站首页>2020-11-07:已知一个正整数数组,两个数相加等于N并且一定存在,如何找到两个数相乘最小的两个数?
2020-11-07:已知一个正整数数组,两个数相加等于N并且一定存在,如何找到两个数相乘最小的两个数?
2020-11-07 23:08:00 【福大大架构师每日一题】
福哥答案2020-11-07:
1.哈希法。 2.排序+双指针夹逼。
golang代码如下:
package main
import (
"fmt"
"sort"
)
const INT_MAX = int(^uint(0) >> 1)
func main() {
nums := []int{2, 1, 3, 4, 5, 6, 9, 8, 7}
fmt.Println(twoSumMultiplication1(nums, 12), "哈希法")
fmt.Println(twoSumMultiplication2(nums, 12), "排序+双指针夹逼")
}
//哈希法
func twoSumMultiplication1(nums []int, target int) int {
map0 := make(map[int]struct{})
min := INT_MAX
for i := 0; i < len(nums); i++ {
complement := target - nums[i] //差值 = 目标值-元素值
if _, ok := map0[complement]; ok { //如果字典里有差值,说明已经找到了
//确保complement是较小的那个值
if complement > nums[i] {
complement, nums[i] = nums[i], complement
}
//谁小保存谁
if complement < min {
min = complement
//如果最小值是1,就不用循环了。
if min == 1 {
break
}
}
} else {
//如果字典里没有差值,缓存数组的当前值
map0[nums[i]] = struct{}{}
}
}
return min
}
//排序+双指针夹逼
func twoSumMultiplication2(nums []int, target int) int {
//排序
sort.Slice(nums, func(i, j int) bool {
return nums[i] < nums[j]
})
sumtemp := 0
min := INT_MAX
for i, j := 0, len(nums)-1; i < j; {
sumtemp = nums[i] + nums[j]
if target == sumtemp {
if min > nums[i] {
min = nums[i]
if min == 1 {
break
}
}
i++
} else if target > sumtemp {
i++
} else {
j--
}
}
return min
}
执行结果如下:
版权声明
本文为[福大大架构师每日一题]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4553401/blog/4707758
边栏推荐
- WPF personal summary on drawing
- Analysis of kubernetes service types: from concept to practice
- 16. File transfer protocol, vsftpd service
- Cpp(三) 什么是CMake
- GoLand writes a program with template
- ngnix集群高并发
- Git code submission operation, and git push prompt failed to push some refs'xxx '
- The software in your host has terminated an established connection. resolvent
- A compilation bug brought by vs2015 Update1 update [existing solutions]
- 14000 word distributed transaction principle analysis, master all of them, are you afraid of being asked in the interview?
猜你喜欢
GoLand writes a program with template
Go sending pin and email
Annual salary of 900000 programmers is not as good as 3800 civil servants a month? How to choose between stability and high income?
wanxin金融
[C + + learning notes] how about the simple use of the C + + standard library STD:: thread?
What do you think of the most controversial programming ideas?
来自不同行业领域的50多个对象检测数据集
Hand tearing algorithm - handwritten singleton mode
Summary of the resumption of a 618 promotion project
WPF 关于绘图个人总结
随机推荐
supervisor进程管理安装使用
Ladongo open source full platform penetration scanner framework
使用 Xunit.DependencyInjection 改造测试项目
C语言I博客作业03
Wanxin Finance
High concurrency in ngnix cluster
Got timeout reading communication packets解决方法
CPP (4) boost installation and basic use for Mac
Cpp(一) 安装CMake
Assembly function MCALL systemstack asmcgocal system call
微服务的出现和意义的探索
Sentry installation
Analysis of kubernetes service types: from concept to practice
Problems of Android 9.0/p WebView multi process usage
一次公交卡被“盗刷”事件带来的思考
Adobe media encoder / me 2021 software installation package (with installation tutorial)
Basic operation of database
Data transmission of asynchronous serial communication controlled by group bus communication
虚拟DOM中给同一层级的元素设置固定且唯一的key为什么能提高性能
关于晋升全栈工程师,从入门到放弃的神功秘籍,不点进来看一看?