494. 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: 5
Explanation: -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 There are 5 ways to assign symbols to make the sum of nums be target 3.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
种类:看似用DP,但是其实很麻烦
[一句话思路]:
用DFS也能求出种类
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 理解一下退出条件:符合sum = target就count++,达到长度要求就退出
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
[复杂度]:Time complexity: O(每一个都试试加减法 2^n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[算法思想:递归/分治/贪心]:递归
[关键模板化代码]:
数组名、目标、位置、当前和
public void dfs(int[] nums, int target, int pos, int sum) {
//exit
if (pos == nums.length) {
if (sum == target) count++;
return ;
}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
282. Expression Add Operators
[代码风格] :
class Solution {
int count = 0;
public int findTargetSumWays(int[] nums, int S) {
//cc
if (nums == null || nums.length == 0) return 0;
//dfs
dfs(nums, S, 0, 0);
//return
return count;
}
public void dfs(int[] nums, int target, int pos, int sum) {
//exit
if (pos == nums.length) {
if (sum == target) count++;
return ;
}
//dfs
dfs(nums, target, pos + 1, sum + nums[pos]);
dfs(nums, target, pos + 1, sum - nums[pos]);
}
}
494. Target Sum 添加标点符号求和的更多相关文章
- LN : leetcode 494 Target Sum
lc 494 Target Sum 494 Target Sum You are given a list of non-negative integers, a1, a2, ..., an, and ...
- LC 494. Target Sum
问题描述 You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 ...
- [LeetCode] 494. Target Sum 目标和
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...
- 【LeetCode】494. Target Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 494. Target Sum
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...
- 494. Target Sum - Unsolved
https://leetcode.com/problems/target-sum/#/description You are given a list of non-negative integers ...
- 494 Target Sum 目标和
给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在你有两个符号 + 和 -.对于数组中的任意一个整数,你都可以从 + 或 -中选择一个符号添加在前面.返回可以使最终数组和为 ...
- Leetcode 494 Target Sum 动态规划 背包+滚动数据
这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20) ...
- 【leetcode】494. Target Sum
题目如下: 解题思路:这题可以用动态规划来做.记dp[i][j] = x,表示使用nums的第0个到第i个之间的所有元素得到数值j有x种方法,那么很容易得到递推关系式,dp[i][j] = dp[i- ...
随机推荐
- iOS中Cookie的管理
平常的app开发中只调用Rest Api可能用不到Cookie,但是当要在App中内嵌WebView就有可能要用到.最近用到了这一块的东西,总结一下. Cookie原理 关于cookie的原理简单描述 ...
- 1006 Sign In and Sign Out (25)(25 分)思路:普通的时间比较题。。。
1006 Sign In and Sign Out (25)(25 分) At the beginning of every day, the first person who signs in th ...
- MySQL单表多字段模糊查询解决方法 又折磨半天concat(字段不能为空,如为空则用IFNULL(字段,'');
SELECT `id`,`weixin_id`,`user_name`,`sex`,`area_id`,`address_near`,`phone`,`create_time`,`import_use ...
- css移除a标签及map、area(图片热区映射)点击过后的边框
默认a标签及其包含的html元素和map中的area(图片热区映射)在点击过后留有默认的蓝色边框,如下图 可以看到,蓝色的边框破坏了页面的整体美感,很多时候我们都是不需要的.通过设置相应的css可以去 ...
- MapReduce启动的Map/Reduce子任务简要分析
对于Hadoop来说,是通过在DataNode中启动Map/Reduce java进程的方式来实现分布式计算处理的,那么就从源码层简要分析一下hadoop中启动Map/Reduce任务的过程. ...
- 20181104_C#线程之Thread_ThreadPool_使用Thread实现回到和带参数的回调
C# .net Framework多线程演变路径: 1.0 1.1 时代使用Thread 2.0 时代使用ThreadPool 3.0 时代使用Task 4.0 时代使用 ...
- HttpClient基础用法
一.HttpClient HttpClient是Apache HttpComponents 下的子项目,用来提供高效的.最新的.功能丰富的支持HTTP协议的客户端编程工具包(httpclient-4. ...
- Julia - 复合表达式
复合表达式是用一个表达式按照顺序对一系列子表达式求值,并返回最后一个子表达式的值 有两种方法:begin 块和 “;” 链 begin 块 begin 块的多行写法 julia> a = beg ...
- MetaboAnalysis KEGG match methods
1.KEGG regexdb.idmap.find({"name":{"$regex":"Prolyl-hydroxyproline",&q ...
- 千万别在UI线程上调用Control.Invoke和Control.BeginInvoke,因为这些是依然阻塞UI线程的,造成界面的假死
原文地址:https://www.cnblogs.com/wangchuang/archive/2013/02/20/2918858.html .c# Invoke和BeginInvoke 区别 Co ...