记录Java递归作业之解决特殊子集求和问题

1.跳过相邻数字的子集求和问题:

给定一个整数列表,我们想知道是否有可能选择 某些整数的子集,

从而使子集中的整数递归地累 加到给定的和。

我们还希望,如果选择一个整数作为求和,

则必 须跳过列表中该整数旁边的整数,而不是选择该 整数作为求和。

不要使用任何循环。

上代码:

package SkipSum;

import java.util.List;

public class SkipSum {
    /**
     * Decide whether there is a subset in the input list
     * that adds up to the target sum, where adjacent integers
     * in the list must not be both in the subset.
     * For example, skipSum([2, 5, 10], 12) → true,
     * and skipSum([2, 5, 10], 7) → false.
     * @param list is the input list.
     * @param sum is the target sum.
     * @return true iff there is a subset of non-adjacent integers
     * in the list that adds to sum.
     */
    public static boolean skipSum(List<Integer> list, int sum) {
        // call your recursive helper method
        return skipSumHelper(list, 0, sum);
    }

    private static boolean skipSumHelper(List<Integer> list, int start, int sum) {
        // base case
        if(list==null||list.isEmpty()||list.size()==0) {
        	if(sum==0) return true;
        	return false;
        }
        if(start>=list.size()) {
        	if(sum==0) return true;
        	return false;
        }
        // recursive step
        if(sum-list.get(start)<0) return skipSumHelper(list, start+1, sum);
        return skipSumHelper(list, start+2, sum-list.get(start))||skipSumHelper(list, start+1, sum);	
    }
}

2.集合分割为元素和为奇数的子集及元素和为10的倍数的子集

给定一个整数列表,您想知道是否可以将整数分成两个集合

这样一个集合的和是奇数,

而另一 个集合的和是 10 的倍数。

每个整数必须在一个或另一个集合中。

你可以写一个递归的 helper 方法,它接受任意数 量的参数,然后在方法内部调用它,

但是你不能 使用任何循环。

上代码:

package oddAndTen;

import java.util.List;

public class OddAndTen {
    /**
     * Decide if it is possible to divide the integers in a list into two sets,
     * so that the sum of one set is odd, and the sum of the other set is a multiple of 10.
     * Every integer must be in one set or the other.
     * For example, oddAndTen([5, 5, 3])  →  true,
     * and oddAndTen([5, 5, 4])  →  false.
     * @param list is a list of integers.
     * @return true iff there is one odd partition and the other multiple of 10.
     */
    public static boolean oddAndTen(List<Integer> list) {
        // call your recursive helper method
        return oddAndTenHelper(0,list,0,0);
    }

    private static boolean oddAndTenHelper(int index,List<Integer> list,int odd,int ten) { // add any parameters
        // base case
        if(list==null||list.isEmpty()||list.size()==0||list.size()==1) return false;
		if(index>=list.size()) {
			if((ten%10==0)&&(odd%2!=0)) return true;
			return false;
		}
        // recursive step
        if((ten+list.get(index))%10==0) {
			return oddAndTenHelper(index+1, list, odd, ten+list.get(index));
		}else {
			return oddAndTenHelper(index+1, list, odd+list.get(index), ten);
		}
    }
}

3.给定一个整数列表,您想知道是否可以将整数分成两个集合,以便两个集合的和是相同的。

每个 整数必须在一个或另一个集合中。

编写一个递归 的 helper 方法,它可以接受任意数量的参数, 并从 equalSum()对递归 helper method 进行 初始调用。

不要使用任何循环。

 

上代码:

package EqualSum;
import java.util.List;
import java.util.ArrayList;

public class EqualSum {
    
    /**
     * Decide if it is possible to divide the integers in a list into two sets,
     * so that the sums of the two sets are the same.
     * Every integer must be in one set or the other.
     * For example, equalSum([2, 3, 5])  →  true.
     * @param list is a list of integers.
     * @return true iff there are two sets having the same sum.
     */
    public static boolean equalSum(List<Integer> list) {
        // call your recursive helper method
        return equalSumHelper(list, 0, 0, 0);
    }

    private static boolean equalSumHelper(List<Integer> list,int start,int sum1,int sum2) { // add any parameters
        // base case
        if(list==null||list.isEmpty()) return true;
        if(list.size()==1) {
        	if(list.get(0)==0) return true;
        	return false;
        }
    	if(list.size()==2) return (list.get(0)==list.get(1)||(list.get(0)+list.get(1))==0)?true:false;
        if(start>=list.size()) {
        	if(sum1==sum2) return true;
        	else return false;
        }
        // recursive step
        return equalSumHelper(list, start+1, sum1+list.get(start), sum2)||equalSumHelper(list, start+1, sum1, sum2+list.get(start));	
    }

}

4.给定一个输入字符串,完成一个 helper 方法,

该 方法提取元音并递归返回输入字符串中的元音 字符串。

所有字符串都是小写的。 不要在代码中使用任何循环。

英文中元音字母有5个:A\E\I\O\U

上代码:

package ExtractVowel;

public class ExtractVowel {
    /**
     * Extract the vowels from the input string.
     * For example, extractVowel("i love you 3000") → "ioeou".
     * @param str is the input string.
     * @return the vowels of the input string.
     */
    public static String extractVowel(String str) {
		// call your recursive helper method
        return extractVowelHelper(str, 0, "");
    }

    private static String extractVowelHelper(String str, int start, String vowels) {
        // base case
        if(str==null) return null;
        if(start>=str.length()||"".equals(str)) return vowels;
        // recursive step
        String cut = str.substring(start,start+1);
		if(isVowel(cut.charAt(0))) vowels+=cut;
		return extractVowelHelper(str.substring(start+1), 0, vowels);	
    }

    private static boolean isVowel(char c) {
        return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章