在数组中按相同顺序重复元素 x 次

huangapple go评论72阅读模式
英文:

Repeating element in array x amount of times in same order

问题

以下是翻译好的部分:

问题要求:「编写一个方法,接受两个参数:一个 String[] 数组和一个表示重复次数的 int numRepeats。返回一个新数组,其中每个元素都重复 numRepeats 次。」

例如:

repeatElements(new String[]{"hello", "world"}, 3)

应返回一个包含以下元素的新数组:

["hello", "hello", "hello", "world", "world", "world"]

我目前的代码是:

public String[] repeatElements(String[] array, int numRepeats)
{
    String[] arr = new String[array.length * numRepeats];
    if(numRepeats == 0)
        return arr;
        
    if(array.length == 0)
        return arr;
        
    int m = 0;
    
    for(int i = 0; i < numRepeats; i++)
    {
        arr[m++] = array[0];
    }
    
    return arr;
}

我的解决方案只适用于某些测试,而不是所有测试。如何修复我的代码,以便它可以解决所有给定的测试?

英文:

The question asks: "Write a method that takes 2 parameters: a String[] array, and an int numRepeats representing the number of times to repeat each element in the array. Return a new array with each element repeated numRepeats times."

For example:

repeatElements(new String[]{&quot;hello&quot;, &quot;world&quot;}, 3)

Should return a new array with the elements:

[&quot;hello&quot;, &quot;hello&quot;, &quot;hello&quot;, &quot;world&quot;, &quot;world&quot;, &quot;world&quot;]

The code I have so far is:

public String[] repeatElements(String[] array, int numRepeats)
{
    String[] arr = new String[array.length * numRepeats];
    if(numRepeats == 0)
        return arr;
        
    if(array.length == 0)
        return arr;
        
    int m = 0;
    
    for(int i = 0; i &lt; numRepeats; i++)
    {
        arr[m++] = array[0];
    }
    
    return arr;
}

My solution only works for some tests, not all of them. How do I fix my code so it can solve all tests given?

答案1

得分: 0

你的循环只复制了数组的第一个元素。你本应该将所有元素复制 numRepeats 次。类似这样:

for (String s : array) {
    for (int i = 0; i < numRepeats; i++) {
        arr[m++] = s;
    }
}

将其理解为对于数组中的每个 String s,将 s 赋值给 arr 中的位置 m(然后递增 m)。

英文:

Your loop only copies the first element of array. You were supposed to copy all of the elements numRepeats times. Something like,

for (String s : array) {
    for (int i = 0; i &lt; numRepeats; i++) {
        arr[m++] = s;
    }
}

Read that as for each String s in array assign s to position m in arr (and increment m afterward).

答案2

得分: 0

你需要另一个嵌套循环来重复array中的元素,重复次数为numRepeats

按照以下方式进行操作:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(repeatElements(new String[] { "hello", "world" }, 3)));

    }

    static String[] repeatElements(String[] array, int numRepeats) {
        String[] arr = new String[array.length * numRepeats];
        if (numRepeats == 0)
            return arr;

        if (array.length == 0)
            return arr;

        int m = 0;

        for (int i = 0; i < array.length; i++) {
            for (int j = 1; j <= numRepeats; j++) {
                arr[m++] = array[i];
            }
        }

        return arr;
    }
}

输出:

[hello, hello, hello, world, world, world]

嵌套循环也可以写成以下形式:

for (int j = 1; j <= numRepeats; j++) {
    for (int i = 0; i < array.length; i++) {
        arr[m++] = array[i];
    }
}
英文:

You need another nested loop to repeat the elements of array for numRepeats number of times.

Do it as follows:

import java.util.Arrays;

public class Main {
	public static void main(String[] args) {
		System.out.println(Arrays.toString(repeatElements(new String[] { &quot;hello&quot;, &quot;world&quot; }, 3)));

	}

	static String[] repeatElements(String[] array, int numRepeats) {
		String[] arr = new String[array.length * numRepeats];
		if (numRepeats == 0)
			return arr;

		if (array.length == 0)
			return arr;

		int m = 0;

		for (int i = 0; i &lt; array.length; i++) {
			for (int j = 1; j &lt;= numRepeats; j++) {
				arr[m++] = array[i];
			}
		}

		return arr;
	}
}

Output:

[hello, hello, hello, world, world, world]

The nested loops can also be written as follows:

for (int j = 1; j &lt;= numRepeats; j++) {
	for (int i = 0; i &lt; array.length; i++) {
		arr[m++] = array[i];
	}
}

huangapple
  • 本文由 发表于 2020年4月8日 07:29:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/61091038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定