国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[leetcode]494. Target Sum

2019-11-14 09:15:39
字體:
來源:轉載
供稿:網友

題目鏈接:https://leetcode.com/PRoblems/target-sum/

You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.

Find out how many ways to assign symbols to make sum of integers equal to target S.

Example 1:

Input: nums is [1, 1, 1, 1, 1], S is 3. Output: 5Explanation: -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 = 3There are 5 ways to assign symbols to make the sum of nums be target 3.

Note:

The length of the given array is positive and will not exceed 20.The sum of elements in the given array will not exceed 1000.Your output answer is guaranteed to be fitted in a 32-bit integer.

class Solution{public:    int findTargetSumWays(vector<int>& nums,int S)    {        int count=0;        dfs(nums,count,0,0,S);        return count;    }    void dfs(vector<int>& nums,int &count,int sum,int index,int S)    {        if (index==nums.size())        {            if(sum==S)                count+=1;            return;        }        dfs(nums,count,sum+nums[index],index+1,S);        dfs(nums,count,sum-nums[index],index+1,S);    }};
上一篇:函數模板

下一篇:藍橋杯題庫算法練習

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广宁县| 东兰县| 赣州市| 南投市| 南安市| 长泰县| 崇文区| 日土县| 泰宁县| 常宁市| 奇台县| 眉山市| 拜城县| 特克斯县| 葫芦岛市| 灵川县| 鲁甸县| 六安市| 白朗县| 福泉市| 宁明县| 鞍山市| 珲春市| 乌拉特后旗| 福贡县| 枝江市| 津南区| 马山县| 正定县| 通海县| 和平区| 璧山县| 连南| 巩留县| 社会| 阿鲁科尔沁旗| 肃北| 长汀县| 江北区| 玛沁县| 香港|