当前位置:网站首页>LeetCode·304竞赛·6132·使数组中所有元素都等于零·模拟·哈希
LeetCode·304竞赛·6132·使数组中所有元素都等于零·模拟·哈希
2022-07-31 13:22:00 【小迅想变强】
链接:https://leetcode.cn/problems/make-array-zero-by-subtracting-equal-amounts/solution/by-xun-ge-v-a5s6/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目
示例
思路
解题思路
- 暴力求解
题目需要我们寻找 最少 操作数,那么直接按题目给定要求进行模拟操作:
- 对元素进行升序处理,每次寻找第一个非0元素 x
- 选出一个正整数 x ,x 需要小于或等于 nums 中 最小 的 非零 元素。
- nums 中的每个正整数都减去 x。
- 减去会再升序处理
- 简易哈希表
再次读题目,可以发现,其实题目就等价于求数组中非0元素的个数,我们定义一个简易哈希表记录数组中不同元素的个数,最后相加即可
代码
暴力求解
int cmp(const void * a, const void * b)//升序
{
return *(int *)a - *(int *)b;
}
int minimumOperations(int* nums, int numsSize){
qsort(nums, numsSize,sizeof(int), cmp);//升序
int conut = 0;
for(int i = 0; i < numsSize; i++)//枚举整个数组
{
if(nums[i] == 0)//寻找第一个非0元素
{
continue;
}
conut++;
int m = nums[i];
for(int j = i; j < numsSize; j++)//nums 中的每个正整数都减去 x
{
nums[j] = nums[j] - m;
}
qsort(nums, numsSize,sizeof(int), cmp);//再升序
}
return conut;
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/make-array-zero-by-subtracting-equal-amounts/solution/by-xun-ge-v-a5s6/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
简易哈希表
int minimumOperations(int* nums, int numsSize){
int ans[101];
memset(ans, 0, sizeof(int) * 101);//初始化
for(int i = 0; i < numsSize; i++)//记录不同元素的个数
{
ans[nums[i]] = 1;
}
int conut = 0;
for(int i = 1; i < 101; i++)//累加
{
conut += ans[i];
}
return conut;
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/make-array-zero-by-subtracting-equal-amounts/solution/by-xun-ge-v-a5s6/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
时间空间复杂度
边栏推荐
- C#Assembly的使用
- C#控件StatusStrip使用
- STM32——软件SPI控制AD7705[通俗易懂]
- go中select语句
- C#使用NumericUpDown控件
- Samba 远程命令执行漏洞(CVE-2017-7494)
- 关于MySQL主从复制的数据同步延迟问题
- /run/NetworkManager占用空间过大
- ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
- PyQt5 rapid development and actual combat 9.7 Automated testing of UI layer
猜你喜欢
C#控件StatusStrip使用
Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
go使用makefile脚本编译应用
VU 非父子组件通信
Install the latest pytorch gpu version
golang-gin-pprof-使用以及安全问题
Error IDEA Terminated with exit code 1
AI cocoa AI frontier introduction (7.31)
C#高级--委托
C# control ToolStripProgressBar usage
随机推荐
STM32的CAN过滤器
深入浅出边缘云 | 4. 生命周期管理
聊聊 SAP 产品 UI 上的消息显示机制
Edge Cloud Explained in Simple Depth | 4. Lifecycle Management
Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
ECCV2022:在Transformer上进行递归,不增参数,计算量还少!
C# List用法 List介绍
C#获得网卡信息 NetworkInterface IPInterfaceProperties
The batch size does not have to be a power of 2!The latest conclusions of senior ML scholars
Architecture Camp | Module 8
查看Mysql数据库版本
Spark Learning: Add Custom Optimization Rules for Spark Sql
3.爬虫之Scrapy框架1安装与使用
模拟量差分和单端(iou计算方法)
PyQt5 rapid development and actual combat 10.2 compound interest calculation && 10.3 refresh blog clicks
The latest complete code: Incremental training using the word2vec pre-training model (two loading methods corresponding to two saving methods) applicable to various versions of gensim
C# 中的Async 和 Await 的用法详解
PartImageNet物体部件分割(Semantic Part Segmentation)数据集介绍
The use of C# control CheckBox
网络协议及相关技术详解