diff --git a/alg.md b/alg.md new file mode 100644 index 0000000000000000000000000000000000000000..062cae857f2d9384bf09329b9c9a110c233591e7 --- /dev/null +++ b/alg.md @@ -0,0 +1,4 @@ +问题:给你一个数组 nums 。数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]…nums[i]) 。
+请返回 nums 的动态和。
+解题:考虑到题目里动态数组runningSum和nums数组的元素数一样,且runningSum[i] = sum(nums[0]…nums[i]).
+我们从第一个元素遍历nums数组,用runningSum[i]=runningSum[i]+runningSum[i-1]记录此位置及前面所有位置的和,即runningSum存储了runningSum数组的前n-1位和
diff --git a/alg.py b/alg.py new file mode 100644 index 0000000000000000000000000000000000000000..6e24208a120d90057e09c8891ed50391e373886f --- /dev/null +++ b/alg.py @@ -0,0 +1,7 @@ +import gen +class Solution: + def runningSum(self, nums: list[int]) -> list[int]: + n=len(nums) + for i in range(1,n): + nums[i]=nums[i]+nums[i-1] + return nums \ No newline at end of file diff --git a/gen.py b/gen.py new file mode 100644 index 0000000000000000000000000000000000000000..e03e07ee1fdd7a80b4a36d037795d8f8c08dada7 --- /dev/null +++ b/gen.py @@ -0,0 +1,7 @@ +#size=input("请输入数组长度") +# nums=list() +# for i in range(1,size): +# nums[i-1]=input('请输入数组元素:') +num = input( "你的银行卡密码是:" ) +print( "你的银行卡密码的类型是:" , type( num ) ) + \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391