博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode刷题:442. Find All Duplicates in an Array
阅读量:4041 次
发布时间:2019-05-24

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

LeetCode刷题:442. Find All Duplicates in an Array

原题链接:

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

Find all the elements that appear twice in this array.

Could you do it without extra space and in O(n) runtime?

Example:

Input:
[4,3,2,7,8,2,3,1]

Output:

[2,3]


算法设计

package com.bean.algorithmexec;import java.util.ArrayList;import java.util.List;public class FindAllDuplicates {		 /*	  * 输入参数为目标整型数组	  * 开辟一个List,记为res	  * */	 public static List
findDuplicates(int[] nums) { List
res = new ArrayList<>(); int[] count = new int[nums.length+1]; for(int i=0; i < nums.length; ++i){ if(++count[nums[i]] > 1) res.add(nums[i]); } return res; } public static void main(String[] args) { // TODO Auto-generated method stub int[] demo = new int[]{4,3,2,7,8,2,3,1}; List
result = findDuplicates(demo) ; for(int i=0;i

程序运行结果:

2    3

 

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

你可能感兴趣的文章
ORA-65016: FILE_NAME_CONVERT must be specified
查看>>
oralce 18c 创建PDB方式——利用seed(种子)模板来创建
查看>>
RAC, Data Gurad, Stream 讲解
查看>>
Oracle 18c CON_GUID_TO_ID
查看>>
Oracle 18c 创建PDB可使用的参数说明
查看>>
ORA-39071: Value for EXCLUDE is badly formed.
查看>>
ORA-65359: unable to create pluggable database with no data
查看>>
ORA-12754: Feature PDB SNAPSHOT CAROUSEL is disabled due to missing capability
查看>>
Oracle数据库——Scheduler Job
查看>>
Oracle执行语句跟踪(1)——使用sql trace实现语句追踪
查看>>
oralce 18c 创建PDB几种方式
查看>>
oracle exp 导出表时会发现少表,空表导不出解决方案
查看>>
ORA-14450:试图访问已经在使用的事务处理临时表
查看>>
ORACLE RMAN 各种场景恢复
查看>>
oracle 自动导出package/package body/procedure 等为sql文件并且自动上传到ftp服务器上
查看>>
linux 下 su - oracle 切换不了
查看>>
初学MonggoDb—Linux平台安装MongoDB
查看>>
使用“rz -be”命令上传文件至服务器;使用“sz 文件名”从服务器下载文件到本地
查看>>
mysql:pt-online-schema-change 在线修改表
查看>>
oracle 调整表空间大小 (resize)
查看>>