java - When to use ArrayList over array in recursion -


so have problem. trying code program print valid possible arrangements of brackets i.e. 3 brackets can have ((())), (()()), ()()(), (())() etc.

i have working code

public static void main(string[] args) {     int number = 3; // no. of brackets     int cn = number;     int on = number;     // open , closed brackets respectively     char[] sol = new char[6];     printsol(on, cn, sol, 0); }  public static void printsol(int cn, int on, char[] sol, int k) {     if (on == 0 && cn == 0) {         system.out.println("");         (int = 0; < 6; i++) {             system.out.print(sol[i]);         }     }      else {         if (on > 0) {             if (cn > 0) {                 sol[k] = '(';                 printsol(on - 1, cn, sol, k + 1);             }         }          if (cn > on) {             sol[k] = ')';             printsol(on, cn - 1, sol, k + 1);         }     } } 

now problem want using arraylist instead of using char array. tried getting errors. if has suggestions please let me know. main purpose of asking question want know when shall prefer arraylists on arrays in recursion problems.

p.s. sorry poor indentation had type whole program due surfing restrictions , thre might syntax errors tried best. mohit

i think you're doing fine using char[]. it's quick , it's point.

i'd recursion problems face in practice don't follow pattern. that's because typically problems demanding recursion you're performing search on search tree 1 specific goal (one specific leaf node on tree). you're performing iteration: you're trying visit every leaf node on tree, , perform action each.

with common search algorithms (like depth-first search), wouldn't need prepare result recurse, rather unwind, after you've found goal.

but cases do, char[] works great. you're simulating stack through parameters sol , k (sol holds data while k points top of stack). others have noticed, use stack directly (by passing deque<character> implementation, commonly linkedlist).

in mind arraylist step backwards. if you're going use collection, use 1 made problem.

edit: here's untested implementation using deque instead:

public static void printsol(int cn, int on, deque<character> sol) {     if (on == 0 && cn == 0) {         system.out.println("");         ( iterator<character> = sol.descendingiterator(); it.hasnext(); ) {             system.out.println(it.next());         }     }      else {         if (on > 0) {             if (cn > 0) {                 sol.push('(');                 printsol(on - 1, cn, sol);                 sol.pop();             }         }          if (cn > on) {             sol.push(')');             printsol(on, cn - 1, sol);             sol.pop();         }     } }  //... printsol(3, 3, new arraydeque<character>(6)); 

as can see, few changes.

edit 2: 1 thing haven't discussed @ specific problem stringbuilder.

stringbuilder mutable string type allows append , remove characters. great solution problem well:

public static void printsol(int cn, int on, stringbuilder sol) {     if (on == 0 && cn == 0) {         system.out.println(sol);     }      else {         if (on > 0) {             if (cn > 0) {                 sol.append('(');                 printsol(on - 1, cn, sol);                 sol.deletecharat(sol.length()-1);             }         }          if (cn > on) {             sol.append(')');             printsol(on, cn - 1, sol);             sol.deletecharat(sol.length()-1);         }     } } 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -