当前位置:网站首页>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.
边栏推荐
- 【自然语言处理】【向量表示】PairSupCon:用于句子表示的成对监督对比学习
- 关于npm/cnpm/npx/pnpm与yarn
- 测试/开发程序员值这么多钱么?“我“不会愿赌服输......
- We used 48h to co-create a web game: Dice Crush, to participate in international competitions
- 多线程与高并发(五)—— 源码解析 ReentrantLock
- 30分钟使用百度EasyDL实现健康码/行程码智能识别
- 转铁蛋白修饰长春新碱-粉防己碱脂质体|转铁蛋白修饰共载紫杉醇和金雀异黄素脂质体(试剂)
- Metasploit——渗透攻击模块(Exploit)
- flask装饰器版登录、session
- servlet映射路径匹配解析
猜你喜欢
力扣150-逆波兰表达式求值——栈实现
spark学习笔记(九)——sparkSQL核心编程-DataFrame/DataSet/DF、DS、RDD三者之间的转换关系
几行深度学习代码设计包含功能位点的候选免疫原、酶活性位点、蛋白结合蛋白、金属配位蛋白
Site Architecture Detection & Chrome Plugin for Information Gathering
Hangdian Multi-School Seven 1003-Counting Stickmen (Combination Mathematics)
Solution for thread not gc-safe when Rider debugs ASP.NET Core
史上最全GIS相关软件(CAD、FME、Arcgis、ArcgisPro)
机器学习|模型评估——AUC
转铁蛋白(Tf)修饰去氢骆驼蓬碱磁纳米脂质体/香豆素-6脂质体/多柔比星脂质体
laya打包发布apk
随机推荐
The servlet mapping path matching resolution
基于TCP的聊天系统
Tf铁蛋白颗粒包载顺铂/奥沙利铂/阿霉素/甲氨蝶呤MTX/紫杉醇PTX等药物
血红素-金纳米颗粒(Heme-AuNP)复合纳米酶|金纳米颗粒核多孔空心碳纳米球壳([email protected])纳米酶
QoS Quality of Service Six Router Congestion Management
@Autowired annotation --required a single bean, but 2 were found causes and solutions
MATLAB设计,FPGA实现,联合ISE和Modelsim仿真的FIR滤波器设计
《分布式微服务电商》专题(一)-项目简介
网站架构探测&chrome插件用于信息收集
测试/开发程序员值这么多钱么?“我“不会愿赌服输......
Modern Privacy-Preserving Record Linkage Techniques: An Overview论文总结
Pt/CeO2 monatomic nanoparticles enzyme | H - rGO - Pt @ Pd NPs enzyme | carbon nanotube load platinum nanoparticles peptide modified nano enzyme | leukemia antagonism FeOPtPEG composite nano enzyme
Ferritin particle-loaded raltitrexed/pemetrexed/sulfadesoxine/adamantane (scientific research reagent)
About npm/cnpm/npx/pnpm and yarn
QoS服务质量七交换机拥塞管理
laya打包发布apk
redis如何查看key的有效期
Optimization is a habit The starting point is to 'stand close to the critical'
西安凯新(CAS:2408831-65-0)Biotin-PEG4-Acrylamide 特性
[教你做小游戏] 斗地主的手牌,如何布局?看25万粉游戏区UP主怎么说