博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Find All Numbers Disappeared in an Array(哈希表)
阅读量:4131 次
发布时间:2019-05-25

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

题目描述

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.

思路

数值大小在1到n之间,乱序,1到N 可以很方便用数组值做索引值链到一个新数组上,但是without extra space,能否链到本数组上呢?可以把链到的索引对应的值加nums.length,然后找索引的时候模去nums.length,这样就不会因为加了这个值而影响索引,在最后可以用数组值和nums.length大小比较来判断对应索引是否存在。

List
res = new ArrayList(); for(int i = 0;i < nums.length;i++){ nums[(nums[i] - 1)%nums.length] += nums.length; } for(int i = 0;i < nums.length;i++){ if(nums[i] <= nums.length) res.add(i+1); } return res;

转载地址:http://vhdvi.baihongyu.com/

你可能感兴趣的文章
Letter Combinations of a Phone Number
查看>>
Single Number II --出现一次的数(重)
查看>>
Valid Parentheses --括号匹配
查看>>
Count and Say
查看>>
Palindrome Partitioning --回文切割 深搜(重重)
查看>>
Valid Palindrome 简单的回文判断
查看>>
对话周鸿袆:从程序员创业谈起
查看>>
web.py 0.3 新手指南 - 如何用Gmail发送邮件
查看>>
web.py 0.3 新手指南 - RESTful doctesting using app.request
查看>>
LeetCode第46题思悟——全排列(permutations)
查看>>
Mysql中下划线问题
查看>>
微信小程序中使用npm过程中提示:npm WARN saveError ENOENT: no such file or directory
查看>>
Xcode 11 报错,提示libstdc++.6 缺失,解决方案
查看>>
idea的安装以及简单使用
查看>>
Windows mysql 安装
查看>>
python循环语句与C语言的区别
查看>>
Vue项目中使用img图片和background背景图的使用方法
查看>>
vue 项目中图片选择路径位置static 或 assets区别
查看>>
vue项目打包后无法运行报错空白页面
查看>>
Vue 解决部署到服务器后或者build之后Element UI图标不显示问题(404错误)
查看>>