我是靠谱客的博主 炙热唇膏,这篇文章主要介绍两层for循环的双指针问题 导致的超时,现在分享给大家,希望可以做个参考。

可以将for循环变化成为 while循环

eg: 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c 。   力扣:633 平方数之和

class Solution {
public boolean judgeSquareSum(int c) {
// //会超时
// long temp = (long)Math.sqrt(c);
// for(int i = 0;i<=temp;i++){
//
for(int j = 0 ;j<=temp;j++){
//
if(i*i+j*j == c){
//
return true;
//
}
//
}
// }
// return false;
//修改
int i = 0;//左指针
int j = (int)Math.sqrt(c);
while(i<=j){
if(c - j*j == i*i){
return true;
}else if(c - j*j<i*i){
j--;
}else{
i++;
}
}
// for(int i = 0;i*i<=c;i++){
//
long temp = (long)Math.sqrt(c);
//
if(temp*temp == c-i*i){
//
return true;
//
}
// }
return false;
}
}

 

最后

以上就是炙热唇膏最近收集整理的关于两层for循环的双指针问题 导致的超时的全部内容,更多相关两层for循环的双指针问题内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(96)

评论列表共有 0 条评论

立即
投稿
返回
顶部