给出一个数组,要求返回不在数组中出现过的最小正整数

该博客展示了一段Java代码,用于对整数数组进行冒泡排序,并找到数组中连续元素间的间隔。主要涉及数组操作、比较和交换元素的逻辑,以及判断序列条件的实现。
public class Test {
    public static int solution(int[] A) {
        int temp;
        for(int i=0;i<A.length-1;i++) {
            for (int j = 0; j < A.length - i - 1; j++) {
                if (A[j + 1] < A[j]) {
                    temp = A[j];
                    A[j] = A[j + 1];
                    A[j + 1] = temp;
                }
            }
        }
        int res=1;
        if (A[0]>1||A[A.length-1]<1){ res=1;}
        else{
            for(int k=0;k<A.length-1;k++) {
                if(A[k]<A[k+1]-1){res=A[k]+1; break;}
                else{
                    res=A[A.length-1]+1;
                }
            }

        }
        if (res<1 ) res=1;
        return res;
    }

 public static void main(String[] args) {
        int[]A = {1,2,3};
        int[]A1 = {1, 3, 6, 4, 1, 2};
        int[]A2 = {-1, -3};
     int[]A4 = {-1, 1,2};

         System.out.println (solution(A));
         System.out.println (solution(A1));
         System.out.println (solution(A2));
     System.out.println (solution(A4));
    }

}
在C语言中,为了找出数组`a`中没有出现的最小正整数,我们可以采用以下步骤: 1. 首先遍历整个数组,将每个元素的值与一个预设的最小正整数(如`1`)进行比较,并检查它是否已经在结果集中。如果不在,就将其添加到结果集。 2. 定义一个变量`min_not_found`作为结果集的起始值,同时设置一个布尔类型的标志`found`为`false`,表示我们还没有找到最小正整数。 3. 使用一个循环结构,如`for`循环,遍历数组。如果当前元素大于0且小于等于`min_not_found`,说明它是我们要找的第一个未找到的正整数,更新`min_not_found`并设置`found`为`true`。 4. 当遍历完成后,如果没有在数组中找到正整数,那么`min_not_found`就是数组中没有出现的最小正整数;如果有,则返回`min_not_found + 1`,因为数组中已存在最小正整数。 下面是一个简单的示例代码: ```c #include <stdio.h> #include <stdbool.h> int findMinPositive(int a[], int n) { if (n == 0) return 1; bool found = false; int min_not_found = 1; for (int i = 0; i < n && !found; ++i) { if (a[i] > 0 && a[i] <= min_not_found) { min_not_found = a[i]; found = true; } } // 如果数组中有正整数,但没有最小正整数返回一个 return found ? min_not_found + 1 : min_not_found; } int main() { int array[] = {5, 2, -1, 8, 3}; int size = sizeof(array) / sizeof(array[0]); int result = findMinPositive(array, size); printf("Array中没有出现的最小正整数是: %d\n", result); return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值