Java simple n^2 solution


  • 0
    L
        public int numberOfArithmeticSlices(int[] A) {
        if (A.length < 3) {
          return 0;
        }
        int count = 0;
        for (int j = 1; j < A.length; j++) {
          for (int i = j; i < A.length - 1; i++) {
            if (A[i] - A[i - 1] == A[i + 1] - A[i])  {
              count++;
            }
            else{
              break;
            }
          }
        }
        return count;    
    }
    

Log in to reply
 

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