Skip to content

Commit aa287bc

Browse files
committed
494
1 parent cae96a0 commit aa287bc

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

494.target_sum.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int findTargetSumWays(vector<int>& nums, int S) {
4+
unordered_map<int, int> ans;
5+
ans[0] = 1;
6+
for (int n : nums) {
7+
unordered_map<int, int> newAns;
8+
for (auto p : ans) {
9+
int sum = p.first, cnt = p.second;
10+
newAns[sum + n] += cnt;
11+
newAns[sum - n] += cnt;
12+
}
13+
ans = newAns;
14+
}
15+
return ans[S];
16+
}
17+
};

0 commit comments

Comments
 (0)