1. PermMissingElem Find the missing element in a given permutation.

少了一个;

package com.code;

import java.util.Arrays;

public class Test03_2 {

    public static int solution(int[] A) {
        // write your code in Java SE 8
        Arrays.sort(A);
        int size = A.length;
        if(size==0){
            return 1;
        }
        if(size==1){
            return A[0]==1?2:1;
        }
        if(A[size-1]==size){
            return size+1;
        }
        for (int i=0;i<size;i++){
            if(A[i]> i+1){
                return (i+1);
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int [] a = {1,2,3,4};
        System.out.println( solution(a));
        int [] b = {2,3,4,5};
        System.out.println(solution(b));
        int [] c = {};
        System.out.println(solution(c));
        int [] d = {1};
        System.out.println(solution(d));
    }
}
/**
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.

*/
时间: 2024-08-01 22:47:03

1. PermMissingElem Find the missing element in a given permutation.的相关文章

Find the missing element in a given permutation.

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:

LeetCode 1060. Missing Element in Sorted Array

原题链接在这里:https://leetcode.com/problems/missing-element-in-sorted-array/ 题目: Given a sorted array A of unique numbers, find the K-th missing number starting from the leftmost number of the array. Example 1: Input: A = [4,7,9,10], K = 1 Output: 5 Explan

PermMissingElem

问题:Find the missing element in a given permutation. 1.PHP部分 1 //时间复杂度O(n),空间复杂度O(1) 2 //通过率100% 3 function solution($A) { 4 // write your code in PHP5.5 5 sort($A); 6 $len = count($A); 7 for($i=1;$i<=$len; $i++){ 8 if($A[$i-1] != $i){ 9 return $i; 10

zTree实现地市县三级级联报错(三)

zTree实现地市县三级级联报错(三) 1.详细报错例如以下 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help | start | stop } 2014-5-11 22:37:22 org.apache.catalina.core.AprLifecycleListener init 信息: Loaded APR based Apache Tomcat Nat

MariaDB 5.5.52 发布,MySQL 分支版本

MariaDB 5.5.52 发布了,值得注意的是在5.5.51版本中有一个安全性的修复,如果你运行的是MariaDB 5.5.50或更低版本,建议你至少升级至MariaDB 5.5.51版本. 更新内容如下: Revision #0da39ca 2016-09-12 16:18:07 +0200 fix BIGINT+MEDIUMINT type aggregation Revision #347eeef 2016-09-11 20:55:11 +0200 don't use my_copys

【Python模块学习】4、collections模块

collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtuple() factory function for creating tuple subclasses with named fields deque list-like container with fast appends and pops on either end ChainMap dict-like class for creating a single view of multiple

ISLR chapter 2, R 基础

2.3.1 Basic Commands create an array x = c(1,6,2) create a matrix >x=matrix (data=c(1,2,3,4) , nrow=2, ncol =2) > x=matrix (c(1,2,3,4) ,2,2) > x[1,2] [1] 3 > matrix (c(1,2,3,4) ,2,2,byrow =TRUE), then populate by rows [,1] [,2][1,] 1 2[2,] 3 4

neo4j 学习-2

Neo4j 查询例句 MATCH (john {name: 'John'})-[:friend]->()-[:friend]->(fof) RETURN john.name, fof.name MATCH (user)-[:friend]->(follower) WHERE user.name IN ['Joe', 'John', 'Sara', 'Maria', 'Steve'] AND follower.name =~ 'S.*' RETURN user.name, follower

selenium docs

Note to the Reader - Docs Being Revised for Selenium 2.0! Introduction Test Automation for Web Applications To Automate or Not to Automate? Introducing Selenium Brief History of The Selenium Project Selenium’s Tool Suite Choosing Your Selenium Tool S