Codility 2.PermMissingElem Java8

Task description

An 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 an 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.

Write an efficient algorithm for the following assumptions:

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)].
Copyright 2009–2020 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

题目本身不难,只是边角问题一定要注意。因为网站这里给出的test case 很奇怪,要是第一次拿不到满分可以尝试按照错误的test case更改,不然最后拿不到满分。自定义的test case 可以尝试写几个。

P.S 这个使用到的包需要自己import 不然会显示错误

解法一

Detected time complexity:O(N) or O(N * log(N))

import java.util.Arrays;

class Solution {
   
    public int solution(int[] A) {
   
        // write your code in Java SE 8
        
        //empty_and_single
        if(A.length==0 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值