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.
*/
1. 少了一个PermMissingElem Find the missing element in a given permutation.的更多相关文章
- redis cluster突然少了一个node的问题
今天进入redis执行cluster info发现 cluster_state:fail 并且 cluster_known_nodes:5 少了一个7006的node 然后我重启了7006的 ...
- Bug2020011601,在ssh项目的applicaitonContext.xml中,少了一个双引号,打包成功(没报错),项目运行才发现
在ssh项目的applicaitonContext.xml中,少了一个双引号,打包成功(没报错),项目运行才发现. 加上少的双引号,解决了.
- jackson 转换 yyyy-MM-dd格式 少了一个小时问题解决(仅限中国)
如果你在你的实体类上面指定了@JsonFormat(pattern = "yyyy-MM-dd",timezone="GMT+8") 然后发现换成json后 小 ...
- 【Vue.js】代码优化:在dom中加一行v-if就可少写一个循环类方法
[问题描述] 把当前用户的购物车中(cartList),商品(good)选中字段checked = true的商品在订单页面中进行展示出来. [一般做法](两次循环) 首先取出当前用户的购物车列表,循 ...
- mac安全与隐私只有两个选项,少了一个任何来源
很多软件安装后就会出现,程序已损坏,请移到废纸篓的提示. 解决方法:在终端里输入:sudo spctl --master-disable 然后回车,然后输入密码,即可在安全选项中看到重新出现的允许任何 ...
- 接着上回,导包正确之后,出现javabean.Friend cannot be cast to java.util.List,的错误。找了很久。以为是User user0作为参数,改成了String username还是错误,看了看listFriend.jsp没有错误,我想会不会是包多了,导致类型复杂。最后发现包少了一个:
import org.apache.commons.dbutils.handlers.BeanListHandler;这个包,BeanListHandler让我发现List<Friend> ...
- 【LeetCode】1060. Missing Element in Sorted Array 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- LeetCode 1060. Missing Element in Sorted Array
原题链接在这里:https://leetcode.com/problems/missing-element-in-sorted-array/ 题目: Given a sorted array A of ...
- 用 customRef 做一个防抖函数,支持 element 等UI库。
这几天学习Vue的官网,看到 customRef 提供了一个例子,研究半天发现这是一个防抖函数,觉得挺好,于是把这个例子扩展了一下,可以用于表单子控件和查询子控件. 需求 v-model 基于 ele ...
随机推荐
- virtualwrapper使用
1.创建: mkvirtualenv 虚拟环境目录 2.查看虚拟环境 workon lsvirtualenv 3.进入虚拟环境 workon 虚拟环境目录 4.退出虚拟环境 deactivate 5. ...
- 6.13---shiro
- Objective-C设计模式——外观Faced(接口适配)
外观模式 外观设计模式和适配器差不多,不过它门对对象控制的粒度不同,适配器一般只是控制一个系统和客户端的对接.外观则是用来抽象多个系统一起工作. 外观一般具有多个子系统,所以外观应持有多个子系统的引用 ...
- 全面介绍Android Studio中Git 的使用(一)
来源 :http://blog.csdn.net/gao_chun/article/details/49817229/
- [转]汇编语言:MOVSB,MOVSW,MOVSD
汇编语言:MOVSB,MOVSW,MOVSD 转自: http://blog.csdn.net/zhenyongyuan123/article/details/8364011 目前80386系列的 ...
- HDU_1242_Rescue
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1242 大意:迷宫搜索,'#'代表墙,'.'代表路,'x'代表守卫,每移动1格用1秒,杀死守卫用1秒,ang ...
- 牛客多校Round 1
Solved:1 rank:249 E. Removal dp i,j表示前i个数删除了j个且选择了第i个的答案 类似字符串的dp 预处理一下nex i_k即i后面k第一次出现的位置 就好转移了 # ...
- python bs4库
Beautiful Soup parses anything you give it, and does the tree traversal stuff for you. BeautifulSoup ...
- Number 数据类型
//Number 数据类型//包含 整数 小数 NaN(not a number)var a = 1233;var b = 12.34;//1/'a'//把其他数据类型转化成数字,他在转化时,只要字符 ...
- 作业 3-5 switch语句的应用
/*输入五级制成绩(A-E),输出相应的百分制成绩(0-100)区间*/ #include<stdio.h> int main(void) { char ch;/*定义一个字符*/ pri ...