3ms C++ O(n) time, O(1) space Concise Solution


  • 0
    L
    int numberOfArithmeticSlices(vector<int>& vIn) {
    	int iRes = 0, iNumEndWithPre = 0, iNumEndWithCur = 0;
    	for (size_t i = 2; i < vIn.size(); ++i)
    	{
    		if (vIn[i] + vIn[i - 2] == (vIn[i - 1] << 1))
    		{
    			iNumEndWithCur = iNumEndWithPre + 1;
    		}
    		else
    		{
    			iNumEndWithCur = 0;
    		}
    		iRes += iNumEndWithCur;
    		iNumEndWithPre = iNumEndWithCur;
    	}
    	return iRes;
    }
    

Log in to reply
 

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