my java 3 ms solution using recursive


  • 0
    J

    public class Solution {
    public int numberOfArithmeticSlices(int[] A) {

    if (A.length<3) return 0;
    List<Integer> results=new ArrayList<>();
    int len=0;
    for(int i=1;i<A.length-1;i++){
    	if(A[i]-A[i-1]==A[i+1]-A[i]){
    		len++;
    	}else{
    		if(len!=0) {
    			results.add(len);
    			len=0;
    		}
    	}
    	
    	
    }
    if(len!=0) results.add(len);
    int total=0;
    for(int i:results){
    	System.out.println(i);
    	total+=getCount(i);
    }
    return total;
    
    }
    

    public int getCount(int len){
    if(len==1)
    return 1;
    return getCount(len-1)+len;
    }
    }


Log in to reply
 

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