You and a friend have received a special gold chain as a gift. The chain links each have an integer weight, not necessarily the same.
You and your friend must choose one of the links to be removed and provided to charity, after which the chain will be reconnected. After that, you can choose one place along the chain to split it into two, such that it creates two equally weighted sections for you and your friend to take home.
Given an input chain (a list of link weights), determine if a solution is possible.
这道题要求判断:在金链中先移除一段链接并重新连接后,能否再切成两段,使两边总重量相等。核心思路通常是先计算整条链的总和,再枚举“删除哪一段链接”以及“切割位置”是否能满足左右两部分重量相同。由于链表是线性结构,前缀和可以帮助快速判断某个切点左右的重量差;如果题目允许删除任意一段,则可以结合前缀和与后缀和检查删除后剩余部分是否存在一个平衡切分点。实现时重点是把“删除”和“重新切分”这两个操作转化为可计算的区间和问题,适合用数组遍历、前缀和或哈希辅助完成。