首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除数组的相邻元素

删除数组的相邻元素
EN

Stack Overflow用户
提问于 2017-08-08 13:09:55
回答 2查看 161关注 0票数 1

假设我有一个数组A,我需要删除所有ith元素,前提是ith元素同时具有相邻的元素,即(i-1)th和(i+1)th,并且在找到该元素时还缩小了该数组的大小。此外,如果任何这类元素满足这一条件,则每次也按照公式计算其成本:

成本= (Ai*Ai-1) + (Ai*Ai+1) + (Ai-1*Ai+1);例如:

A= {1,2,3,4}

删除元素‘2’后:a= {1,3,4}

删除元素‘3’后:a= {1,4}

我不知道如何使用ArrayList。有人能指点我用数组的概念来完成这个任务吗?因为我无法解决这个问题。

PS:不是家庭作业

这是我的密码:

代码语言:javascript
复制
import java.util.*;


 class TestClass
{
    public static void main(String args[] ) throws Exception
{

    Scanner sc = new Scanner(System.in);
    int T = sc.nextInt();
    int cost=0;
    for (int i = 0; i < T; i++)   // scanning no. of test cases
     {
       int N = sc.nextInt();  // scanning no. of elements
        int[] A = new int[N];
        for(int j=0 ; j<N ; j++)
        {
          A[j] = sc.nextInt();
        }

        while (A.length>2)
        {
              cost = cost + getResultForLocation(A, 1);
              A = reduceArray(A, 1);
          }
    }

    System.out.println(cost);
}

static int getResultForLocation(int[] array, int location)
{
int sum = 0;

sum = sum + (array[location] * array[location - 1]) + (array[location] * array[location + 1]) + (array[location - 1] * array[location + 1]);

    return sum;
}


static int[] reduceArray (int[] array, int locationToRemove)
 {
if (array==null || array.length<=2)
 {
  return array;
}
if (locationToRemove == array.length || locationToRemove==1)
 {
  return array;
}
int[] returnArray = new int[array.length-1];
for (int i=0;i<locationToRemove;i++)
{
  returnArray[i]=array[i];
}
for (int i=locationToRemove;i<array.length-1;i++)
 {
  returnArray[i]=array[i+1];
}
return returnArray;
}


 }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-08 13:45:51

看看这些方法(从代码中操作):

代码语言:javascript
复制
import java.util.*;


 class TestClass
{
public static void main(String args[]) throws Exception {

    Scanner sc = new Scanner(System.in);
    int numberOfTests = sc.nextInt();
    int cost = 0;
    for (int i = 0; i < numberOfTests; i++) // scanning no. of test cases
    {
      int arraySize = sc.nextInt(); // scanning no. of elements
      int[] array = new int[arraySize];
      for (int j = 0; j < arraySize; j++) {
        array[j] = sc.nextInt();
      }

      while (array.length > 2) {
        cost = cost + getResultForLocation(array, 1);
        array = reduceArray(array, 1);
      }
    }

    System.out.println(cost);
  }

  static int getResultForLocation(int[] array, int location) {

    int cost = 0;
    if (location > 0 && location < array.length - 1) {
      // logic part
      cost = cost + (array[location] * array[location - 1]) + (array[location] * array[location + 1]) + (array[location - 1] * array[location + 1]);
    }
    return cost;
  }

  static int[] reduceArray(int[] array, int locationToRemove) {

    if (array == null || array.length <= 2) {
      return array;
    }
    if (locationToRemove == array.length) {
      return array;
    }
    int[] returnArray = new int[array.length - 1];
    for (int i = 0; i < locationToRemove; i++) {
      returnArray[i] = array[i];
    }
    for (int i = locationToRemove; i < array.length - 1; i++) {
      returnArray[i] = array[i + 1];
    }
    return returnArray;

}

方法getResultForLocation计算位置x中元素的成本,如果它是合法位置,并返回它。

方法reduceArray删除位置x中的元素,并返回一个较小的数组。

在主要部分中,我建立了一个积累的可能性--从第一个合格的位置开始,计算成本,删除项目-然后重复,直到数组减少到两个成员,在每一步累积成本。这里给出的数组的结果是30。

在控制台中输入数字:1 4 1 2 3 4

结果是30

票数 1
EN

Stack Overflow用户

发布于 2017-08-08 13:30:25

你非得用Arraylist吗?那样的话,试试这个。这篇文章很好地解释了Arraylist是如何工作的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45569279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档