看到没有人和我的方法一样,我来水一发题解吧。
本题解和其他题解不同的是首先考虑了n的范围,然后分别讨论了当n为奇数和偶数时连续自然数段存在的条件。
本题解时间复杂度小于O(n) ,代码一共17行,运行时间21ms。下面先放代码。