繁体   English   中英

我如何在O(0)中将未知数量的元素插入数组

[英]how i can insert unkown number of element to array in O(0)

工作面试的问题

给定函数接收一个数组和一个int数作为参数,该函数必须在O(0)时间内将给定数插入到数组中的所有位置。

不确定O(o)含义,但实际上可以得到的最接近的是O(n)

private void filArrayWith(int[] a, int f) {
    Arrays.fill(a,f);
}

如@OldCurmudgeon所述,

不确定O(o)的含义,但实际上可以得到的最接近的是O(n)。

函数Arrays.fill(int[] a, int val)本身使用普通的for循环来分配元素。 因此,我认为这两种解决方案都是不错的。 下面是Arrays.fill方法的源代码:

    /**
     * Assigns the specified int value to each element of the specified array
     * of ints.
     *
     * @param a the array to be filled
     * @param val the value to be stored in all elements of the array
     */
    public static void fill(int[] a, int val) {
        for (int i = 0, len = a.length; i < len; i++)
            a[i] = val;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM