# 在数组中如果相同则相乘值

go评论104阅读模式

Multiplying values in array if same

# 问题

I wanna write a recursive method that follows this logic:

suppose if the array is [3, 3, 3, 3] it will return a value of 30 because the consecutive number for trail[i] are equal to each other. So what happens here is 3 + (3 * 2) + (3 * 3) + (3 * 4) = 30

Another example is [2, 4, 3] it will return a value of 9

I hope this makes sense

Can anyone help?

# 答案1

``````  int sum(int pos, int[] trail, int cnt) {
if (pos >= trail.length) {   // 当完整数组遍历时
return 0;
}
if (pos != 0 && trail[pos - 1] == trail[pos]) {         // 如果前一个元素相同
return (cnt + 1) * trail[pos] + sum(pos + 1, trail, cnt + 1);
} else {                                                // 第一个元素或前一个不相同
return trail[pos] + sum(pos + 1, trail, 1);
}
}
``````

You can try this way

``````  int sum(int pos, int[] trail, int cnt) {
if (pos &gt;= trail.length) {   // when full array traversed
return 0;
}
if (pos != 0 &amp;&amp; trail[pos - 1] == trail[pos]) {         // if previous element is same
return (cnt + 1) * trail[pos] + sum(pos + 1, trail, cnt + 1);
} else {                                                // first element or prev not same
return trail[pos] + sum(pos + 1, trail, 1);
}
}
``````

And call this way `sum(0, trail, 0)`

# 答案2

``````public int sum(int[] arr) {
int sum = 0;
for (int i = 0, p = 0; i < arr.length; i++) {
if (i == 0 || arr[i] == arr[i - 1]) {
p++;
} else {
p = 1;
}
sum += arr[i] * p;
}
return sum;
}
``````

Java 8 Stream API 可以用来产生相同的结果：

``````public int sumStream(int[] arr) {
int[] pp = {0};

return IntStream.range(0, arr.length)
// 更新第 i 个元素的商
.peek(i -> {
pp[0] = i == 0 || arr[i] == arr[i - 1] ? pp[0] + 1 : 1;
})
.map(i -> pp[0] * arr[i])
.sum();
}
``````

Why do you need recursion for this?

Simple loop should do the job:

``````public int sum(int[] arr) {
int sum = 0;
for (int i = 0, p = 0; i &lt; arr.length; i++) {
if (i == 0 || arr[i] == arr[i - 1]) {
p++;
} else {
p = 1;
}
sum += arr[i] * p;
}
return sum;
}
``````

update

Java 8 Stream API may be used to produce the same result:

``````public int sumStream(int[] arr) {
int[] pp = {0};

return IntStream.range(0, arr.length)
// update the quotient for the i-th element
.peek(i -&gt; {
pp[0] = i == 0 || arr[i] == arr[i - 1] ? pp[0] + 1 : 1;
})
.map(i -&gt; pp[0] * arr[i])
.sum();
}
``````

# 答案3

``````int recursiveHelper(int[] nums, int index, int pow) {
if (index >= nums.length) return 0;

if (index == 0)
return nums[0] + recursiveHelper(nums, index + 1, 0);
else {
if (nums[index] == nums[index - 1])
return nums[index] * pow + recursiveHelper(nums, index, pow + 1);
else
return nums[index] + recursiveHelper(nums, index + 1, 0);
}
}
``````

As others already mentioned that this could be easily written as an interative function without using recursion but if for some reason you still want a recursive function then it will be something like below:

``````int recursiveHelper(int[] nums, int index, int pow){
if(index &gt;= nums.length) return 0;

if(index == 0)
return nums[0] + recursiveHelper(nums, index+1,0);
else{
if(nums[index] == nums[index-1])
return nums[index] * pow + recursiveHelper(nums, index, pow+1);
else
return nums[index] + recursiveHelper(nums, index+1,0);

}

}
``````

Notice how we pass the pow variable to track the repetition of integers. If a number is not equal to its previous number, we ignore pow and set it 0. If it is equal to previous number, we increment pow and call the recursive function.

Note : I didn't execute this, there may be some typos and errors here but this should give you an idea on how to start.

# 答案4

``````int[] values = new int[]{3,3,3,3};
int currentNumber=0,previousNumber=-1,count=1,sum=0;

for(int i = 0; i<values.length;i++,previousNumber = currentNumber){
currentNumber = values[i];
if(currentNumber == previousNumber){
count++;
}else{
count=1;
}
sum += currentNumber*count;
}
System.out.println("Sum : " + sum);
``````

``````        int[] values = new int[]{3,3,3,3};
int currentNumber=0,previousNumber=-1,count=1,sum=0;

for(int i = 0; i&lt;values.length;i++,previousNumber = currentNumber){
currentNumber = values[i];
if(currentNumber == previousNumber){
count++;
}else{
count=1;
}
sum += currentNumber*count;
}
System.out.println(&quot;Sum : &quot; + sum);
``````

• 本文由 发表于 2020年7月29日 12:46:46
• 转载请务必保留本文链接：https://go.coder-hub.com/63146513.html
• java

go 48

go 54

go 52

go 48