当前位置:网站首页>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
边栏推荐
- 使用jsDelivr加速你的网站
- Animation techniques and details you may not know
- These core technology of object-oriented, after you master it, you can have a good interview
- use Xunit.DependencyInjection Transformation test project
- Problems of Android 9.0/p WebView multi process usage
- It's time to end bertology
- supervisor进程管理安装使用
- Awk implements SQL like join operation
- C language I blog assignment 03
- Do not understand the underlying principle of database index? That's because you don't have a B tree in your heart
猜你喜欢

京淘项目day09

Animation techniques and details you may not know

Reflection on a case of bus card being stolen and swiped

关于晋升全栈工程师,从入门到放弃的神功秘籍,不点进来看一看?

GoLand writes a program with template

Insight -- the application of sanet in arbitrary style transfer

Jingtao project day09
![[original] the influence of arm platform memory and cache on the real-time performance of xenomai](/img/cb/0395507ece572556b2bab373f013a5.jpg)
[original] the influence of arm platform memory and cache on the real-time performance of xenomai

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

微服务的出现和意义的探索
随机推荐
洞察——风格注意力网络(SANet)在任意风格迁移中的应用
CPP (3) what is cmake
使用 Xunit.DependencyInjection 改造测试项目
Basic operation of database
Implementation of Caesar cipher
IDEA-项目未自动生成 .iml 文件
Assembly function MCALL systemstack asmcgocal system call
Speed up your website with jsdelivr
High concurrency in ngnix cluster
Face recognition: attack types and anti spoofing techniques
On the stock trading of leetcode
Awk implements SQL like join operation
Go之发送钉钉和邮箱
android基础-RadioButton(单选按钮)
static+代码块+多态+异常
Principles of websocket + probuf
Data transmission of asynchronous serial communication controlled by group bus communication
Android 9.0/P WebView 多进程使用的问题
sed之查找替换
Design pattern of facade and mediator
