記錄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';
    }
}

 

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