Codility----PermMissingElem
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.
// 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;
}
}
Codility----PermMissingElem的更多相关文章
- codility上的练习 (1)
codility上面添加了教程.目前只有lesson 1,讲复杂度的……里面有几个题, 目前感觉题库的题简单. tasks: Frog-Jmp: 一只青蛙,要从X跳到Y或者大于等于Y的地方,每次跳的距 ...
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
- *[codility]AscendingPaths
https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
- *[codility]MaxDoubleSliceSum
https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...
- *[codility]Fish
https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...
- *[codility]CartesianSequence
https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...
随机推荐
- react里执行shouldComponentUpdate时返回false的后果
大家都知道生命周期shouldComponentUpdate返回false时,不会进行后续的渲染,那这个时候state是什么情况呢.我们看一下demo class Toggle extends Rea ...
- Android推送服务——百度云推送
一.推送服务简介 消息推送,顾名思义,是由一方主动发起,而另一方与发起方以某一种方式建立连接并接收消息.在Android开发中,这里的发起方我们把它叫做推送服务器(Push Server),接收方叫做 ...
- 【31.58%】【codeforces 682D】Alyona and Strings
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 拦截和跟踪HTTP请求的主要方法及实现
一.HTTP 请求的拦截技术可以广泛地应用在反向代理.拦截 Ajax 通信.网页的在线翻译.网站改版重构等方面.而拦截根据位置可以分为服务器端和客户端两大类,客户端拦截借助 JavaScript 脚本 ...
- 开发Android项目中使用androidannotations
前言 最近接手了一个工程,使用了androidannotations,因为以前使用过butterknife所以有似曾相识的感觉,但是有些用法还是不一样,所以就花时间学习了下androidannotat ...
- End-to end provisioning of storage clouds
Embodiments discussed in this disclosure provide an integrated provisioning framework that automates ...
- HTC VIVE 虚拟现实眼镜VR游戏体验
HTC的VIVE入手一段时间了,体验了几个免费的VR游戏,效果还不错,分享一下. 1. VIVE主要部件 VIVE的主要部件有3个,分别是头盔,两个无线控制手柄和两个定位器. 1.1 头盔 头盔整体照 ...
- 半监督学习(semi-supervised learning)
P(x)P(x,y)}⇒P(y|x) 自 P(x) 生成的无标签样本: 自 P(x,y) 生成的标记样本:
- Scatter matrix(散布矩阵)
n 个 m 维的样本,Xm×n=[x1,x2,-,xn],样本均值定义为: x¯=1n∑i=1nxi 散列矩阵定义为如下的半正定矩阵: S=∑j=1n(xj−x¯)(xj−x¯)T=∑j=1n(xj− ...
- Android Widget 小工具(两) 使用configure
添加Widget在此之前需要做一些处理操作,可以使用 配置活动 在上一篇的实现基础上,加上配置活动(configure=activity).这时加入Widget时.会先打开一个Activity,进行配 ...