当前位置:网站首页>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 下面做按数据结构开始做,先从数组开始
边栏推荐
- 【实战技能】单片机bootloader的CANFD,I2C,SPI和串口方式更新APP视频教程(2022-08-01)
- Real number rounding and writing to file (C language file)
- [R] Use grafify for statistical plotting, ANOVA, intervention comparisons, and more!
- PyTorch框架训练线性回归模型(CPU与GPU环境)
- leetcode 11. The container that holds the most water
- GameFi 行业下滑但未出局| June Report
- 基于php家具销售管理系统获取(php毕业设计)
- Golang GMP 原理
- An动画基础之元件的图形动画与按钮动画
- Image fusion GAN-FM study notes
猜你喜欢
基于php志愿者服务平台管理系统获取(php毕业设计)
An基本工具介绍之选择线条工具(包教会)
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
HCIP第十五天笔记(企业网的三层架构、VLAN以及VLAN 的配置)
[OpenCV] Book view correction + advertising screen switching Perspective transformation image processing
PyTorch builds a classification network model (Mnist dataset, fully connected neural network)
Graphic animation and button animation of an animation basic component
[R] Use grafify for statistical plotting, ANOVA, intervention comparisons, and more!
Oracle安装完毕(系统盘),从系统盘转移到数据盘
ECCV 2022 | AirDet: 无需微调的小样本目标检测方法
随机推荐
客户:我们系统太多,能不能实现多账号互通?
An动画基础之元件的影片剪辑效果
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 GMP 原理
An基本工具介绍之选择线条工具(包教会)
Sogou news-数据集
Golang sync.WaitGroup
Redis 6 的多线程
可重入锁详解(什么是可重入)
An动画优化之遮罩层动画
软件测试自学还是报班好?
技术分享 | 接口自动化测试如何搞定 json 响应断言?
软件测试面试(四)
字节最爱问的智力题,你会几道?
An动画优化之传统引导层动画
来广州找工作有一个多月了,今天终于有着落了,工资7000
【OpenCV】 书本视图矫正 + 广告屏幕切换 透视变换图像处理
Autumn recruitment work
安全自定义 Web 应用程序登录
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