当前位置:网站首页>6132. All the elements in the array is equal to zero - quick sort method
6132. All the elements in the array is equal to zero - quick sort method
2022-08-01 23:10:00 【Mr Gao】
6132. 使数组中所有元素都等于零
给你一个非负整数数组 nums .在一步操作中,你必须:
选出一个正整数 x ,x 需要小于或等于 nums 中 最小 的 非零 元素.
nums 中的每个正整数都减去 x.
返回使 nums 中所有元素都等于 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 中的每个元素都已经是 0 ,所以不需要执行任何操作.
void quick_sort(int *a,int low,int high){
int l=low,h=high;
if(low<high){
int p=a[low];
while(low<high){
while(low<high&&a[high]>=p){
high--;
}
a[low]=a[high];
while(low<high&&a[low]<=p){
low++;
}
a[high]=a[low];
}
a[low]=p;
quick_sort(a,l,low-1);
quick_sort(a,low+1,h);
}
}
int minimumOperations(int* nums, int numsSize){
quick_sort(nums,0,numsSize-1);
int target=nums[0];
int count=0;
int i;
for(i=1;i<numsSize;i++){
if(nums[i]!=target){
count++;
target=nums[i];
}
}
// printf("%d ",count);
if(nums[0]!=0){
count=count+1;
}
return count;
}
边栏推荐
- Chapter 12 End-User Task As Shell Scripts
- 检查点是否在矩形内
- drf生成序列化类代码
- vscode hide menu bar
- Postman batch test interface detailed tutorial
- 10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
- PostgreSQL Basics--Common Commands
- System availability: 3 9s, 4 9s in SRE's mouth... What is it?
- Nacos配置中心之加载配置
- 如何使用pywinauto和pyautogui将动漫小姐姐链接请回家
猜你喜欢
Wechat Gymnasium Reservation Mini Program Graduation Design Finished Work Mini Program Graduation Design Finished Product (2) Mini Program Function
C语言——分支语句和循环语句
13、学习MySQL 分组
系统可用性:SRE口中的3个9,4个9...到底是个什么东西?
img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)
联邦学习在金融领域的发展和应用
请问什么是 CICD
System availability: 3 9s, 4 9s in SRE's mouth... What is it?
JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method
PHP算法之电话号码的字母组合
随机推荐
Avoid hidden text when loading fonts
excel change cell size
IDEA common plugins
Graph Theory - Strongly Connected Component Condensation + Topological Sort
IDEA常用插件
How to use pywinauto and pyautogui to link the anime lady and sister please go home
加载字体时避免隐藏文本
sys_kill系统调用
y84.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十五)
Prufer sequence
Calculate the angle of a line defined by two points
联邦学习入门
excel vertical to horizontal
visual studio code multiple editing
数据分析04
如何使用pywinauto和pyautogui将动漫小姐姐链接请回家
研发团队数字化转型实践
Always use "noopener" or "noreferrer" for links that open in a new tab
毕业作业
Chapter 19 Tips and Traps: Common Goofs for Novices