public class DeleteRange { public static void main(String[] args) { int[] L = {4, 2, 1, 5, 3, 6, 4}; int x = 2; int y = 4;

    int[] result = deleteRange(L, x, y);
    
    for (int i = 0; i < result.length; i++) {
        System.out.print(result[i] + " ");
    }
}

public static int[] deleteRange(int[] L, int x, int y) {
    int count = 0;
    int n = L.length;
    
    // 计算需要删除的元素个数
    for (int i = 0; i < n; i++) {
        if (L[i] >= x && L[i] <= y) {
            count++;
        }
    }
    
    int[] result = new int[n - count];
    int index = 0;
    
    // 将不需要删除的元素复制到新的数组中
    for (int i = 0; i < n; i++) {
        if (L[i] < x || L[i] > y) {
            result[index] = L[i];
            index++;
        }
    }
    
    return result;
}

}

Java 线性表删除指定范围元素算法 - 高效实现

原文地址: https://www.cveoy.top/t/topic/pgTW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录