当前位置:网站首页>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 下面做按数据结构开始做,先从数组开始
边栏推荐
- An animation basic element movie clip effect
- How to build an overseas purchasing system/purchasing website - source code analysis
- Database basics one (MySQL) [easy to understand]
- 【深度学习】高效轻量级语义分割综述
- 为冲销量下探中低端市场,蔚来新品牌产品定价低至10万?
- 实数取整写入文件(C语言文件篇)
- An animation based button animation combined with basic code
- PyTorch框架训练线性回归模型(CPU与GPU环境)
- Sogou news-数据集
- 【实战技能】单片机bootloader的CANFD,I2C,SPI和串口方式更新APP视频教程(2022-08-01)
猜你喜欢
An基本工具介绍之选择线条工具(包教会)
ECCV 2022|通往数据高效的Transformer目标检测器
Station B responded that "HR said that core users are all Loser": the interviewer was persuaded to quit at the end of last year and will learn lessons to strengthen management
An animation optimization of shape tween and optimization of traditional tweening
IDEA的模板(Templates)
类和对象(中上)
leetcode16最接近的三数之和 (排序+ 双指针)
PyTorch framework to train linear regression model (CPU and GPU environment)
期货公司开户关注的关键点
In order to counteract the drop in sales and explore the low-end market, Weilai's new brand products are priced as low as 100,000?
随机推荐
OpenCV perspective transform
基于php网上零食商店管理系统获取(php毕业设计)
An introduction to the skeleton tool
An动画优化之补间形状与传统补间的优化
Golang strings
Grafana 高可用部署最佳实践
How to make the history record time-stamped before
An animation based button animation combined with basic code
Golang 互斥锁
In order to counteract the drop in sales and explore the low-end market, Weilai's new brand products are priced as low as 100,000?
浅谈低代码平台远程组件加载方案
Golang 通道 channel
Last blog for July
Redis connection pool tool class
SQL分页查询_Sql根据某个字段分页
Golang interface interface
Golang 数组和切片
Tinymce plugins [Tinymce扩展插件集合]
Database basics one (MySQL) [easy to understand]
Key points for account opening of futures companies