当前位置:网站首页>2022.07.31(LC_6132_使数组中所有元素都等于零)
2022.07.31(LC_6132_使数组中所有元素都等于零)
2022-08-02 06:18:00 【Leeli9316】
方法一:优先队列
class Solution {
public int minimumOperations(int[] nums) {
Queue<Integer> pq = new PriorityQueue<>();
for (int num : nums) {
pq.offer(num);
}
int ans = 0;
while (!pq.isEmpty()) {
int min = pq.poll();
if (min != 0) {
List<Integer> list = new ArrayList<>();
while (!pq.isEmpty()) {
list.add(pq.poll() - min);
}
for (int num : list) {
pq.offer(num);
}
ans++;
}
}
return ans;
}
}
方法二:哈希表
class Solution {
public int minimumOperations(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
if (num != 0) {
set.add(num);
}
}
return set.size();
}
}
边栏推荐
- nacos源码启动找不到istio包
- Nodejs installation and global configuration (super detailed)
- MySQL high-level --- storage engine, index, lock
- 有人开源全凭“为爱发电”,有人却用开源“搞到了钱”
- 返回文件名问题
- MySql -- 不存在则插入,存在则更新或忽略
- 2022年7月18日-7月31日(Ue4视频教程和文档,20小时。合计1412小时,剩8588小时)
- MySql 5.7.38 download and installation tutorial, and realize the operation of MySql in Navicat
- See the picture to understand | How to choose sales indicators to measure the health of business growth
- love
猜你喜欢
Specified URL is not reachable,caused by :‘Read timed out
Revitalize rural circular economy and digital chain to link agricultural "ecological chain"
推出 Space On-Premises (本地部署版) Beta 版!
实例032:反向输出II
chrome 插件开发指南
File upload vulnerability (2)
数据库概论之MySQL表的增删改查2
Nodejs installation tutorial
[Dataset][VOC] Eyewear dataset 6000 in VOC format
SphereEx苗立尧:云原生架构下的Database Mesh研发实践
随机推荐
Servlet
数据库概论-MySQL的数据表的基本操作
MySQL 23道经典面试吊打面试官
Launch Space on-premises deployment (local) Beta!
【论文精读】Geometric Structure Preserving Warp for Natural Image Stitching
Project development specification
MySQL Advanced Statements (1)
享年94岁,图灵奖得主、计算复杂性理论先驱Juris Hartmanis逝世
MySQL Advanced Statements (1)
chrome plugin development guide
July 18-July 31, 2022 (Ue4 video tutorials and documentation, 20 hours. Total 1412 hours, 8588 hours left)
SphereEx苗立尧:云原生架构下的Database Mesh研发实践
实例027:递归输出
【暑期每日一题】洛谷 P3156 【深基15.例1】询问学号
关于ue4.27像素流送打包后的本地服务器问题
Unity Shader学习(七)纹理图像的简单使用
MySQL Advanced - MVCC (ultra-detailed finishing)
有人开源全凭“为爱发电”,有人却用开源“搞到了钱”
能与观众实时互动的Claper
Resolving C# non-static field, method or property "islandnum.Program.getIslandCount(int[][], int, int)" requires an object reference