首页 > 其他分享 >Time Complexity - PermMissingElem

Time Complexity - PermMissingElem

时间:2022-12-01 12:11:39浏览次数:100  
标签:given .. missing PermMissingElem element int Complexity Time array

Question

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)].

 

Report

class Solution {
    public int solution(int[] A) {
        // write your code in Java SE 8
        if(A.length == 0) {
            return 1;
        }
        BitSet bs = new BitSet();
        for (int i = 0; i < A.length; i++) {
            bs.set(A[i]);
        }

        int ret = -1;
        for (int i = 1; i <= A.length + 1; i++) {
            if (!bs.get(i)) {
                ret = i;
                break;
            }
        }
        return ret;
    }
}

 

标签:given,..,missing,PermMissingElem,element,int,Complexity,Time,array
From: https://www.cnblogs.com/KresnikShi/p/16941041.html

相关文章

  • Time Complexity - FrogJmp
    QuestionAsmallfrogwantstogettotheothersideoftheroad.ThefrogiscurrentlylocatedatpositionXandwantstogettoapositiongreaterthanoreq......
  • Java 时间格式化方法 DateTimeFormatter
    在 Java8之前,一般使用 SimpleDateFormat类进行时间格式化,但是这不是同步执行的方法,所以存在多线程执行不安全的问题。如果使用的是Java8之前的JDK,变成线程安全,就......
  • Python中5大模块的使用教程(collections模块、time时间模块、random模块、os模块、sys
    1.模块的简单认识定义:模块就是我们把装有特定功能的代码进行归类的结果.从代码编写的单位来看我们的程序,从小到大的顺序:一条代码<语句块<代码块(函数,类)<模......
  • 定时器:Timer
    定时器定时器是一种控制任务延时调用,或者周期调用的技术。作用:闹钟、定时邮件发送。定时器的实现方式方式一:TimerTimer定时器Timer定时器的特点和存在的问题1、Timer是......
  • jstl一些标签 中timestamp类型在页面去掉时分秒!
    <fmt:formatDatevalue='${vo.updateDate}'pattern='yyyy-MM-dd'/>  JSTL的ifelse:有c:if没有else的处理分类:JSP/JAVA/J2EE(11)作者同类文章X jstl的......
  • rabbitmq的consumer_timeout修改
    问题:项目中使用了rabbitmq来做异步任务,最近突然发现一个耗时比较长的任务一直在重复执行。排查:排查日志后发现了超时,channel关闭。如果超过了consumer_timeout时间默认......
  • ios中getTime()的兼容性问题
    时间格式为:2017-12-1212:00:00在苹果上获取时间戳有兼容性问题 需要转换成2017/12/1212:00:00 才可以正确获取到时间戳 vargetTime=function(time){varmyDate......
  • RuntimeError: Unable to create a new session key. It is likely that the cache is
    使用Centos7.6安装的Stein版本,按照官方文档一步步安装Dashboard,输入用户名和密码后出错,如下所示 查看apache日志/var/log/httpd/error_log,发现以下错误[root@controll......
  • python中time模块的常用方法的转换关系图
      获取当前的时间戳  把时间戳转换成了时间的格式  获取时间  把时间格式数据转换为易识别的字符串 获取到表示时间的字符串,再转换为时间数据。 ......
  • Linux系统时间的设定以及自带的timesync时间同步
     1.三个阶段的系统时间设定 1.1内核启动阶段   这里是在menuconfig文件配置RTC设定系统时间选项。   CONFIG_RTC_HCTOSYS_DEVICE="rtc1",或者直接在decon......