博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
448. Find All Numbers Disappeared in an Array
阅读量:4348 次
发布时间:2019-06-07

本文共 1152 字,大约阅读时间需要 3 分钟。

题目:

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements of [1, n] inclusive that do not appear in this array.

Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

Example:

Input:[4,3,2,7,8,2,3,1]Output:[5,6]

链接:

3/25/2017

performance 14% 35ms

1 public class Solution { 2     public List
findDisappearedNumbers(int[] nums) { 3 List
ret = new ArrayList
(); 4 if (nums.length == 0) return ret; 5 6 for (int i = 0; i < nums.length; i++) { 7 ret.add(0); 8 } 9 for (int i = 0; i < nums.length; i++) {10 ret.set(nums[i] - 1, ret.get(nums[i] - 1) + 1);11 }12 int index = 0;13 for (int i = 0; i < nums.length; i++) {14 if (ret.get(i) == 0) {15 ret.set(index, i + 1);16 index++;17 }18 }19 return ret.subList(0, index);20 }21 }

别人的方法

转载于:https://www.cnblogs.com/panini/p/6622304.html

你可能感兴趣的文章
UIView的layoutSubviews,initWithFrame,initWithCoder方法
查看>>
STM32+IAP方案 实现网络升级应用固件
查看>>
用74HC165读8个按键状态
查看>>
jpg转bmp(使用libjpeg)
查看>>
linear-gradient常用实现效果
查看>>
sql语言的一大类 DML 数据的操纵语言
查看>>
VMware黑屏解决方法
查看>>
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>
C#检测驱动是否安装的问题
查看>>
web-4. 装饰页面的图像
查看>>
微信测试账户
查看>>
算法练习题
查看>>
学习使用Django一 安装虚拟环境
查看>>
Hibernate视频学习笔记(8)Lazy策略
查看>>
CSS3 结构性伪类选择器(1)
查看>>