在数组中在特定值之前添加一个元素

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

Adding an element before particular value in an array

问题

需要在特定元素的每次出现之前添加一个元素。我可以使用for循环来计算索引,然后添加元素来实现这个目标。是否有不使用for循环或使用流的高效方法?

例如:int[] a = {1,2,4,5,7,9,2,5}
在每次出现2之前添加元素**{3},结果为{1,3,2,4,5,7,9,3,2,5}**

尝试:

int[] a = {1, 2, 4, 5, 7, 9, 2, 5};
int[] new1 = null;
int[] indexToAdd = IntStream.range(0, a.length)
    .filter(i -> a[i] == 2)
    .map(i -> i) 
    .toArray();
for (int j = 0; j < indexToAdd.length; j++) {
    final Integer innerj = new Integer(j);
    new1 = IntStream.range(0, a.length)
        .map(i -> {
            if (i < indexToAdd[innerj]) {
                return a[i];
            } else if (i == indexToAdd[innerj]) {
                return 3;
            } else {
                return a[i - 1];
            }
        }).toArray();
}

请注意,这段代码有一些问题,我可以帮助你进行修复或提供更高效的方法。如果需要进一步的指导,请告诉我。

英文:

I need to add an element before every occurrence of a particular element. I can do this using for loop and calculate the index and then add the element. Is there any efficient method without using for loop or using streams.

e.g: int[] a = {1,2,4,5,7,9,2,5}
Add element {3} before every occurrence of 2 which result in {1,3,2,4,5,7,9,3,2,5}

Attempt:

int[] a =  {1, 2, 4, 5, 7, 9, 2, 5};
			int[] new1 = null;
			int[] indexToAdd = IntStream.range(0, a.length)
				      .filter(i -&gt; a[i] == 2)
				      .map(i -&gt; i) 
			          .toArray();
			for(int j = 0; j&lt;indexToAdd.length; j++){
				final Integer innerj = new Integer(j);
				new1 = IntStream.range(0,a.length)
						.map(i -&gt; {
							if (i &lt; indexToAdd[innerj]) {
								return a[i];
							} else if (i == indexToAdd[innerj]) {
								return 3 ;
							} else {
								return a[i - 1];
							}
						}).toArray();
			}

答案1

得分: 5

最高效的方法是在运行时创建另一个数组,这样我们始终在数组末尾添加元素,时间复杂度为 O(1),而不是在数组中间添加元素,时间复杂度为 O(n):

    int[] arr1 = {1,2,4,5,7,9,2,5};
    List<Integer> list2 = new ArrayList<>();

    for(int i=0; i < arr1.length; i++)
    {
      int elem = arr1[i];
      if(elem == 2)
      {
        list2.add(3);
      }
      list2.add(elem);
    }
    
    Integer[] arr2 = list2.toArray(new Integer[0]);
    arr1 = Arrays.stream(arr2).mapToInt(i -> i).toArray();

另一种使用流的一行代码:

    int[] arr1 = {1, 2, 4, 5, 7, 9, 2, 5};
    arr1 = Arrays.stream(arr1)
        .flatMap(x -> x == 2 ? Stream.of(3, x).mapToInt(i -> i) : Stream.of(x).mapToInt(i -> i))
        .toArray();
英文:

The most efficient way is to create another array on the fly so that we always add elements at the end which is O(1) as compared to adding elements somewhere in the middle of the array which is O(n):

    int[] arr1 = {1,2,4,5,7,9,2,5};
    List&lt;Integer&gt; list2 = new ArrayList&lt;&gt;();

    for(int i=0; i &lt; arr1.length; i++)
    {
      int elem = arr1[i];
      if(elem == 2)
      {
        list2.add(3);
      }
      list2.add(elem);
    }
    
    Integer[] arr2 = list2.toArray(new Integer[0]);
    arr1 = Arrays.stream(integers).mapToInt(i-&gt;i).toArray();

Another 1 liner using streams:

    int[] arr1 = {1, 2, 4, 5, 7, 9, 2, 5};
    arr1 = Arrays.stream(arr1)
        .flatMap(x -&gt; x == 2 ? Stream.of(3, x).mapToInt(i -&gt; i) : Stream.of(x).mapToInt(i -&gt; i))
        .toArray();

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

发表评论

匿名网友

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

确定