This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package compareToProject; | |
class Rectangle { | |
public int width, height; | |
public Rectangle(int w, int h) { | |
width = Math.max(0, w); | |
height = Math.max(0, h); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Permutations { | |
public static void main(String[] args) { | |
int n = 5; | |
for(int i=0; i < factorial(n); i++) | |
System.out.println("P_"+i+": "+getPermutation(n, i, getSymbolString(n))); | |
} | |
public static String getPermutation(int n, int k, String s) { | |
if(n==1 || k==0) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RecursionTraceString { | |
public static void main(String[] args) { | |
String str = "stressed"; | |
String rev = reverse(str); | |
System.out.println(rev); | |
} | |
public static String reverse(String s) { | |
if(s==null || s.length() < 2) { | |
return s; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RecursionTraceNumerical { | |
public static void main(String[] args) { | |
int n = 18672; | |
int totalEven = sumEvenDigits(n); | |
System.out.println(totalEven); | |
} | |
public static int sumEvenDigits(int val) { | |
if(val == 0) { | |
return 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RecursionTraceMix { | |
public static void main(String[] args) { | |
int n = 19; | |
int to = 3; | |
String s = convert(n, to); | |
System.out.println(s); | |
} | |
public static String convert(int decVal, int baseTo) { | |
if(decVal == 0) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Rectangle { | |
public int width, height; | |
public Rectangle(int w, int h) { | |
width = w; | |
height = h; | |
} | |
} | |
public class MemoryDiagrams { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class RecursionBacktracking { | |
public static ArrayList<Integer> itemsAddingUpto(ArrayList<Integer> list, int start, int target) { | |
if(target == 0) { | |
return new ArrayList<Integer>(); | |
} | |
if(start >= list.size()) { | |
return null; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StringRecursion { | |
public static String reverse(String str) { | |
if(str == null || str.length() < 2) { | |
return str; | |
} | |
//string has at least 2 characters | |
char first = str.charAt(0); | |
char last = str.charAt(str.length()-1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NumericalRecursion { | |
public static boolean isSquare(int n) { | |
return Math.pow((int)Math.sqrt(n), 2) == n; | |
} | |
public static int countSquares(int low, int high) { | |
if(low > high) { | |
return 0; | |
} | |
boolean lowSq = isSquare(low); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
class MyArrayList { | |
public double[] data; | |
public int nItems; | |
public MyArrayList() { | |
data = new double[5]; | |
nItems = 0; //no item added yet | |
} |