首页 > 其他 > 详细

arithmetic-slices

时间:2016-10-16 16:44:47      阅读:132      评论:0      收藏:0      [点我收藏+]

 

https://leetcode.com/problems/arithmetic-slices/

public class Solution {
    public int numberOfArithmeticSlices(int[] A) {
        if (A.length < 3) {
            return 0;
        }

        int lastLen = 1;
        int lastDiff = A[1] - A[0];

        int ret = 0;
        for (int i=2; i<A.length; i++) {
            if (A[i] - A[i-1] == lastDiff) {
                // 这一步,很重要,因为增加的个数正好是lastLen
                ret += lastLen;
                lastLen++;
            }
            else {
                lastLen = 1;
                lastDiff = A[i] - A[i-1];
            }
        }
        return ret;
    }
}

 

arithmetic-slices

原文:http://www.cnblogs.com/charlesblc/p/5966646.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!