当前位置:网站首页>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 下面做按数据结构开始做,先从数组开始
边栏推荐
- The components of the basis of An animation movie clip animation between traditional filling
- Notepad++ install jsonview plugin
- 15. PARTITIONS「建议收藏」
- HCIP第十五天笔记(企业网的三层架构、VLAN以及VLAN 的配置)
- leetcode 11. The container that holds the most water
- [Practical skills] APP video tutorial for updating APP in CANFD, I2C, SPI and serial port mode of single-chip bootloader (2022-08-01)
- When Nodejs installation depends on cpnm, the install shows Error: Cannot find module 'fs/promises'
- 图像融合SDDGAN文章学习
- An animation basic element movie clip effect
- An动画基础之按钮动画与基础代码相结合
猜你喜欢
![[微服务]多级缓存](/img/58/72e01c789a862c058cba58b9113272.png)
[微服务]多级缓存

An introduction to the camera

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

Real number rounding and writing to file (C language file)

Sogou news-数据集

PyTorch构建分类网络模型(Mnist数据集,全连接神经网络)

PyTorch builds a neural network to predict temperature (dataset comparison, CPU vs GPU comparison)

基于php家具销售管理系统获取(php毕业设计)

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?

An动画优化之补间形状与传统补间的优化
随机推荐
An animation basic element movie clip effect
层次分析法
An动画基础之按钮动画与基础代码相结合
[Blue Bridge Cup Trial Question 48] Scratch Dance Machine Game Children's Programming Scratch Blue Bridge Cup Trial Question Explanation
如何让history历史记录前带时间戳
Use %Status value
An动画基础之元件的图形动画与按钮动画
基于php家具销售管理系统获取(php毕业设计)
An introduction to the pen tool, pencil tool and brush tool
PyTorch builds a classification network model (Mnist dataset, fully connected neural network)
期货公司开户关注的关键点
365天挑战LeetCode1000题——Day 048 有序队列 脑筋急转弯
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
技术分享 | 接口自动化测试如何搞定 json 响应断言?
图像融合DDcGAN学习笔记
GameFi industry down but not out | June Report
技术分享 | 接口自动化测试如何搞定 json 响应断言?
Golang interface interface
Golang sync.WaitGroup
基于php志愿者服务平台管理系统获取(php毕业设计)