Share my accepted EZ understood recursion version java solution


  • 0
    J
    public class Solution extends VersionControl {
      public int firstBadVersion(int n) {
          if(n==1) return n;
          return firstHelper(1,n);
      }
      public int firstHelper(int start, int end){
          if(start==end) return start;
          int mid=start+ (end-start)/2;
          if(isBadVersion(mid) && !isBadVersion(mid-1)) return mid;
          if(isBadVersion(mid) && isBadVersion(mid-1))  return firstHelper(start, mid-1);
          return firstHelper(mid+1, end);
      }
    }

  • 0
    H

    Do you pass the test?

    Iw rote similar code but encountered stack overflow in a test case.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.