当前位置:网站首页>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
}
}
边栏推荐
- 学生如何申请免费IDEA
- Usage and common problems of SIP softphone registered with SIP account
- Yuancosmos provides a digital social platform for fashion design display
- Lesson 2 getting to know slam for the first time
- LNMP架构
- Debugging sharp weapon! A lightweight log library log.c
- 动态内存管理及柔性数组
- Chinese character style transfer --- learn the conversion and generation of one to many programmed Chinese characters through generation confrontation network
- Data warehouse construction -dim floor
- SIP账号注册的SIP软电话的使用和常见问题
猜你喜欢

Thread三种实现方式 和 Handler的用法

【个人总结】2022.7.24周结

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

LNMP架构

FTP实验及概述

LNMP architecture

开发项目事半功倍,一款开源的stm32驱动库大集合

Another open source artifact, worth collecting and learning!

MBA-day29 算术-绝对值初步认识

Attack and defense world flatscience
随机推荐
kubernetes install completed
You'd better not take this kind of project!
Day011 一维数组
Redis official visualization tool, with high appearance value and powerful functions!
Usage and common problems of SIP softphone registered with SIP account
Yuancosmos provides a digital social platform for fashion design display
C语言详解系列——函数的认识(4)函数的声明与定义,简单练习题
C language explanation series - understanding of functions (4) declaration and definition of functions, simple exercises
Getaverse, a distant bridge to Web3
TZC 1283: simple sort Bubble Sort
Shell read read console input, use of read
Unity Profiler
520送什么?DIY一个高颜值RGB时钟,女生看了都想要
Debugging sharp weapon! A lightweight log library log.c
Hack the box -sql injection fundamentals module detailed Chinese tutorial
ES Cluster in Red status: what about write & delete operations?
jupyter notebook快捷键
Knowledge points of Polymer Physics
How to handle aggregate collection code
怎么办理聚合收款码