首页 > 其他 > 详细

leetcode 中等难度

时间:2021-09-02 06:24:24      阅读:8      评论:0      收藏:0      [点我收藏+]

L494

//方法一:回溯法
class Solution {
    public static int findTargetSumWays(int[] nums, int target) {
        cnt =0;
        recurve(nums, 0, 0, target);
        return cnt;
    }

    static int cnt = 0;

    public static void recurve(int[] nums, int n, int sum, int target) {
        if (n == nums.length) {
            if (sum == target) cnt++;
            return;
        }
        recurve(nums, n + 1, sum + nums[n], target);
        recurve(nums, n + 1, sum - nums[n], target);
    }
}
//方法二:深度优先搜索

leetcode 中等难度

原文:https://www.cnblogs.com/zhouyu0-0/p/15213242.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!