当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Awk implements SQL like join operation
- 洞察——风格注意力网络(SANet)在任意风格迁移中的应用
- golang 匿名结构体成员,具名结构体成员,继承,组合
- Adobe Prelude / PL 2020 software installation package (with installation tutorial)
- Web安全(四)---XSS攻击
- Problems of Android 9.0/p WebView multi process usage
- 京淘项目知识点总结
- 16. File transfer protocol, vsftpd service
- easyui dialog“缓存问题”
- Stack bracket matching
猜你喜欢

laravel8更新之维护模式改进

微信小程序request报400错误 @RequestBody接收不到

【原创】ARM平台内存和cache对xenomai实时性的影响

Qt混合Python开发技术:Python介绍、混合过程和Demo

云计算之路-出海记:整一台 aws 免费云服务器

14000 word distributed transaction principle analysis, master all of them, are you afraid of being asked in the interview?

手撕算法-手写单例模式

Download, installation and configuration of Sogou input method in Ubuntu

Reflection on a case of bus card being stolen and swiped

LadonGo开源全平台渗透扫描器框架
随机推荐
虚拟DOM中给同一层级的元素设置固定且唯一的key为什么能提高性能
Judging whether paths intersect or not by leetcode
C/C++编程笔记:C语言相比其他编程语言,有什么不一样的优势?
What magic things can a line of Python code do?
Basic operation of database
Jingtao project day09
汇编函数mcall systemstack asmcgocall syscall
Web安全(二)---跨域资源共享
WPF personal summary on drawing
关于晋升全栈工程师,从入门到放弃的神功秘籍,不点进来看一看?
一万四千字分布式事务原理解析,全部掌握你还怕面试被问?
C++在C的基础上改进了哪些细节
Awk implements SQL like join operation
wanxin金融
0.计算机简史
Static + code block + polymorphism + exception
Web Security (3) -- CSRF attack
【解决方案】分布式定时任务解决方案
Go sending pin and email
Search and replace of sed