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;
}
}

最新文章

  1. (转载)最长递增子序列 O(NlogN)算法
  2. RDIFramework.NET平台代码生成器V2.8发布-更新于2014-12-31(提供下载)
  3. Fresco 源码分析(一) DraweeView-DraweeHierarchy-DraweeController(MVC) DraweeHierachy+DraweeController的分析
  4. php获得文件夹下所有文件的递归算法
  5. android LinearLayout 实现两端对齐
  6. nodejs以及npm的安装
  7. Delphi TcxTreeList 节点添加图片
  8. MySql连接异常解决
  9. springdata+redis配置详解
  10. Caffe Ubuntu14.04 64位 的最快安装 (cuda7.5 + cudnn7.0 2016最新)
  11. 生成JSON数据--fastjson(阿里)方法
  12. AngularJS 全局scope与指令 scope通信
  13. ArcGIS API for JavaScript 入门教程[3] 你看得到:数据与视图分离
  14. sklearn交叉验证-【老鱼学sklearn】
  15. 数据库表中不建索引,在插入数据时,通过sql语句防止重复添加
  16. windows安装 centos
  17. HIHOcoder 1441 后缀自动机一&#183;基本概念
  18. Linux系统运维笔记(四),CentOS 6.4安装Nginx
  19. JavaWeb基础—dbutils的简单入门
  20. 如何安装JDeveloper

热门文章

  1. spring-boot-starter-parent 1.3.6.RELEASE
  2. C++利用结构
  3. Qt - QDialog,QWidget实现模态及非模态(模态Widget不能有父窗口,如果设置无边框就不能阻塞父窗口,但是可以强行设置指定Qt::Dialog,还可以setAttribute(Qt::WA_ShowModal),很多讲究)good
  4. WPF4文字模糊不清晰、边框线条粗细不一致的解决方法
  5. 此C语言功能---A
  6. Python小技巧1
  7. 自由度(degree of freedom)
  8. 与jQuery的感情碰撞——由浅入深学jQuery
  9. 解决Ubuntu14.04在外接显示器不能指定问题的最佳分辨率
  10. Java数据结构和算法的数组