Codility----PermMissingElem
2024-09-01 01:04:13
Task description
A zero-indexed array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing.
Your goal is to find that missing element.
Write a function:
class Solution { public int solution(int[] A); }
that, given a zero-indexed array A, returns the value of the missing element.
For example, given array A such that:
A[0] = 2 A[1] = 3 A[2] = 1 A[3] = 5
the function should return 4, as it is the missing element.
Assume that:
- N is an integer within the range [0..100,000];
- the elements of A are all distinct;
- each element of array A is an integer within the range [1..(N + 1)].
Complexity:
- expected worst-case time complexity is O(N);
- expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.
Solution
Programming language used: Java
Total time used: 4 minutes
Code: 10:43:42 UTC, java, final, score: 100
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.Arrays;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int res = 0;
int max = A.length;
if(max==0)
return 1;
Arrays.sort(A);
for(int i=0; i<max; i++){
res = (i+1)^A[i];
if(res != 0)
return (i+1);
}
return max+1;
}
}
最新文章
- (转载)最长递增子序列 O(NlogN)算法
- RDIFramework.NET平台代码生成器V2.8发布-更新于2014-12-31(提供下载)
- Fresco 源码分析(一) DraweeView-DraweeHierarchy-DraweeController(MVC) DraweeHierachy+DraweeController的分析
- php获得文件夹下所有文件的递归算法
- android LinearLayout 实现两端对齐
- nodejs以及npm的安装
- Delphi TcxTreeList 节点添加图片
- MySql连接异常解决
- springdata+redis配置详解
- Caffe Ubuntu14.04 64位 的最快安装 (cuda7.5 + cudnn7.0 2016最新)
- 生成JSON数据--fastjson(阿里)方法
- AngularJS 全局scope与指令 scope通信
- ArcGIS API for JavaScript 入门教程[3] 你看得到:数据与视图分离
- sklearn交叉验证-【老鱼学sklearn】
- 数据库表中不建索引,在插入数据时,通过sql语句防止重复添加
- windows安装 centos
- HIHOcoder 1441 后缀自动机一&#183;基本概念
- Linux系统运维笔记(四),CentOS 6.4安装Nginx
- JavaWeb基础—dbutils的简单入门
- 如何安装JDeveloper
热门文章
- spring-boot-starter-parent 1.3.6.RELEASE
- C++利用结构
- Qt - QDialog,QWidget实现模态及非模态(模态Widget不能有父窗口,如果设置无边框就不能阻塞父窗口,但是可以强行设置指定Qt::Dialog,还可以setAttribute(Qt::WA_ShowModal),很多讲究)good
- WPF4文字模糊不清晰、边框线条粗细不一致的解决方法
- 此C语言功能---A
- Python小技巧1
- 自由度(degree of freedom)
- 与jQuery的感情碰撞——由浅入深学jQuery
- 解决Ubuntu14.04在外接显示器不能指定问题的最佳分辨率
- Java数据结构和算法的数组