当前位置:网站首页>Week 304 Dunk
Week 304 Dunk
2022-08-02 03:01:00 【Jack Ju】
1.Introduction
Do less code topic,Can't fall asleep today,I think she's right increasingly,我思考了下,Indeed there is a big probability,For ten years to do goal,There is a strong possibility cannot achieve.So did a few problem is reluctantly let oneself of mind.Sipping kraal mountain strong spirit,一边写Leetcode题目,还好第 304 Field force buckles weeks "the first is a simple question,Otherwise can't sleep tonight,But to admit that you do dishes,哈哈哈哈哈哈哈哈.
2.The description of Problems
- 使数组中所有元素都等于零
给你一个非负整数数组 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 ,所以不需要执行任何操作.
提示:
1 <= nums.length <= 100
0 <= nums[i] <= 100
3.My Solution
class Solution {
public:
int minimumOperations(vector<int>& nums) {
int count = 0;
int flag = 1;
int subflag = 1;
for(int i =0;i<nums.size();i++){
if(nums[i]>0){
subflag = 0;
}
}
if(subflag){
return 0;
}
while(flag){
flag = 0;
int min = 10000;
for(int i=0;i<nums.size();i++){
if(nums[i]<min && nums[i]!=0){
min = nums[i];
}
}
for(int i = 0;i<nums.size();i++){
if(nums[i]>0)
nums[i]-= min;
}
for(int i = 0;i<nums.size();i++){
if(nums[i] != 0){
flag = 1;
break;
}
}
count++;
}
return count;
}
};
边栏推荐
猜你喜欢
随机推荐
Duplicate entry ‘XXX‘ for key ‘XXX.PRIMARY‘解决方案。
消息队列经典十连问
架构:分布式任务调度系统(SIA-Task)简介
"Paid paddling" stealthily brushes Brother Ali's face scriptures, challenges bytes three times, and finally achieves positive results
亲身经历过的面试题
AcWing 1053. Repair DNA problem solution (state machine DP, AC automata)
JSP Webshell free kill
Recursively check if a configuration item has changed and replace it
MySQL索引优化实战
VPS8702 VPSC(源特科技)电源管理(PMIC) 封装SOT23-6
Chrome浏览器无法加载已解压的.crx文件的解决办法
给你一个大厂面试的机会,你能面试上吗?进来看看!
JS中获取对象数据类型的键值对的键与值
WebShell connection tools (Chinese kitchen knife, WeBaCoo, Weevely) use
Nacos source code analysis topic (1) - environment preparation
总体写作原则
Flask入门学习教程
Common SQL interview questions: 50 classic examples
PAT甲级打卡-1001-1004
MySQL8.0.28安装教程