Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Running Sum of 1d Array

class Solution {
    public int[] runningSum(int[] nums) {
    int[] ans = new int[nums.length];
    ans[0] = nums[0];
    for (int i = 1; i < nums.length; i++)
        ans[i] = ans[i-1] + nums[i];
    return ans;
    }
}
Comment

Running sum of 1d array

// Running sum of 1d array

// Iterative
fn running_sum1(nums: Vec<i32>) -> Vec<i32> {
    let mut arr = vec![nums[0]];
    for i in 1..nums.len() {
        arr.push(nums[i] + arr[i - 1]);
    }
    arr
}

// Functional
fn running_sum2(nums: Vec<i32>) -> Vec<i32> {
	let mut sum = 0;
	nums.into_iter().map(|i| {sum += i; sum}).collect::<Vec<i32>>()
}

fn main() {
    println!("Running sum = {:?} ", running_sum1(vec![4,1,7,10,1]));
    println!("Running sum = {:?} ", running_sum2(vec![4,1,7,10,1]));    
}
Comment

running sum of array

Input: nums = [1,1,1,1,1]
Output: [1,2,3,4,5]
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
Comment

PREVIOUS NEXT
Code Example
Java :: java remove space at the end of string 
Java :: java check if number is in range 
Java :: how to set radio button checked in android programmatically 
Java :: reverse a integer in java 
Java :: java collapse string array 
Java :: operador ternario java 
Java :: cls java 
Java :: print java 
Java :: assert throws exception java 
Java :: integer to string java 
Java :: java how to convert string to int 
Java :: hello world java code 
Java :: date minus date java 
Java :: Getting Pending intent for 12 version 
Java :: code for checking android build version 
Java :: count occurrences of character in string java 8 
Java :: how to remove null values in java 
Java :: java extract zip 
Java :: iterate through an arraylist java 
Java :: intellisense for eclipse java 
Java :: synchronized java 
Java :: car class java 
Java :: object vs class c# 
Java :: java binart tree print 
Java :: java print formatted string 
Java :: check folder for files java 
Java :: how to go to next iteration of while loop java 
Java :: java new char array 
Java :: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver 
Java :: java vector push_back 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =