输出列表中的所有元素python_程序在Python列表中查找所有每个元素的串联对的总和?...

假设我们有一个称为nums的数字列表。我们必须找到以数字为单位的每对数字的每个串联的总和。在这里,对(i,j)和对(j,i)被认为是不同的。

因此,如果输入类似于nums = [5,3],则输出将为176,因为我们有以下串联:(nums [0] + nums [0])=(5 concat 5)= 55,( nums [0] + nums [1])=(5 concat 3)= 53(nums [1] + nums [0])=(3 concat 5)= 35,(nums [0] + nums [0]) =(3 concat 3)= 33,则总和为55 + 53 + 35 + 33 = 176

为了解决这个问题,我们将按照以下步骤操作:memo := a new map

nums1 := nums

temp := 0

c := sum of all elements in nums1

a := size of nums

for i in range 0 to a, do

if nums[i] is same as 0, then

temp := temp + c

otherwise,

if nums[i] is present in memo, then

temp := temp + memo[nums[i]]

otherwise,

b := 0

for j in range 0 to a, do

b := b + integer of (nums[i] concatenate nums1[j])

memo[nums[i]] := b

temp := temp + memo[nums[i]]

return temp

让我们看下面的实现以更好地理解:

示例class Solution:

def solve(self, nums):

memo = {}

nums1 = nums

temp = 0

c = sum(nums1)

a = len(nums)

for i in range(a):

if nums[i] == 0:

temp += c

else:

if nums[i] in memo:

temp += memo[nums[i]]

else:

b = 0

for j in range(a):

b += int(str(nums[i]) + str(nums1[j]))

memo[nums[i]] = b

temp += memo[nums[i]]

return temp

ob = Solution()nums = [5, 3]

print(ob.solve(nums))

输入值[5, 3]

输出结果176

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值