aravindbros
BAN USERpublic class Solution {
public static List<List<Integer>> combinationSum(int[] nums, int target, int k){
if(nums.length == 0 || k ==0)
return new ArrayList<List<Integer>>();
List<List<Integer>> result = new ArrayList<List<Integer>>();
combinationSumUtil(nums, k, 0, target, new ArrayList<Integer>(), result);
return result;
}
public static void combinationSumUtil(int[] nums, int k, int start, int target, List<Integer> tempList, List<List<Integer>> result) {
if(k < 0) return;
if(k == 0 && target == 0){
result.add(new ArrayList<Integer>(tempList));
return;
}
for(int i = start; i< nums.length; i++){
tempList.add(nums[i]);
combinationSumUtil(nums, k - 1, i + 1, target - nums[i], tempList, result);
tempList.remove(tempList.size()-1);
}
}
public static void main(String args[]){
int[] nums = {-1,2,-2,3,4,1};
List<List<Integer>> result = combinationSum(nums, 3, 2);
System.out.print(result.toString());
}
}
public class diamondShapeStars {
public static void printStar(int n, int diff, int i){
if(i == n) diff = -diff;
if(i<1) return;
int space = (n-i)/2;
for(int x = 0 ; x < space; x++) System.out.print(" ");
for(int x = 0 ; x < i; x++) System.out.print("*");
for(int x = 0 ; x < space; x++) System.out.print(" ");
System.out.println("");
printStar(n, diff, i+diff);
}
public static void main(String args[]){
printStar(5, 2, 1);
}
}
- aravindbros May 31, 2017