当前位置:网站首页>leetcode 448. Find All Numbers Disappeared in an Array 找到所有数组中消失的数字(简单)
leetcode 448. Find All Numbers Disappeared in an Array 找到所有数组中消失的数字(简单)
2022-08-03 12:53:00 【InfoQ】
一、题目大意
- n == nums.length
- 1 <= n <= 105
- 1 <= nums[i] <= n
- 进阶:你能在不使用额外空间且时间复杂度为 O(n) 的情况下解决这个问题吗? 你可以假定返回的数组不算在额外空间内。
二、解题思路
三、解题方法
3.1 Java实现
public class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
for (int num : nums) {
// 这个地方要注意 -1,数组下标从0开始
int pos = Math.abs(num) - 1;
if (nums[pos] > 0) {
nums[pos] = -nums[pos];
}
}
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
ans.add(i + 1);
}
}
return ans;
}
}
四、总结小记
- 2022/8/3 下面做按数据结构开始做,先从数组开始
边栏推荐
- Basic principle of the bulk of the animation and shape the An animation tip point
- An animation basic element movie clip effect
- 欧曼自动挡、银河大马力、行星新产品 欧曼全新产品以燎原之势赢领市场
- [R] Use grafify for statistical plotting, ANOVA, intervention comparisons, and more!
- The components of the basis of An animation movie clip animation between traditional filling
- 利用pgsql插件PostGIS 实现地理坐标系数据转换
- 如何让history历史记录前带时间戳
- 【实战技能】单片机bootloader的CANFD,I2C,SPI和串口方式更新APP视频教程(2022-08-01)
- An introduction to the width tool, deformation tool and lasso tool
- JS获得浏览器类型
猜你喜欢

汉源高科G8032标准ERPS环网交换机千兆4光10电工业以太网交换机环网+WEB管理+SNMP划VLAN

【OpenCV】 书本视图矫正 + 广告屏幕切换 透视变换图像处理

setTimeout, setInterval requestAnimationFrame

Hanyuan Hi-Tech G8032 standard ERPS ring network switch Gigabit 4 optical 10 electrical industrial Ethernet switch ring network + WEB management + SNMP VLAN planning

云计算服务主要安全风险及应对措施初探

什么是分布式锁?几种分布式锁分别是怎么实现的?

An introduction to the camera

An动画基础之元件的图形动画与按钮动画

漫画:怎么证明sleep不释放锁,而wait释放锁?

GameFi industry down but not out | June Report
随机推荐
An introduction to the pen tool, pencil tool and brush tool
[Microservice] Multi-level cache
Golang 字符串
Multithreading in Redis 6
Database basics one (MySQL) [easy to understand]
欧曼自动挡、银河大马力、行星新产品 欧曼全新产品以燎原之势赢领市场
Golang 互斥锁
An animation basic element movie clip effect
Redis connection pool tool class
PyTorch构建分类网络模型(Mnist数据集,全连接神经网络)
滑动窗口的最大值
How to disable software from running in the background in Windows 11?How to prevent apps from running in the background in Windows 11
GameFi industry down but not out | June Report
[微服务]多级缓存
An introduction to the camera
SQL分页查询_Sql根据某个字段分页
查看GCC版本_qt版本
BOM系列之sessionStorage
Image fusion GAN-FM study notes
The common problems in the futures account summary