当前位置:网站首页>November 07, 2020: given an array of positive integers, the sum of two numbers equals N and must exist. How to find the two numbers with the smallest multiplication?
November 07, 2020: given an array of positive integers, the sum of two numbers equals N and must exist. How to find the two numbers with the smallest multiplication?
2020-11-07 23:08:00 【Fuda Dajia architect's daily question】
Fogo's answer 2020-11-07:
1. Hashifa . 2. Sort + Double pointer pinch .
golang The code is as follows :
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), " Hashifa ")
fmt.Println(twoSumMultiplication2(nums, 12), " Sort + Double pointer pinch ")
}
// Hashifa
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] // Difference value = The target - Element value
if _, ok := map0[complement]; ok { // If there is a difference in the dictionary , It means that we have found
// Make sure complement It's the smaller value
if complement > nums[i] {
complement, nums[i] = nums[i], complement
}
// Who is small and who keeps it
if complement < min {
min = complement
// If the minimum is 1, You don't have to cycle .
if min == 1 {
break
}
}
} else {
// If there is no difference in the dictionary , Cache the current value of the array
map0[nums[i]] = struct{}{}
}
}
return min
}
// Sort + Double pointer pinch
func twoSumMultiplication2(nums []int, target int) int {
// Sort
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
}
The results are as follows : 
版权声明
本文为[Fuda Dajia architect's daily question]所创,转载请带上原文链接,感谢
边栏推荐
- Web Security (1) -- browser homology strategy
- 面部识别:攻击类型和反欺骗技术
- More than 50 object detection datasets from different industries
- These core technology of object-oriented, after you master it, you can have a good interview
- 密码学-尚硅谷
- Cpp(二) 创建Cpp工程
- GET,POST,PUT,DELETE,OPTIONS用法与说明
- Web安全(三)---CSRF攻击
- Face recognition: attack types and anti spoofing techniques
- [original] the influence of arm platform memory and cache on the real-time performance of xenomai
猜你喜欢

Delphi10's rest.json And system.json Step on the pit

LadonGo开源全平台渗透扫描器框架

Download, installation and configuration of Sogou input method in Ubuntu

获取树形菜单列表

What kind of technical ability should a programmer who has worked for 1-3 years? How to improve?

laravel8更新之维护模式改进

Go之发送钉钉和邮箱

C/C++编程笔记:C语言相比其他编程语言,有什么不一样的优势?
![A compilation bug brought by vs2015 Update1 update [existing solutions]](/img/3b/00bc81122d330c9d59909994e61027.jpg)
A compilation bug brought by vs2015 Update1 update [existing solutions]

C language I blog assignment 03
随机推荐
Getting started with go wire dependency injection
laravel8更新之维护模式改进
Qt混合Python开发技术:Python介绍、混合过程和Demo
[C + + learning notes] how about the simple use of the C + + standard library STD:: thread?
0.计算机简史
Download, installation and configuration of Sogou input method in Ubuntu
密码学-尚硅谷
来自不同行业领域的50多个对象检测数据集
Go之发送钉钉和邮箱
面部识别:攻击类型和反欺骗技术
到底选openstack还是vmware?
汇编函数mcall systemstack asmcgocall syscall
Supervisor process management installation and use
面部识别:攻击类型和反欺骗技术
Ubuntu下搜狗输入法的下载安装及配置
关于update操作并发问题
What kind of technical ability should a programmer who has worked for 1-3 years? How to improve?
虚拟DOM中给同一层级的元素设置固定且唯一的key为什么能提高性能
关于晋升全栈工程师,从入门到放弃的神功秘籍,不点进来看一看?
2020天翼智能生态博览会中国电信宣布5G SA正式规模商用