changgyhub / leetcode_101

LeetCode 101:力扣刷题指南
8.57k stars 1.16k forks source link

好像135题 跑不通 ? #37

Closed 1e0n-xx closed 3 years ago

1e0n-xx commented 3 years ago

如果测试用例是这个的话 [1,2,87,87,87,2,1] 好像跑不过?

1e0n-xx commented 3 years ago

java的版本好像跑不出来?

public int candy(int[] ratings) {

        if(ratings==null || ratings.length == 0)
            return 0;
        int len = ratings.length;
        int [] c = new int [len];
        Arrays.fill(c,1);
        for (int i = 1; i < len; i++) {
            if(ratings[i]>ratings[i-1])
                c[i]++;
        }
        for (int i = len-1; i >0; i--) {
            if (ratings[i] < ratings[i-1]) {
                c[i-1] = Math.max(c[i-1], c[i] + 1);
            }
        }
        int ans = 0;
        for(int i : c)
            ans+=i;
        return ans;
    }
changgyhub commented 3 years ago

您好,第一个for loop应该是c[i] = c[i-1] + 1。 请不要再在issue提帮忙debug的需求,谢谢。