package com.code;

import java.util.Arrays;

public class Test04_2 {
public static int solution(int[] A) {
int size = A.length;
if(size == 1){
return A[0]==1?1:0;
}
Arrays.sort(A);
for(int i=0;i<size;i++){
if(A[i]!=i+1){
return 0;
}
}
return 1;
}
public static void main(String[] args) {
int [] a = {1,1,3,4};
System.out.println(solution(a));
}
} /**
1. PermCheck 桃花顺检验
Check whether array A is a permutation.
A non-empty zero-indexed array A consisting of N integers is given. A 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 a zero-indexed 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. Assume that: 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].
Complexity: expected worst-case time complexity is O(N);
expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.
*/

1. PermCheck 桃花顺检验 Check whether array A is a permutation.的更多相关文章

  1. Java – Check if Array contains a certain value?

    Java – Check if Array contains a certain value?1. String Arrays1.1 Check if a String Array contains ...

  2. 括弧匹配检验(check.cpp)

    [问题描述]        假设表达式中允许包含两种括号:圆括号和方括号,其嵌套的顺序随意,如([ ]())或[([ ][ ])]等为正确的匹配,[( ])或([ ]( )或 ( ( ) ) )均为错 ...

  3. [Swift]LeetCode958. 二叉树的完全性检验 | Check Completeness of a Binary Tree

    Given a binary tree, determine if it is a complete binary tree. Definition of a complete binary tree ...

  4. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  5. counting elements--codility

    lesson 4: counting elements 1. FrogRiverOne 2. PermCheck 3. MissingInteger 4. MaxCounters lesson 4: ...

  6. Codility---PermCheck

    Task description A non-empty zero-indexed array A consisting of N integers is given. A permutation i ...

  7. 4. Median of Two Sorted Arrays(Array; Divide-and-Conquer)

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  8. java array

    1 array变量 Type[] array_virable_name; 2 array对象 2.1 new Type[] array_virable_name = new Type[NUM]; 2. ...

  9. 算法与数据结构基础 - 数组(Array)

    数组基础 数组是最基础的数据结构,特点是O(1)时间读取任意下标元素,经常应用于排序(Sort).双指针(Two Pointers).二分查找(Binary Search).动态规划(DP)等算法.顺 ...

随机推荐

  1. 如何在Eclipse或者Myeclipse中使用tomcat(配置tomcat,发布web项目)?(图文详解)(很实用)

    前期博客 Eclipse里的Java EE视图在哪里?MyEclipse里的Java EE视图在哪里?MyEclipse里的MyEclipse Java Enterprise视图在哪里?(图文详解) ...

  2. Quartz.Net学习笔记(1)-完整的例子

    一.开发环境 系统:Win10 编译器:VS2013 .Net版本:4.5 Quartz版本:2.3.3 二.涉及程序集 Common.Logging.Core.dll Common.Logging. ...

  3. Hacker News的热门排名算法(转)

    Hacker News 是一家关于计算机黑客和创业公司的社会化新闻网站,由 Paul Graham 的创业孵化器 Y Combinator 创建.与其它社会化新闻网站不同的是 Hacker News ...

  4. 人人都能读懂的css3 3d小demo

    css3 3d案例总结 最近入坑 Web 动画,所以把自己的学习过程记录一下分享给大家.就把最近做的比较好的给大家分享下 1.旋转拼图 首先看下效果 代码主要由HTML和CSS3组成,应该说还是比较简 ...

  5. json-server模拟REST API

    介绍 可以开启一个服务前端使用 安装 npm i json-server -g 使用 $ json-server db.json -p 3000 // 在文件目录下执行上述命令,开放一个端口3000的 ...

  6. HDU_1143_tri tiling

    Tri Tiling Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  7. WINVER WIN32 WINNT

    WINVER 和 _WIN32_WINNT 请在WINDOWS.H前定义 从 Visual C++ 2008 开始,Visual C++ 不支持面向 Windows 95.Windows 98.Win ...

  8. 2019西安多校联训 Day2

    试题链接:http://www.accoders.com/contest.php?cid=1894   考试密码请私信; T1 残忍WA 0,明明就是一道非常菜的字符串QAQ 思路:一共找四种东西,A ...

  9. 洛谷 P1280 尼克的任务 (线性DP)

    题意概括 线性资源分配的问题,因为空闲的时间大小看后面的时间(反正感觉这个就是个套路)所以从后往前DP. 转移方程 如果当前时刻没有工作 f[i]=f[i+1]+1 如果当前时刻有工作 f[i]=ma ...

  10. Java对象序列化为什么要使用SerialversionUID

    1.首先谈谈为什么要序列化对象 把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬盘上,通 ...