当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- sed之查找替换
- These core technology of object-oriented, after you master it, you can have a good interview
- Privacy violation and null dereference of fortify vulnerability
- easyui dialog“缓存问题”
- Face recognition: attack types and anti spoofing techniques
- Improvement of maintenance mode of laravel8 update
- C语言I博客作业03
- 团灭 LeetCode 股票买卖问题
- android基础-RadioButton(单选按钮)
- QT hybrid Python development technology: Python introduction, hybrid process and demo
猜你喜欢
Go之发送钉钉和邮箱
Got timeout reading communication packets解决方法
14000 word distributed transaction principle analysis, master all of them, are you afraid of being asked in the interview?
一次公交卡被“盗刷”事件带来的思考
laravel8更新之维护模式改进
Delphi10's rest.json And system.json Step on the pit
Hand tearing algorithm - handwritten singleton mode
Annual salary of 900000 programmers is not as good as 3800 civil servants a month? How to choose between stability and high income?
数据库基本操作
面部识别:攻击类型和反欺骗技术
随机推荐
Web Security (4) -- XSS attack
The emergence and significance of micro service
What kind of technical ability should a programmer who has worked for 1-3 years? How to improve?
leetcode之判断路径是否相交
About the promotion of the whole stack of engineers, from the introduction to give up the secret arts, do not click in to have a look?
来自不同行业领域的50多个对象检测数据集
CPP (2) creating CPP project
Delphi10's rest.json And system.json Step on the pit
关于晋升全栈工程师,从入门到放弃的神功秘籍,不点进来看一看?
Android 9.0/P WebView 多进程使用的问题
IDEA-项目未自动生成 .iml 文件
use Xunit.DependencyInjection Transformation test project
UCGUI简介
C / C + + Programming Notes: what are the advantages of C compared with other programming languages?
Sentry installation
Cpp(一) 安装CMake
GoLand writes a program with template
Problems of Android 9.0/p WebView multi process usage
Privacy violation and null dereference of fortify vulnerability
Awk implements SQL like join operation