recursive-backtracking

Given an array of integers nums and an integer k, return the total number of continuous subarrays whose sum equals to k

核能气质少年 提交于 2021-02-11 12:46:43
问题 def subarraySum(self, nums: List[int], k: int) -> int: count = 0 target = k self.cal(nums,target,count,k) return count def cal(nums,target, count,k): if target == 0: count = count+1 target = k return count,target if target<0: return for i in range(len(nums)): self.cal(nums[:i]+nums[i+1:],target-nums[i],count,k) ''' here when the target < 0 i want to break the loop and for example if there is array 1,2,3 and my target is 2 i will go to the loop first add 1 next again add 2 which is not

Given an array of integers nums and an integer k, return the total number of continuous subarrays whose sum equals to k

时间秒杀一切 提交于 2021-02-11 12:46:28
问题 def subarraySum(self, nums: List[int], k: int) -> int: count = 0 target = k self.cal(nums,target,count,k) return count def cal(nums,target, count,k): if target == 0: count = count+1 target = k return count,target if target<0: return for i in range(len(nums)): self.cal(nums[:i]+nums[i+1:],target-nums[i],count,k) ''' here when the target < 0 i want to break the loop and for example if there is array 1,2,3 and my target is 2 i will go to the loop first add 1 next again add 2 which is not

Why after pressing semicolon program is back in deep recursion?

强颜欢笑 提交于 2021-02-04 07:34:28
问题 I'm trying to understand the semicolon functionality. I have this code: del(X,[X|Rest],Rest). del(X,[Y|Tail],[Y|Rest]) :- del(X,Tail,Rest). permutation([],[]). permutation(L,[X|P]) :- del(X,L,L1), permutation(L1,P). It's the simple predicate to show all permutations of given list. I used the built-in graphical debugger in SWI-Prolog because I wanted to understand how it works and I understand for the first case which returns the list given in argument. Here is the diagram which I made for

Why after pressing semicolon program is back in deep recursion?

青春壹個敷衍的年華 提交于 2021-02-04 07:34:25
问题 I'm trying to understand the semicolon functionality. I have this code: del(X,[X|Rest],Rest). del(X,[Y|Tail],[Y|Rest]) :- del(X,Tail,Rest). permutation([],[]). permutation(L,[X|P]) :- del(X,L,L1), permutation(L1,P). It's the simple predicate to show all permutations of given list. I used the built-in graphical debugger in SWI-Prolog because I wanted to understand how it works and I understand for the first case which returns the list given in argument. Here is the diagram which I made for

Why after pressing semicolon program is back in deep recursion?

拈花ヽ惹草 提交于 2021-02-04 07:34:11
问题 I'm trying to understand the semicolon functionality. I have this code: del(X,[X|Rest],Rest). del(X,[Y|Tail],[Y|Rest]) :- del(X,Tail,Rest). permutation([],[]). permutation(L,[X|P]) :- del(X,L,L1), permutation(L1,P). It's the simple predicate to show all permutations of given list. I used the built-in graphical debugger in SWI-Prolog because I wanted to understand how it works and I understand for the first case which returns the list given in argument. Here is the diagram which I made for

Knights tour backtracking lasts too long

萝らか妹 提交于 2020-05-08 19:05:06
问题 How long does it last to solve the knights tour problem with backtracking on an 8x8 board? Because my algo allready computes somehow too long and it seems, like it wont finish. But when I try a 6x6, or 5x5 board, it finishes succesfuly. the code: class KnightsTour{ private boolean[][] board; private int count, places; private static final Point[] moves = new Point[]{ new Point(-2, -1), new Point(-2, 1), new Point(2, -1), new Point(2, 1), new Point(-1, -2), new Point(-1, 2), new Point(1, -2),

Backtracking bruteforce Java password cracker

血红的双手。 提交于 2020-01-11 07:39:09
问题 I have this homework assignment to make a recursive method to crack a password of a given length, n (unlimited and unknown!) made of small English letters, a-z ONLY. Here's the class "Password" that creates a random password: import java.util.Random; public class Password { private String _password = ""; public Password(int length) { Random generator = new Random(); for (int i = 0; i < length; ++i) { this._password = this._password + (char) (generator.nextInt(26) + 97); } } public boolean

Constructing a randomised matrix with no duplicates but fixed partial input

人走茶凉 提交于 2019-12-20 01:09:54
问题 I´m facing a problem with constructing a randomised matrix where I partially already have values (that need to stay fixed - so no further randomisation there). Lets see: matrix should end up being 10 by 10 n <- 10 I do want my first rows to be the data I enter. e.g: row1<- c(1,4,7,6,5,3,2,8,9,10) row2<- c(10,7,3,2,1,4,5,9,8,6) row3<- c(9,2,4,3,8,7,10,1,6,5) To bild a matrix with 10 rows (and 10 columns) I combined those rows with samples (no replace because I want each number to be unique in

Longest Common Substring using Recursion and DP

时光毁灭记忆、已成空白 提交于 2019-12-12 02:32:38
问题 I'm trying to find the Longest Common Substring of two strings using Recursion and DP. Please note that I'm not referring to Longest Contiguous subsequence. So, if the two strings were String s1 = "abcdf"; String s2 = "bzcdf" Longest Common Substring == "cdf" (not "bcdf"). Basically they have to be continuous elements I am trying to do this using recursion and backtracking. However, the problem is that if I use a recursion such as below, the +1 are added upfront in a frame, that is higher up

recursive Permutation of a 3 Digit Number

无人久伴 提交于 2019-12-11 13:05:54
问题 I am working on finding All permutations of a 3-Digit Number recursively. I am tired with making up the following permutation Method: static int a = 1; static int b = 2; static int c = 3; static int aCount; static int bCount; static int cCount; static void perm(int a, int b, int c) { Console.WriteLine("( {0}, {1}, {2} )", a, b, c); // (1,2,3 ) if (aCount < 1 && bCount<1 &&cCount<1) { aCount++; perm(a, c, b); } else if (aCount==1 && bCount < 1 && cCount<1) { bCount++; perm(b, a, c); } else if