从数组中删除最后出现的特定整数

问题描述 投票:-1回答:2

出于某种原因,我的解决方案还不完整。我从隐藏的规格测试中得到80/100。

  1. 我的解决方案出了什么问题?可能有一个我没有想到的用例。
  2. 如何使用ArrayList而不是数组来改变时空复杂度?
  3. 是否有更好的方法来解决此问题?

我当前的解决方案处理:

  • 空输入数组
  • 输入数组中的负/正整数值
  • 输入数组中的重复项
  • 已排序/未排序的输入数组

说明:

[编写Java方法removeLastOccurrence(int x,int [] arr),该方法从给定的整数元素arr数组中删除最后一次出现的给定整数x。

该方法应返回一个新数组,其中包含给定数组arr中的所有元素,但最后一次出现的元素x除外。其余元素应以相同顺序出现在输入数组和返回的数组中。

右侧的代码向您显示了一个代码框架,在该框架中仍然缺少一个静态方法的实现。提供此实现并通过自己编写更多测试或使用提供的测试和规范测试来检查其是否正确。

我的代码:

class RemoveLastOccurrenceArray {

    /**
     * Takes the array and the last occurring element x,
     * shifting the rest of the elements left. I.e.
     * [1, 4, 7, 9], with x=7 would result in:
     * [1, 4, 9].
     *
     * @param x   the entry to remove from the array
     * @param arr to remove an entry from
     * @return the updated array, without the last occurrence of x
     */
    public static int[] removeLastOccurrence(int x, int[] arr) {

        // if arr == null return null;
        if (arr == null || arr.length == 0) return arr;

        // return a new array which will be size arr.legnth-1
        int[] res = new int[arr.length - 1];

        // introduce an int tracker which keep tracks of the index of the last occurrence of x
        int last_index = -1;

        // traverse through the array to get the index of the last occurrence
        for (int i = 0; i < arr.length; i++) if (arr[i] == x) last_index = i;

        int i = 0, j = 0;

        // copying elements of array from the old one to the new one except last_index
        while (i < arr.length) {
            if (i == last_index) {
                if (i++ < res.length) {
                    res[j++] = arr[i++];
                }
            } else res[j++] = arr[i++];
        }

        // if we pass in x which is not in the array just return the original array
        if (last_index == -1) return arr;

        // are there duplicates in the array? - WORKS
        // does the array have negative numbers? - WORKS
        // Is the array sorted/unsorted - WORKS

        return res;
    }
}

通过单元测试

import static org.junit.Assert.*;

import org.junit.*;

public class RemoveLastOccurrenceArrayTest {
    @Test
    public void testRemoveArray_Empty() {
        int[] array = new int[0];
        assertEquals(0, RemoveLastOccurrenceArray.removeLastOccurrence(5, array).length);
    }

    @Test
    public void testFirstSimple() {
        int[] input = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int[] result = {2, 3, 4, 5, 6, 7, 8, 9, 10};
        assertArrayEquals(result, RemoveLastOccurrenceArray.removeLastOccurrence(1, input));
    }

    @Test
    public void testLastSimple() {
        int[] input = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int[] result = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        assertArrayEquals(result, RemoveLastOccurrenceArray.removeLastOccurrence(10, input));
    }

    @Test
    public void testPositiveInMiddleDuplicate() {
        int[] input = {1, 2, 3, 3, 4, 5};
        int[] result = {1, 2, 3, 4, 5};
        assertArrayEquals(result, RemoveLastOccurrenceArray.removeLastOccurrence(3, input));
    }

    @Test
    public void testNegativeFirst() {
        int[] input = {-3, -1, 2, -3, 3, 4, 5, 0};
        int[] result = {-3, -1, 2, 3, 4, 5, 0};
        assertArrayEquals(result, RemoveLastOccurrenceArray.removeLastOccurrence(-3, input));
    }

    @Test
    public void testLasttoRemove() {
        int[] input = {1, 4, 7, 9};
        int[] result = {1, 4, 7};
        assertArrayEquals(result, RemoveLastOccurrenceArray.removeLastOccurrence(9, input));
    }
}
java algorithm
2个回答
0
投票

这是答案,非常感谢!

另外,如果找不到x,则您的崩溃会崩溃,我的不会崩溃。也许那是二十个标记去的地方?

我已经在检查这个,但是在我的代码中为时已晚。所以我只需要移动

[if (last_index == -1) return arr;在while循环之前,我得到100/100分。

enter image description here


您的教授会喜欢这个吗?换一种说法,我认为没有比您的答案更有效的方法了。但是也许他们喜欢看使用的Java类...

您的教授不告诉您您在哪里失去分数吗?如果他们不告诉您他们对满分的期望,您将无济于事。但是这是另一种方式……再次,我认为没有更好的选择,也不值得再增加20分。我将其发布,因为它是“另一种方式”。

    public int[] removeLastOccurrence2(int x, int[] arr) {

        // if arr == null return null;
        if (arr == null || arr.length == 0) return arr;

        // Fill an ArrayList with your initial array ...

        java.util.List list = new java.util.ArrayList(arr.length);

        for (int i=0; i<arr.length; i++) {
            list.add(arr[i]);
        }

        int[] res;

        // Now ... use ArrayList methods to do the work.

        // Also, if there is no x to find, yours crashes ... mine doesn't.
        // Maybe that's where the twenty marks went?

        if ( list.lastIndexOf(x) != -1 ) { // This screens for no x found at all ...

             list.remove( list.lastIndexOf(x) ); // Done!

             // Make a new array to return.
             res = new int[list.size()];
             for (int i=0; i<list.size(); i++) {
                 res[i] = (int) list.get(i);
             }

        } else {

            // No 'x' found, so just return the original array.
            res = arr;
        }        

       return res;
    }

1
投票

为什么不尝试向后迭代?

for(int i = arr.length; i => 0; i--)
{ 
   if (arr[i] == x)
   {
       return ArrayUtils.remove(arr, i)
    }
}

然后,找到索引后,可以使用Apache Commons ArrayUtils remove命令在[]处删除该项目>

© www.soinside.com 2019 - 2024. All rights reserved.