美文网首页kata
每日kata~04-Sum consecutives-[gith

每日kata~04-Sum consecutives-[gith

作者: Lacia | 来源:发表于2020-04-29 16:15 被阅读0次

    Sum consecutives

    Detail:

    You are given a list/array which contains only integers (positive and negative). Your job is to sum only the numbers that are the same and consecutive. The result should be one list.

    Extra credit if you solve it in one line. You can asume there is never an empty list/array and there will always be an integer.

    Same meaning: 1 == 1

    1 != -1

    Examples:

    [1,4,4,4,0,4,3,3,1] # should return [1,12,0,4,6,1]
    
    """So as you can see sum of consecutives 1 is 1 
    sum of 3 consecutives 4 is 12 
    sum of 0... and sum of 2 
    consecutives 3 is 6 ..."""
    
    [1,1,7,7,3] # should return [2,14,3]
    [-5,-5,7,7,12,0] # should return [-10,14,12,0]
    
    
    Solution:

    明明没看答案就做对了却没给我积分QAQ...

    public static List<Integer> sumConsecutives(List<Integer> s) {
            List<Integer> res = new ArrayList();
            for(int i=0;i<s.size();i++) {
                int value = s.get(i);
                int j = i;
                int k = 0;
                int t = value;
                while((j<(s.size()-2))&&(s.get(j+1)==value)) {
                    t += value;
                    j++;
                }
                res.add(t);
                k++;
                i = j;
            }
            return res;
        }
    
    

    相关文章

      网友评论

        本文标题:每日kata~04-Sum consecutives-[gith

        本文链接:https://www.haomeiwen.com/subject/skknwhtx.html