当前位置:网站首页>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
边栏推荐
- easyui dialog“缓存问题”
- go wire 依赖注入入门
- See once to understand, graphic single chain table inversion
- Thinkphp6中where条件中字段与字段比较条件的写法
- WPF personal summary on drawing
- Idea - the. IML file was not automatically generated by the project
- [solution] distributed timing task solution
- Principles of websocket + probuf
- [original] the influence of arm platform memory and cache on the real-time performance of xenomai
- Judging whether paths intersect or not by leetcode
猜你喜欢
Got timeout reading communication packets解决方法
What do you think of the most controversial programming ideas?
计组-总线通信控制之异步串行通信的数据传输
Annual salary of 900000 programmers is not as good as 3800 civil servants a month? How to choose between stability and high income?
WPF 关于绘图个人总结
【原创】ARM平台内存和cache对xenomai实时性的影响
Count the frequency of letters in text (case insensitive)
你可能不知道的Animation动画技巧与细节
Ladongo open source full platform penetration scanner framework
Idea - the. IML file was not automatically generated by the project
随机推荐
Sentry 安装
ngnix集群高并发
These core technology of object-oriented, after you master it, you can have a good interview
Cpp(二) 创建Cpp工程
The software in your host has terminated an established connection. resolvent
Learn Scala if Else statement
Android Basics - RadioButton (radio button)
Go sending pin and email
Adobe media encoder /Me 2021软件安装包(附安装教程)
CPP (1) installation of cmake
awk实现类sql的join操作
洞察——风格注意力网络(SANet)在任意风格迁移中的应用
Sentry installation
easyui dialog“缓存问题”
Got timeout reading communication packets解决方法
【解决方案】分布式定时任务解决方案
Improvement of maintenance mode of laravel8 update
Problems of Android 9.0/p WebView multi process usage
手撕算法-手写单例模式
use Xunit.DependencyInjection Transformation test project