1. PermCheck 桃花顺检验 Check whether array A is a permutation.
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.的更多相关文章
- 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 ...
- 括弧匹配检验(check.cpp)
[问题描述] 假设表达式中允许包含两种括号:圆括号和方括号,其嵌套的顺序随意,如([ ]())或[([ ][ ])]等为正确的匹配,[( ])或([ ]( )或 ( ( ) ) )均为错 ...
- [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 ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- counting elements--codility
lesson 4: counting elements 1. FrogRiverOne 2. PermCheck 3. MissingInteger 4. MaxCounters lesson 4: ...
- Codility---PermCheck
Task description A non-empty zero-indexed array A consisting of N integers is given. A permutation i ...
- 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 ...
- java array
1 array变量 Type[] array_virable_name; 2 array对象 2.1 new Type[] array_virable_name = new Type[NUM]; 2. ...
- 算法与数据结构基础 - 数组(Array)
数组基础 数组是最基础的数据结构,特点是O(1)时间读取任意下标元素,经常应用于排序(Sort).双指针(Two Pointers).二分查找(Binary Search).动态规划(DP)等算法.顺 ...
随机推荐
- Java 8 (11) 新的日期和时间API
在Java 1.0中,对日期和时间的支持只能依赖java.util.Date类.这个类只能以毫秒的精度表示时间.这个类还有很多糟糕的问题,比如年份的起始选择是1900年,月份的起始从0开始.这意味着你 ...
- [ 东莞市选 2008 ] GCD&LCM
\(\\\) \(Description\) 给出两数的\(GCD\)和\(LCM\),求合法的两数之差的绝对值最小是多少. \(GCD\times LCM\le10^{18}\) \(\\\) \( ...
- 2199. [HZOI 2016] 活动投票
★★ 输入文件:hztp.in 输出文件:hztp.out 简单对比 时间限制:0.5 s 内存限制:2 MB [题目描述] 衡中活动很多,人也很多,一次活动有n个学生参与投票,现已知 ...
- Python 快排[pythonnic]
def QS(array): less = [] more = [] if len(array) <= 1: return array head = array.pop() for x in a ...
- Selenium学习第二天,了解Selenium工作模式与学习Selenium需要具备的知识与工具。
Selenium学习网站: 1.http://www.ltesting.net/ceshi/open/kygncsgj/selenium/2014/0408/207237.html——好像是对API的 ...
- 了解Selenium与自动化测试第一天“云里雾里”
以前没有搭建过Selenium自动化功能测试环境,想象中就像QTP一样,集成IDE一般简单快捷. 昨天通过博客园的一篇博友日志,才开始大概认识到Selenium的工作方式与特征: 1.插件般与浏览器结 ...
- 网络编程基础_4.1TCP_服务端
TCP_服务端 #include <stdio.h> // 1. 包含必要的头文件和库, 必须位于 windows之前 #include <WinSock2.h> #pragm ...
- vue基础---介绍
(1)声明式渲染 Vue.js 的核心是采用简洁的模板语法来声明式地将数据渲染进 DOM 的系统: ①文本 <div id="app"> {{ message }} & ...
- python队列的实现
队列是一种抽象数据结构,具有以下特点: (1)具有先进先出的特性(FIFO) (2)拥有两种基本操作,即加入和删除,而且使用front和rear两个指针来分别指向队列的前端和末尾. 队列的基本操作 c ...
- ocelot+consul+identity4的使用demo
Ocelot网关搭建 搭建core控制台项目 本demo采用2.1版本 命名为APPIGateWay 在Nuget包中添加Ocelot引用 选用8.0.0版本 添加Ocelot.json 文件 内容为 ...