首页 > 其他分享 >Counting Elements - PermCheck

Counting Elements - PermCheck

时间:2022-12-05 01:11:28浏览次数:118  
标签:given PermCheck int 000 Elements permutation such array Counting

Question

A non-empty array A consisting of N integers is given.

permutation is a sequence containing each element from 1 to N once, and only once.

For example, array A such that:

A[0] = 4 A[1] = 1 A[2] = 3 A[3] = 2

is a permutation, but array A such that:

A[0] = 4 A[1] = 1 A[2] = 3

is not a permutation, because value 2 is missing.

The goal is to check whether array A is a permutation.

Write a function:

class Solution { public int solution(int[] A); }

that, given an array A, returns 1 if array A is a permutation and 0 if it is not.

For example, given array A such that:

A[0] = 4 A[1] = 1 A[2] = 3 A[3] = 2

the function should return 1.

Given array A such that:

A[0] = 4 A[1] = 1 A[2] = 3

the function should return 0.

Write an efficient algorithm for the following assumptions:

  • N is an integer within the range [1..100,000];
  • each element of array A is an integer within the range [1..1,000,000,000].

 

 

Test Result

查看代码

public int solution(int[] A) {
        Set<Integer> nums = new HashSet<Integer>();
        for (int i = 1; i <= A.length; i++) {
            nums.add(i);
        }

        for (int a : A) {
            if (!nums.contains(a)) {
                return 0;
            } else {
                nums.remove(a);
            }
        }
        return nums.isEmpty() ? 1 : 0;
    }

标签:given,PermCheck,int,000,Elements,permutation,such,array,Counting
From: https://www.cnblogs.com/KresnikShi/p/16951309.html

相关文章

  • Counting Elements - FrogRiverOne
    QuestionAsmallfrogwantstogettotheothersideofariver.Thefrogisinitiallylocatedononebankoftheriver(position0)andwantstogettotheop......
  • CF 1722E Counting Rectangles(前缀和)
    题目分析(摘自这篇博客,原博主关于包含的定义有错误,在此处做了修改)给出一个长度为1e5的矩阵序列和1e5次询问,每次询问给出两个上下界矩阵,保证大矩阵包含小矩阵,请输出矩阵序......
  • LeetCode347. Top K Frequent Elements
    题意给一个序列,输出其前K个出现频次的数字方法优先队列代码classSolution{public:structnode{intval;//值intcnt;//出现次数......
  • 数数字(Digit Counting)
    DigitCountingTimelimit:3.000secondsTrungisboredwithhismathematicshomeworks.Hetakesapieceofchalkandstartswritingasequenceofconsecutiveint......
  • 【Amadeus原创】零基础使用vue3和elements创建应用
    一.创建环境1.创建D:\code\vue文件夹2.vscode打开文件夹3.打开终端,输入​​npminstall-g@vue/cli​​4.配置环境变量终端输入:​​npmconfiglist​​找到路径将路......
  • Java 8 | Collectors counting() with Examples
    https://www.geeksforgeeks.org/java-8-collectors-counting-with-examples/Collectorscounting() methodisusedtocountthenumberofelementspassedinthestr......
  • [AGC005D] ~K Perm Counting
    [AGC005D]~KPermCounting智慧转化,但不是很难想到。首先考虑容斥,设\(f_i\)表示强制\(i\)个位置\(|P_i-i|=k\)的方案数,那么答案就为\(\sum_{i=0}^nf_i(n-i)!\),......
  • Initialize all elements of an array to same value in C/C++
    UsingDesignatedInitializers//ordon'tspecifythesizeintarr[]={[0...4]=1};Usingstd::fill_nfunctionFinally,wecanusestd::fill_ninC++,......
  • LG4778 Counting swaps 题解
    LG4778Countingswaps给定你一个\(1\simn\)的排列\(p\),可进行若干次操作,每次选择两个整数\(x,y\),交换\(p_x,p_y\)。用最少的操作次数将给定排列变成单调上升的序......
  • Counting Rectangles(二维数组前缀和)
    题目链接题目描述:Youhave\(n\)rectangles,the\(i\)-threctanglehasheight\(h_i\)andwidth\(w_i\).Youareasked\(q\)queriesoftheform\(h_sw_sh_b......