当前位置:网站首页>leetcode-6132: Make all elements in array equal to zero
leetcode-6132: Make all elements in array equal to zero
2022-08-01 07:58:00 【chrysanthemum bat】
leetcode-6132:Makes all elements in the array equal to zero
题目
给你一个非负整数数组 nums .在一步操作中,你必须:
pick a positive integer x ,x 需要小于或等于 nums 中 最小 的 非零 元素.
nums Subtract each positive integer in x.
返回使 nums All elements in are equal 0 需要的 最少 操作数.
示例 1:
输入:nums = [1,5,0,3,5]
输出:3
解释:
第一步操作:选出 x = 1 ,之后 nums = [0,4,0,2,4] .
第二步操作:选出 x = 2 ,之后 nums = [0,2,0,0,2] .
第三步操作:选出 x = 2 ,之后 nums = [0,0,0,0,0] .
示例 2:
输入:nums = [0]
输出:0
解释:nums Every element in is already 0 ,So no action is required.

解题
方法一:模拟
由于nums.length最大为100,So you can use brute force simulation to do this problem
class Solution {
public:
int minimumOperations(vector<int>& nums) {
int n=nums.size();
int res=0;
for(int i=0;i<n;i++){
sort(nums.begin(),nums.end());
if(nums[i]==0) continue;
for(int j=n-1;j>=i;j--){
nums[j]-=nums[i];
}
res++;
}
return res;
}
};
方法二:转化为 Find the number of non-zero and distinct elements
class Solution {
public:
int minimumOperations(vector<int>& nums) {
unordered_set<int> set;
for(int num:nums){
if(num!=0) set.insert(num);
}
return set.size();
}
};
边栏推荐
- 套接字选项
- VSCode 快捷键及通用插件推荐
- 扁平数组转树结构实现方式
- Image lossless compression software which works: try completely free JPG - C image batch finishing compression reduces weight tools | latest JPG batch dressing tools download
- Chapter 9 of Huawei Deep Learning Course - Convolutional Neural Network and Case Practice
- pytest接口自动化测试框架 | 跳过模块
- 22牛客多校1 I. Chiitoitsu (概率dp)
- 372. 超级次方
- 2022.7.31-----leetcode.1161
- [Tear AHB-APB Bridge by hand]~ Why aren't the lower two bits of the AHB address bus used to represent the address?
猜你喜欢
随机推荐
179. 最大数
【手撕AHB-APB Bridge】~ AHB地址总线的低两位为什么不用来表示地址呢?
最小生成树
C语言学习概览(二)
图像基本操作的其他内容
pytest interface automation testing framework | single/multiple parameters
Pod环境变量和initContainer
JVM:运行时数据区-PC寄存器(程序计数器)
搜索框字符自动补全
pytest interface automation testing framework | parametrize source code analysis
USB Protocol (2) Terminology
Golang:go获取url和表单属性值
Holoview--Introduction
app 自动化 打开app (二)
基于tika实现对文件类型进行判断
LabVIEW RT中的用户界面更新速度
网络个各种协议
VSCode插件推荐(Rust环境)
JVM: Runtime Data Area - PC Register (Program Counter)
Data Analysis 6









