当前位置:网站首页>leetcode-Array
leetcode-Array
2022-07-26 05:36:00 【CodeStars码星人】
Link
思路:
- nums中的数要是能整除numsDivide中的所有数,那么,其满足的条件就是整除nunsDivide中的最小公约数
- 所以需要求numsDivide的最小公约数,需要用到辗转相乘法
int gcb(int a,int b){
return a==0?b:gcb(b%a,a);
//举个例子,就能简单推出
//比如,4和18的最小公约数,拿18除4的余数(为2),然后拿余数2继续跟4进行相同的运算,gcb(2,4),然后会发现这一次的4%2为0,所以就返回2,也即返回b
}
3.这道题真正要考的地方在于(理解了题意后转化为:
1.循环nums中的每个数,找到最小的能够整除于最小公约数的数k
2.然后再遍历循环一次,这次找到所有比k小的数
class Solution {
public int minOperations(int[] nums, int[] numsDivide) {
//这道题重要的点在于求最小公约数的辗转相除法
//nums中的数要是能整除numsDivide中的所有数,那么,其满足的条件就是整除nunsDivide中的最小公约数
//所以先用辗转相除法求numsDivide中的最小公约数
int p=0;
for(int numsdivide:numsDivide){
p=gcb(p,numsdivide);
}
int res=Integer.MAX_VALUE;
int sum=0;
//找到最小能够整除p的数
for(int i:nums){
if(p%i==0){
res=Math.min(res,i);
}
}
if(res==Integer.MAX_VALUE)return -1;
for(int i:nums){
if(i<res)sum++;
}
return sum;
}
int gcb(int a,int b){
return a==0?b:gcb(b%a,a);
//举个例子,就能简单推出
//比如,4 和18的最小公约数,拿18除4的余数,然后拿余数继续跟4进行相同的运算,直到余数为0,也就是上面的a=0,就返回b 比如4%2和2 4%2为0了,就返回2,也即返回b
}
}
边栏推荐
- 芯片的翻新和造假,人被坑麻了
- Hack The Box - Web Requests Module详细讲解中文教程
- Qt编写物联网管理平台47-通用数据库设置
- 调试利器!一款轻量级日志库 log.c
- High frequency electronic circuit review examination questions and answers
- Lightweight MCU command line interaction project, all open source
- Real scientific weight loss
- LNMP架构
- SQL injection
- ES Cluster in Red status: what about write & delete operations?
猜你喜欢

OD-Paper【1】:Rich feature hierarchies for accurate object detection and semantic segmentation

Hack The Box - Web Requests Module详细讲解中文教程

Polymer physics test question bank

Hack The Box - Introduction to Networking Module详细讲解中文教程

Hack The Box -SQL Injection Fundamentals Module详细讲解中文教程

高效,可靠,安全的串口通讯开源方案

选电子工程被劝退,真的没前景了?

电机控制专栏文章汇总

Benji Bananas 开启第二季边玩边赚奖励活动,支持使用多张 Benji 通行证!

Redis official visualization tool, with high appearance value and powerful functions!
随机推荐
Getaverse, a distant bridge to Web3
Flex layout principle and common parent elements
Code audit CMS
中文文本纠错任务简介
DOM operation -- operation node
Redis事务
Three graduates, three years of experience in embedded software
How to conduct test case review
Go exceed API source code reading (VI) -- deletesheet (sheet string)
87. 扰乱字符串
攻防世界-FlatScience
新人如何做好功能测试,学会这几项够用了
517. Super washing machine
MBA-day28 数的概念-练习题
If MySQL calculates the current month change / current month increase / year-on-year change / year-on-year increase?
Hack The Box - Introduction to Networking Module详细讲解中文教程
High frequency electronic circuit review examination questions and answers
FPGA question brushing sequence detection
Redis 官方可视化工具,高颜值,功能真心强大!
Use playbook in ansible