当前位置:网站首页>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();
}
};
边栏推荐
- 【ASWC Arxml结构分解】-7-Explicit(显式)和Implicit(隐式) Sender-Receiver communication描述差异
- Shell执行SQL发邮件
- zip package all files in the directory (including hidden files/folders)
- LeetCode 415:字符串相加
- leetcode-6133:分组的最大数量
- pytest接口自动化测试框架 | 跳过测试类
- Golang:go连接和使用mysql
- 小程序通过云函数操作数据库【使用get取数据库】
- Go 支持 OOP: 用 struct 代替 class
- The log causes these pits in the thread block, you have to prevent
猜你喜欢
小程序全面屏手势配置案例
日志导致线程Block的这些坑,你不得不防
special day to remember
USB 协议 (二) 术语
Vim扩展内容
特殊的日子,值得纪念
Image lossless compression software which works: try completely free JPG - C image batch finishing compression reduces weight tools | latest JPG batch dressing tools download
配置我的kitty
LeetCode 415:字符串相加
Data Analysis 6
随机推荐
LeetCode240+312+394
Delphi MDI appliction 文档最大化显示、去掉最大化最小化等按钮
走进音视频的世界——mp3封装格式
C语言学习概览(一)
将aof文件转换为命令waoffle安装和使用
Image lossless compression software which works: try completely free JPG - C image batch finishing compression reduces weight tools | latest JPG batch dressing tools download
pytest接口自动化测试框架 | 跳过测试类
USB Protocol (2) Terminology
pytest接口自动化测试框架 | 使用函数返回值的形式传入参数值
How to use Photoshop to composite star trail photos, post-processing method of night sky star trail photos
网络基础学习
Pod环境变量和initContainer
数据分析5
【STM32】入门(二):跑马灯-GPIO端口输出控制
Do I need to introduce any dependencies to write data to clickhouse using flinksql?
JVM内存模型之深究模型特征
The log causes these pits in the thread block, you have to prevent
pytest interface automation testing framework | pass in parameter values in the form of function return values
mysql查看cpu使用情况
小程序更多的手势事件(左右滑动、放大缩小、双击、长按)