当前位置:网站首页>1D Array Dynamics and Question Answers
1D Array Dynamics and Question Answers
2022-08-10 19:57:00 【Small question mark we are friends】
Given the stem:
Gives you an array nums .The formula for calculating the "dynamic sum" of the array is: runningSum[i] = sum(nums[0]…nums[i]) .
Please return the dynamic sum of nums.
Title source:
LeetCode
1. Example
The code is as follows (example):
Input: nums = [1,1,1,1,1]Output: [1,2,3,4,5]Explanation: The dynamic sum calculation process is [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1] .2. Answers and NotesRemember
The code is as follows (Java):
class Solution {public int[] runningSum(int[] nums) {int len = nums.length;//Solution 1//create an empty arrayint[] resSum = new int[len];// Traverse the given array, the first element of the array is directly put into the new array, and the subsequent elements are added to the previous element of the new array each time.for(int i = 0; i < len; i++) {if(i == 0) {resSum[i] = nums[i];}else {resSum[i] = resSum[i - 1] + nums[i];}}return resSum;//Solution 2//On the basis of the original array, in-situ traversal and modification are performed, the first element is not modified, and the subsequent elements are added to the previous element each time.for(int i = 0; i < len; i++) {if(i != 0) {nums[i] += nums[i - 1];}}return nums;}}Summary
The above is what I want to talk about today. This article introduces the solution to the dynamic sum of one-dimensional arrays. Here is a memo for reference.
边栏推荐
猜你喜欢

2022杭电多校七 Black Magic (签到)

机器学习|模型评估——AUC

@Autowired注解 --required a single bean, but 2 were found出现的原因以及解决方法

spark学习笔记(九)——sparkSQL核心编程-DataFrame/DataSet/DF、DS、RDD三者之间的转换关系

越折腾越好用的 3 款开源 APP

【自然语言处理】【向量表示】PairSupCon:用于句子表示的成对监督对比学习

电脑重装系统Win11格式化硬盘的详细方法
[email protected]纳米模拟酶|PtCo合金纳米粒子"/>水溶性合金量子点纳米酶|CuMoS纳米酶|多孔硅基Pt(Au)纳米酶|[email protected]纳米模拟酶|PtCo合金纳米粒子

QoS服务质量八拥塞避免

转铁蛋白(TF)修饰紫杉醇(PTX)脂质体(TF-PTX-LP)|转铁蛋白(Tf)修饰姜黄素脂质体
随机推荐
365天挑战LeetCode1000题——Day 053 求解方程 解析 模拟
多功能纳米酶Ag/PANI|柔性衬底纳米ZnO酶|铑片纳米酶|Ag-Rh合金纳米颗粒纳米酶|铱钌合金/氧化铱仿生纳米酶
水溶性合金量子点纳米酶|CuMoS纳米酶|多孔硅基Pt(Au)纳米酶|[email protected]纳米模拟酶|PtCo合金纳米粒子
大家要的Biotin-PEG3-Br/acid/NHS ester/alcohol/amine合集分享
西安凯新(CAS:2408831-65-0)Biotin-PEG4-Acrylamide 特性
转铁蛋白Tf功能化β-榄香烯-雷公藤红素/紫杉醇PLGA纳米粒/雷公藤甲素脂质体(化学试剂)
[CNN] Brush SOTA's trick
[Go WebSocket] 你的第一个Go WebSocket服务: echo server
转铁蛋白(TF)修饰紫杉醇(PTX)脂质体(TF-PTX-LP)|转铁蛋白(Tf)修饰姜黄素脂质体
苹果字体查找
主动信息收集
FEMRL: A Framework for Large-Scale Privacy-Preserving Linkage of Patients’ Electronic Health Rec论文总结
一维数组动态和问题答记
《分布式微服务电商》专题(一)-项目简介
spark学习笔记(九)——sparkSQL核心编程-DataFrame/DataSet/DF、DS、RDD三者之间的转换关系
【自然语言处理】【向量表示】PairSupCon:用于句子表示的成对监督对比学习
Introduction to 3 d games beginners essential 】 【 modeling knowledge
西安Biotin-PEG8-IA_IA-PEG8-生物素供应商
What is the upstream bandwidth and downstream bandwidth of the server?
Keras深度学习实战(17)——使用U-Net架构进行图像分割