英文:
Multiplying values in array if same
问题
我想编写一个遵循以下逻辑的递归方法:
假设数组是[3, 3, 3, 3],它将返回一个值为30,因为trail[i]的连续数字相等。所以这里发生的是3 + (3 * 2) + (3 * 3) + (3 * 4) = 30
另一个例子是[2, 4, 3],它将返回一个值为9
希望这有意义
有人可以帮忙吗?
英文:
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
得分: 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);
}
}
并以这种方式调用 sum(0, trail, 0)
英文:
You can try this way
int sum(int pos, int[] trail, int cnt) {
if (pos >= trail.length) { // when full array traversed
return 0;
}
if (pos != 0 && 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
得分: 1
为什么需要递归呢?
使用简单的循环即可完成任务:
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 < 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 -> {
pp[0] = i == 0 || arr[i] == arr[i - 1] ? pp[0] + 1 : 1;
})
.map(i -> pp[0] * arr[i])
.sum();
}
答案3
得分: 1
如其他人已经提到,这可以很容易地编写成一个迭代函数,而不使用递归,但如果出于某种原因你仍然想要一个递归函数,那么它将类似于以下内容:
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);
}
}
注意我们传递了 pow 变量以跟踪整数的重复。如果一个数字不等于前一个数字,我们忽略 pow 并将其设置为 0。如果它等于前一个数字,我们递增 pow 并调用递归函数。
注意: 我没有执行这个代码,可能会有一些拼写错误和错误,但这应该给你一个开始的思路。
英文:
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 >= 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
得分: 0
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<values.length;i++,previousNumber = currentNumber){
currentNumber = values[i];
if(currentNumber == previousNumber){
count++;
}else{
count=1;
}
sum += currentNumber*count;
}
System.out.println("Sum : " + sum);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论