PAT 1084 外观数列 运行超时问题
问题描述
提交这道题的时候,第四个测试点提示运行超时,根据内存占用看,应该是输入的N较大,导致运行超时
问题分析
1 | int main() { |
解决运行超时的方法无非就是降低时间复杂度或者优化语句。我当前解决方法的时间复杂度是O(n^2^),也想不出在更低的时间复杂度下解决方法,所以只能通过优化语句来降低运行时间。
解决方法
百度发现有遇到相同问题的同学给出了相关解决方案。我在字符串拼接时用了result[1] = result[1] + current + to_string(num);
而博主给出的解决方案是用temp += x
来替代原来的写法,可以提高运行速度。修改后所由测试点通过。
参考博客:https://blog.csdn.net/qq_42251120/article/details/107406026
本文是原创文章,采用CC BY-NC-SA 4.0协议,完整转载请注明来自DaneSun's Blog
评论
匿名评论
✅ 你无需删除空行,直接评论以获取最佳展示效果