当前位置:网站首页>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
}
}
边栏推荐
- Polymer physics test question bank
- A trick to teach you to easily understand Potter's map
- 517. 超级洗衣机
- Flex layout principle and common parent elements
- 这种项目,最好别接!
- Is it really hopeless to choose electronic engineering and be discouraged?
- No EGL Display 报错解决
- 秋招-准备计划
- Chapter 1 - Construction of development environment
- MBA-day29 算术-绝对值初步认识
猜你喜欢

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

CANoe-XML在Test Modules中的应用

Do you really understand fiddler, a necessary tool for testing?

How to understand "array name is essentially an address" from the perspective of memory parsing?

FTP实验及概述

SSH Remote Management

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

SSH远程管理

高分子物理知识点

Basic methods of realizing licensing function in C language
随机推荐
Hack the box - Web requests module detailed Chinese tutorial
高分子物理知识点
Mongodb common commands
Redis发布订阅
520 for what? DIY is a high-value RGB clock that girls want to watch
数仓搭建-DIM层
Use latex to typeset multiple-choice test paper
10. Regular expression matching
[personal summary] end of July 24, 2022
Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
Circular structure practice
SSTI payload and various bypass methods
I also found excellent software and hardware projects, all open source
YOLOV3预备工作
Nn.moudle module - details of creating neural network structure
DOM操作--操作节点
Dynamic memory management and flexible array
Development to testing: a six-year road to automation from scratch
Common modules in ansible
87. 扰乱字符串