当前位置:网站首页>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.
边栏推荐
- 【SemiDrive源码分析】【MailBox核间通信】52 - DCF Notify 实现原理分析 及 代码实战
- 多线程与高并发(五)—— 源码解析 ReentrantLock
- 优化是一种习惯●出发点是'站在靠近临界'的地方
- 陕西CAS:1244028-50-9_Biotin-PEG3-SCO-PPh3 固体
- 力扣150-逆波兰表达式求值——栈实现
- The servlet mapping path matching resolution
- 西安凯新(CAS:2408831-65-0)Biotin-PEG4-Acrylamide 特性
- 电脑开不了机是什么原因?
- IIC通信协议总结[通俗易懂]
- 【毕业设计】基于STM32的天气预报盒子 - 嵌入式 单片机 物联网
猜你喜欢
随机推荐
烟雾、空气质量、温湿度…自己徒手做个环境检测设备
cordova 安装错误 Command failed: powershell 解决方法
spark学习笔记(九)——sparkSQL核心编程-DataFrame/DataSet/DF、DS、RDD三者之间的转换关系
opengrok搭建[通俗易懂]
mysql踩坑----case when then用法
端口探测详解
新建离线同步节点时选择数据去向-表时报错,数据库类型是adb pg,怎么办?
QoS Quality of Service Seven Switch Congestion Management
TikTok选品有什么技巧?
Modern Privacy-Preserving Record Linkage Techniques: An Overview论文总结
LeetCode·283.移除零·双指针
flask的配置文件
七月券商金工精选
Rider调试ASP.NET Core时报thread not gc-safe的解决方法
Redis persistence mechanism
杭电多校七 1003-Counting Stickmen(组合数学)
【LeetCode】42、接雨水
从 Delta 2.0 开始聊聊我们需要怎样的数据湖
手把手教你Charles抓包工具使用
Leetcode 200.岛屿数量 BFS