2017-11-08 2 views
1

문자 목록에서 단어 "예"의 가장 오른쪽 항목을 찾기 위해 재귀 알고리즘을 작성했습니다.문자열의 가장 오른쪽 발생 찾기

public class Question7 { 


    public static int where(char[] A, String s, int i) { 
     // A recursive function where() 
     // Return the location of rightmost occurence of a given string s in a given array A 
     // Complete the where function 
     // You may want to add more parameters to the where function 
     s = "yes"; 
     where(A, s, A.length); 
     if (A.length < 3) { 
      return -1; 
     } else if (A.length == 3) { 
      if (A[i - 2] == s.charAt(0) && A[i - 1] == s.charAt(1) && A[i] == s.charAt(2)) { 
       return i - 2; 
      } else { 
       return -1; 
      } 
     } else { 
      if (A[i - 2] == s.charAt(0) && A[i - 1] == s.charAt(1) && A[i] == s.charAt(2)) { 
       return i - 2; 
      } else { 
       return where(A, s, i - 1); 
      } 
     } 

    } 

    public static void main(String[] args) { 
     char[] givenarray = {'o', 't', 'z', 'y', 'e', 's', 'v', 'g', 'r', 'a', 'y', 'e', 's'}; 

     // Test your method 
     System.out.println("The rightmost occurence of 'yes' in the given array is at index " + where()); 
     // Your method should return 10 
    } 

} 

메서드를 호출 할 때 내 문제가 있습니다. 특정 매개 변수 또는 특정 매개 변수를 사용해야합니까? 예를 들면 다음과 같습니다. (givenarray, "yes", givenarray.length) 또는 그냥 (char [] A, String s, int i)? 필자는 매개 변수가있는 메서드를 호출하는 것을 매우 잘 해본 적이 없으므로 어떤 도움도 받으실 수 있습니다!

+0

재귀 (https://www.cs.cmu.edu/~15110-s13/Unit05PtA-handout.pdf)를 살펴보십시오. 여기에 기본 케이스가 없습니다. 재귀 함수를 다시 호출 할 때 문제를 해결하기 위해 전달해야합니다. – ajc

+0

재귀를 사용해야합니까? char 배열을 String으로 변환하고 lastIndexOf를 사용하여이 특정 문제를 해결하면 복잡하게하지 않고 필요한 것을 얻을 수 있습니다. – trappski

+0

@ trappski 재귀를 사용해야합니다. – Katie

답변

2

먼저 반환 할 항목을 이해해야합니다.

- 여기에서는 단어의 마지막 발생 색인을 가져와야합니다. 무엇을해야합니까? 캡처 할 수 있습니다. 그것을 할 수있는 많은 방법이 있습니다. 한 가지 방법은 다음과 같습니다 -

public class Question7 { 
     public static int lastFoundIndex = -1; 
     .. 
    } 

둘째로, 프로세스를 시작하는 방법은 무엇입니까?

public static void main(String... args) { 
     char[] givenArray = {'o', 't', 'z', 'y', 'e', 's', 'v', 'g', 'r', 'a', 'y', 'e', 's'}; 

     // this is the initializing function, where you pass 2 params 
     // The array and Index from where you want to start looking. 
     where(givenArray, 0); 
     System.out.println("The rightmost occurence of 'yes' in the given array is at index "+ lastFoundIndex); 
    } 

다음은 where(..) 기능이 어떻게되는지 보겠습니다.

public static void where(char[] arr, int startIndex) { 
     // this is the base case. First statement. 
     // Basically for testing, if the array is empty or we reached at the end of the execution (we'll reach there soon) 
     if(startIndex >= arr.length) { 
      return; 
     } 

     // Now we check if we have the word 'yes', if so - save the lastFoundIndex. and call where function and tell it to look starting from currentIndex + 3 place. 
     if(arr[startIndex] == 'y' && startIndex + 2 < arr.length) { 
      if(arr[startIndex + 1] == 'e' && arr[startIndex + 2] == 's') { 
       lastFoundIndex = startIndex; 
       where(arr, startIndex+3); 
      } 
     } 

     // if we dont find the character y, then just go on with next char. 
     where(arr, startIndex+1); 
    } 
+0

오, 어디서 잘못되었는지 알 수 있습니다. 예, 나는 색인을 모두 잘못 참조하고있었습니다. 도와 주셔서 감사합니다 !! – Katie