clear java solution


  • 0
    H

    we calculate the difference between any two consecutive elements and find that for n same difference the total number will be (n-1)+(n-2)+(n-3)...+(1)

    public class Solution {
        public int numberOfArithmeticSlices(int[] A) {
            if(A.length==0)return 0;
            int [] tabs=new int[A.length-1];
            for(int i=1;i<A.length;i++){
                tabs[i-1]=A[i]-A[i-1];
            }
            int ans=0;
            int count=1;
            for(int i=1;i<tabs.length;i++){
    
                if(tabs[i]==tabs[i-1]){
                    ans+=count++;
                }
                else count=1;
            }
            return ans;
            
        }
    }
    

Log in to reply
 

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