Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

sum of 1D array(JAVA)

import java.util.Arrays;
import java.util.Scanner;

public class LeetCode_RunningSum {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int[] arr = {1,2,3,4};

        System.out.println(Arrays.toString(out(arr)));

    }

    static int[] out(int[] nums){

        int sum = 0;
        int[] arr = new int[nums.length];

        for (int i = 0; i < nums.length; i++){
            sum += nums[i];
            arr[i] = sum;
        }

        return arr;
    }
}
Comment

Running Sum of 1d Array Java

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

java running sum of array

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

// Example: runningSum([1,3,6,9]) = [1, 4, 10, 19] = [1, 1+3, 1+3+6, 1+3+6+9]
Comment

PREVIOUS NEXT
Code Example
Java :: get preference value android 
Java :: check if sqlexception is duplicate entry java 
Java :: volley dependency 
Java :: java convert a string to char[] 
Java :: get random number from enum in java 
Java :: java copy file 
Java :: spring.jpa.properties.hibernate 
Java :: split number java 
Java :: fhow to find location of java jdk 
Java :: find first element of list java 
Java :: how to print something in java 
Java :: random in java a to b 
Java :: Duration class java 
Java :: number of digits java 
Java :: java get number out of string 
Java :: java switch case 
Java :: PathProviderPlugin.java uses unchecked or unsafe operations. 
Java :: java unit test an api 
Java :: how to return array in java 
Java :: change string to char array 
Java :: AndroidManifest.xml could not be found. 
Java :: matrix multiplication javascrpt 
Java :: difference between object and class 
Java :: set textView.android:drawableEnd programmatically 
Java :: arraylist .set java 
Java :: find elements in selenium 
Java :: override onbackpressed in fragment 
Java :: How to efficiently find the area of largest rectangle that can be formed by adjacent buildings with known heights, in Java? 
Java :: android maven dependency 
Java :: java datatypes 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =