classSolution:deffindTargetSumWays(self, nums: List[int], target:int)->int:
s =sum(nums)-abs(target)if s <0or s %2:return0
m = s //2# 背包容量@cache# 缓存装饰器,避免重复计算 dfs 的结果(记忆化)defdfs(i:int, c:int)->int:if i <0:return1if c ==0else0if c < nums[i]:return dfs(i -1, c)# 只能不选return dfs(i -1, c)+ dfs(i -1, c - nums[i])# 不选 + 选return dfs(len(nums)-1, m)
效果展示: 步骤流程:
1.使用的是UGUI,将下面的脚本拖拽到图片上即可。
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;public class CheckImage : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragH…