java輸出數組中的最大值和最小值,找出數組中從未出現的最小正整數java實現

 2023-12-06 阅读 27 评论 0

摘要:1 /** 2 * 找出未出現的最小正整數 3 * @param A 4 * @param n 5 * @date 2016-10-7 6 * @author shaobn 7 */ 8 public static int findArrayMex(int[] a,int n){ 9 int count = n; 10 int temp = 0; 11 int dir = 1; 12 int num = 0; 13 f
 1 /**
 2      * 找出未出現的最小正整數
 3      * @param A
 4      * @param n
 5      * @date 2016-10-7
 6      * @author shaobn
 7      */
 8     public static int findArrayMex(int[] a,int n){
 9         int count = n;
10         int temp = 0;
11         int dir = 1;
12         int num = 0;
13         for(int i = 0;i<count-1;i++){
14             if(a[i]>a[i+1]){
15                 temp = a[i];
16                 a[i]= a[i+1];
17                 a[i+1] = temp;        
18             }
19             if(i==count-2){
20                 count--;
21                 i=-1;
22             }
23         }
24         if(a[0]>1 || a[n-1]<1){
25             return 1;
26         }else {
27             for(int i =0;i<n-1;i++){
28                 if(a[i]>0){
29                     num = i;
30                     break;
31                 }                
32             }
33             if(a[num]>1){
34                 return 1;
35             }else if(a[num]==1) {
36                 for(int i =num+1;i<n-1;i++){
37                     if(a[i]-a[num]>dir){
38                         return a[num]+dir;
39                     }else {
40                         dir++;
41                     }
42                 }
43             }
44         }
45         return a[num]+dir;
46     }

?

轉載于:https://www.cnblogs.com/assassin666/p/5936493.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/192014.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息