Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
18 lines (16 loc) · 591 Bytes

File metadata and controls

18 lines (16 loc) · 591 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package problems;
public class LeetCode494 {
int count = 0;
public int findTargetSumWays(int[] nums, int target) {
backtrace(nums, target, 0, 0);
return count;
}
private void backtrace(int[] nums, int target, int index, int sum) {
if(index == nums.length) { // 题目要求将全部整数联合起来,因此条件必须是 index == nums.length
if(sum == target) count++;
return;
}
backtrace(nums, target, index + 1, sum + nums[index]);
backtrace(nums, target, index + 1, sum - nums[index]);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.