leetcode327 超时大坑

这题使用把两个赋值

        for (int i = st; i <= mid; i++)
        {
            int m = mid;
            int n = mid;
            while (m <= ed && sums[m] - sums[i] < lower) m++;
            while (n <= ed && sums[n] - sums[i] <= upper) n++;
            count += n-m;
        }

像下面这样 换到for循坏外就不会超时了

        int m = mid;
        int n = mid;
        for (int i = st; i < mid; i++)
        {
            while (m < ed && sums[m] - sums[i] < lower) m++;
            while (n < ed && sums[n] - sums[i] <= upper) n++;
            count += n-m;
        }

 

你可能感兴趣的:(算法与数据结构)