# 题目

• 给当前结果添加一个数位（i + 1）的成本为 cost[i] （cost 数组下标从 0 开始）。
• 总成本必须恰好等于 target 。
• 添加的数位中没有数字 0 。

输入：cost = [4,3,2,5,6,7,2,5,5], target = 9

数字     成本
1  ->   4
2  ->   3
3  ->   2
4  ->   5
5  ->   6
6  ->   7
7  ->   2
8  ->   5
9  ->   5


输入：cost = [7,6,5,5,5,6,8,7,8], target = 12



输入：cost = [2,4,6,2,4,6,4,4,4], target = 5



输入：cost = [6,10,15,40,40,40,40,40,40], target = 47



• cost.length == 9
• 1 <= cost[i] <= 5000
• 1 <= target <= 5000

# 题目

输入：n = 12输出：3 解释：12 = 4 + 4 + 4

输入：n = 13输出：2解释：13 = 4 + 9

• 1 <= n <= 104

# 思路分析

dp[i]的意义 i最少可以由dp[i]个完全平方数组成

# C++代码

class Solution {public:    int numSquares(int n) {        ve

# 题目

输入: amount = 5, coins = [1, 2, 5]

5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1


输入: amount = 3, coins = [2]



输入: amount = 10, coins = [10]



• 0 <= amount (总金额) <= 5000
• 1 <= coin (硬币面额) <= 5000
• 硬币种类不超过 500 种
• 结果符合 32 位符号整数

# 思路分析

dp[i]自然表示的就是总金额为i元时的方案数。

# C++代码

# Rust代码rustimpl Solution {    pub fn change(amount: i32, coins: Vec<i32>) -> i32 {        let amount = amount as usize;        let mut v = vec

# 题目

输入：n = 5, minProfit = 3, group = [2,2], profit = [2,3]

输入：n = 10, minProfit = 5, group = [2,3,5], profit = [6,7,8]

• 1 <= n <= 100
• 0 <= minProfit <= 100
• 1 <= group.length <= 100
• 1 <= group[i] <= 100
• profit.length == group.length
• 0 <= profit[i] <= 100

# 题目

• 如果 x == y，那么两块石头都会被完全粉碎；
• 如果 x != y，那么重量为 x 的石头将会完全粉碎，而重量为 y 的石头新重量为 y-x

输入：stones = [2,7,4,1,8,1]



输入：stones = [31,26,33,21,40]



输入：stones = [1,2]



• 1 <= stones.length <= 30
• 1 <= stones[i] <= 100

# 题目

• 例如，nums = [2, 1] ，可以在 2 之前添加 '+' ，在 1 之前添加 '-' ，然后串联起来得到表达式 "+2-1"

输入：nums = [1,1,1,1,1], target = 3

-1 + 1 + 1 + 1 + 1 = 3
+1 - 1 + 1 + 1 + 1 = 3
+1 + 1 - 1 + 1 + 1 = 3
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3


输入：nums = [1], target = 1



• 1 <= nums.length <= 20
• 0 <= nums[i] <= 1000
• 0 <= sum(nums[i]) <= 1000
• -1000 <= target <= 100

# 思路分析

class Solution {public:    int findTargetSumWays(vector<int>& nums, int target) {        unordered_map<int, int> m;        m[0] = 1;        for (auto & num : nums) {            unordered_map<int, int> mx;            for (auto & p

# 题目

输入：head = [1,2,6,3,4,5,6], val = 6



输入：head = [], val = 1



输入：head = [7,7,7,7], val = 7



• 列表中的节点在范围 [0, 104]
• 1 <= Node.val <= 50
• 0 <= k <= 50

# C++代码

class Solution {public:    ListNode* removeElements(ListNode* head, int val) {        ListNode * watch = new ListNode(0);        watch->next = head;        ListNode*  cur = watch;        while (cur->next) {    

# 题目

输入：intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3



输入：intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1



输入：intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2



• listA 中节点数目为 m
• listB 中节点数目为 n

# 题目

输入: nums = [0,1]

输入: nums = [0,1,0]

• 1 <= nums.length <= 105
• nums[i] 不是 0 就是 1

0
1
0
1
1
1
0
0
0比1多的数量
0
1
-1
-2

# C++代码

class Solution {public:    int findMaxLength(vector<int>& nums) {        unordered_map<int, int> m;        int res = 0;        m[0] = 0;        vector<int> prefix(nums.size() + 1);        for (int i = 0; i < nums.size(); ++i) {            prefix[i + 1] = (prefix[i] + nums[i] * 2 - 1);            if (m.find(prefix[i + 1]) != m.end()) {                res = max(res, i + 1 - m[prefix[i + 1]]);            } else {                m[prefix[i + 1]] = i + 1;             }        }      

# 题目

• 子数组大小 至少为 2 ，且
• 子数组元素总和为 k 的倍数。

输入：nums = [23,2,4,6,7], k = 6

输入：nums = [23,2,6,4,7], k = 6

42 是 6 的倍数，因为 42 = 7 * 6 且 7 是一个整数。


输入：nums = [23,2,6,4,7], k = 13



• 1 <= nums.length <= 105
• 0 <= nums[i] <= 109
• 0 <= sum(nums[i]) <= 231 - 1
• 1 <= k <= 231 - 1