【问题标题】:How to reduce compilation time and which cases will this code fail如何减少编译时间以及此代码在哪些情况下会失败
【发布时间】:2021-02-07 11:12:35
【问题描述】:

问题-
Monk 喜欢对数组执行不同的操作,因此作为 Hackerearth 学校的校长,他将一项任务分配给了他的新学生 Mishki。 Mishki 将获得一个大小为 N 的整数数组 A 和一个整数 K ,她需要将数组沿正确方向旋转 K 步,然后打印结果数组。由于她是新来的学校,请帮助她完成任务。
输入:
第一行由一个整数 T 组成,表示测试用例的数量。 对于每个测试用例:

  1. 第一行由两个整数 N 和 K 组成,N 是数组中元素的数量,K 表示旋转的步数。
  2. 下一行由 N 个空格分隔的整数组成,表示数组 A 的元素。
    它失败了一些测试用例,我不知道哪个以及如何降低时间复杂度。
    这是我的代码,我做了所有我能做的研究。另外请给出简单的解决方案,因为我是初学者。
    Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        for (int i = 0; i < t; i++) {

            int n = sc.nextInt();
            int k = sc.nextInt();
            int v[] = new int[n];

            for (int j = 0; j < n; j++) {
                v[j] = sc.nextInt();
            }

            for (int m = 1; m <= k; m++) {
                int temp = v[n - 1];
                for (int p = 1; p < n; p++) {

                    v[n - p] = v[n - (p + 1)];

                }
                v[0] = temp;

            }
            for (int a : v) {
                System.out.print(a + " ");
            }

        }

【问题讨论】:

  • 尝试创建测试用例,找出哪些有效,哪些失败。
  • 我试过了,都通过了,找不到一个不符合此代码的案例我认为我遵循了家庭作业指南并针对这个问题做了所有我能做的研究。

标签: java arrays loops time


【解决方案1】:

我不确定我是否理解代码,但据我所知,您发布的内容有效。

我对这一切进行了重构,使其更易于消化 - 更好的命名,提取到方法的许多部分,消除了对扫描仪的需求,因此我可以针对“旋转”方法运行一堆我自己的测试用例来检查输入与预期输出。

import java.util.Arrays;

public class Application {

    public void run(int testCases, int arraySize, int rotations, String[] arrayElements) {
        for (int i = 0; i < testCases; i++) {
            getRotatedArray(arraySize, rotations, arrayElements);
        }
    }

    public int[] getRotatedArray(int arraySize, int rotationXTimes, String[] arrayElements) {
        int[] a = new int[arraySize];
        populateIntArray(arrayElements, a);
        rotateArray(rotationXTimes, a);
        Arrays.toString(a);
        return a;
    }

    private void rotateArray(int rotations, int[] array) {
        for (int m = 1; m <= rotations; m++) {
            // get last item of array...
            int length = array.length;
            int temp = array[length - 1];
            // shift elements 1 position right
            for (int p = 1; p < length; p++) {
                array[length - p] = array[length - (p + 1)];
            }
            // first  element becomes last.
            array[0] = temp;
        }
    }

    private int[] populateIntArray(String[] arrayElements, int[] array) {
        for (int j = 0; j < arrayElements.length; j++) {
            array[j] = Integer.parseInt(arrayElements[j]);
        }
        return array;
    }
}

一些测试:

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

public class ApplicationTest {

    private Application app;

    @Before
    public void setUp()  {
        app = new Application();
    }

    @Test
    public void test1() {
        int[] result = app.getRotatedArray(0 , 0, new String[]{});

        assertEquals(0, result.length);
    }

    @Test
    public void test2() {
        int[] result = app.getRotatedArray(1 , 0, new String[]{"0"});

        assertEquals(1, result.length);
        assertArrayEquals(new int[]{0}, result);
    }

    @Test
    public void test3() {
        int[] result = app.getRotatedArray(1 , 1, new String[]{"0"});

        assertEquals(1, result.length);
        assertArrayEquals(new int[]{0}, result);
    }

    @Test
    public void test4() {
        int[] result = app.getRotatedArray(1 , 2, new String[]{"0"});

        assertEquals(1, result.length);
        assertArrayEquals(new int[]{0}, result);
    }

    @Test
    public void test5() {
        int[] result = app.getRotatedArray(2 , 0, new String[]{"0","1"});

        assertEquals(2, result.length);
        assertArrayEquals(new int[]{0,1}, result);
    }

    @Test
    public void test6() {
        int[] result = app.getRotatedArray(2 , 1, new String[]{"0","1"});

        assertEquals(2, result.length);
        assertArrayEquals(new int[]{1,0}, result);
    }

    @Test
    public void test7() {
        int[] result = app.getRotatedArray(2 , 2, new String[]{"0","1"});

        assertEquals(2, result.length);
        assertArrayEquals(new int[]{0,1}, result);
    }

    @Test
    public void test8() {
        int[] result = app.getRotatedArray(3 , 0, new String[]{"0","1","2"});

        assertEquals(3, result.length);
        assertArrayEquals(new int[]{0,1,2}, result);
    }

    @Test
    public void test9() {
        int[] result = app.getRotatedArray(3 , 1, new String[]{"0","1","2"});

        assertEquals(3, result.length);
        assertArrayEquals(new int[]{2,0,1}, result);
    }

    @Test
    public void test10() {
        int[] result = app.getRotatedArray(3 , 2, new String[]{"0","1","2"});

        assertEquals(3, result.length);
        assertArrayEquals(new int[]{1,2,0}, result);
    }

    @Test
    public void test11() {
        int[] result = app.getRotatedArray(3 , 3, new String[]{"0","1","2"});

        assertEquals(3, result.length);
        assertArrayEquals(new int[]{0,1,2}, result);
    }

    @Test
    public void test12() {
        int[] result = app.getRotatedArray(4 , 0, new String[]{"0","1","2","3"});

        assertEquals(4, result.length);
        assertArrayEquals(new int[]{0,1,2,3}, result);
    }

    @Test
    public void test13() {
        int[] result = app.getRotatedArray(4 , 4, new String[]{"0","1","2","3"});

        assertEquals(4, result.length);
        assertArrayEquals(new int[]{0,1,2,3}, result);
    }
}

所有测试都通过了。

如果您阅读了测试,您应该能够看到我正在提供一些 输入,执行特定方法(执行一些 过程),并检查 >输出与我的预期相反。

即我正在测试 getRotatedArray 是否符合预期。确实如此。

当您说它中断并且您不知道在哪里时,我建议添加一些 System.out.println 语句,以便您可以验证应用程序正在执行您认为它正在执行的操作。

【讨论】:

  • 嗯,我会努力的,感谢您对代码进行了一些研究。
猜你喜欢
  • 2016-11-27
  • 2016-09-01
  • 1970-01-01
  • 2014-07-13
  • 1970-01-01
  • 1970-01-01
  • 2019-12-14
  • 1970-01-01
  • 2010-10-14
相关资源
最近更新 更多