当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 密码学-尚硅谷
- 关于update操作并发问题
- 2020-11-07:已知一个正整数数组,两个数相加等于N并且一定存在,如何找到两个数相乘最小的两个数?
- Wechat applet request reported 400 error @ requestbody failed to receive
- 【原创】ARM平台内存和cache对xenomai实时性的影响
- ROS学习---远程启动ROS节点
- 微服务的出现和意义的探索
- Annual salary of 900000 programmers is not as good as 3800 civil servants a month? How to choose between stability and high income?
- UCGUI简介
- Insight -- the application of sanet in arbitrary style transfer
猜你喜欢
随机推荐
【解决方案】分布式定时任务解决方案
Data structure and sorting algorithm
ubuntu实时显示cpu、内存占用率
Adobe Prelude /Pl 2020软件安装包(附安装教程)
supervisor和Python多进程multiprocessing使用 子进程残留问题
Cpp(三) 什么是CMake
[solution] distributed timing task solution
Web安全(三)---CSRF攻击
你可能不知道的Animation动画技巧与细节
2020-11-07:已知一个正整数数组,两个数相加等于N并且一定存在,如何找到两个数相乘最小的两个数?
Privacy violation and null dereference of fortify vulnerability
Improvement of maintenance mode of laravel8 update
来自不同行业领域的50多个对象检测数据集
Problems of Android 9.0/p WebView multi process usage
Static + code block + polymorphism + exception
构造请求日志分析系统
GET,POST,PUT,DELETE,OPTIONS用法与说明
汇编函数mcall systemstack asmcgocall syscall
Animation techniques and details you may not know
Adobe Lightroom / LR 2021 software installation package (with installation tutorial)