target-sum.dart (758B)
1 //Find all possible variations of nums where the values 2 //added together are equal to the target. Achieve 3 //this by adding either a + or - to the front of the value. 4 //This code is the brute force solution that gives a tle in leetcode. 5 //The time complexity of this code is 2^n where n is the length of the list 6 //nums. 7 class Solution { 8 int findTargetSumWays(List<int> nums, int target) { 9 if(nums.length == 0){ 10 if(target == 0){ 11 return 1; 12 } 13 return 0; 14 } 15 int first_val = nums[0]; 16 nums.removeAt(0); 17 int return_val = findTargetSumWays( List.from(nums) , target - first_val); 18 return_val += findTargetSumWays( List.from(nums) , target + first_val ); 19 return return_val;; 20 21 } 22 }