我有一個在線自學功課總和要做: 查找單詞列表中的單詞。查找字符列表中的單詞
eg Input: COOL
List of characters: {A,B,C,O,L,M,O,L,F} return true
Input : Book
List of characters: {A,B,C,O,L,M,O,L,F} return false (as K is not present).
Note: C found at index 2, then O should be found at index > 2 (Linear searching, search from left to right).
I could think of two solutions, both brute force.
1. Using Brute force approach to get the output
2. Using Recursion (not sure if this approach is right, however, I am expected to solve it using dynamic programming).
不是動態規劃方面的專家,請耐心等待。 的解決方案,我想出了:
public boolean found(final String input,int n, boolean isFound, int a, String words) {
if(n == input.length) {
return isFound;
}
char charValue = input.charAt(n);
for(int i = a; i<words.length-1; i++) {
if(charValue == words.charAt[i]) {
isFound = true;
return found(input, n+1, true, i+1; words);
}else {
isFound = false;
}
}
return isFound;
}
我不知道這個解決方案有效,需要嘗試它的IDE。不過,我期待這一點可以通過動態編程來解決。我沒有看到我可以將輸入保存在緩存/內存中以便再次使用。
感謝您的解決方案。我正在參加Udemy課程,在動態編程部分,講師提到這是一個家庭作業總和。 – LifeStartsAtHelloWorld