【双指针】【数之和】 LeetCode 633.平方数之和

算法思想:

  • 双指针枚举i,j;类似三数之和
cpp 复制代码
class Solution {
public:
    bool judgeSquareSum(int c) {
        long long  sum=0;
        vector<int> dp;dp.push_back(0);
        long long start=1;
        while(sum < c)
        {
            sum = start *start;
            if(sum>c) break;
            else dp.push_back(sum);
            start++;

        }
        long long l=0,r=dp.size()-1;
        while(l<=r)
        {
            //long long tmp =dp[l] + dp[r]; 会溢出
            if(dp[l] < c-dp[r])
            {
                l++;
                continue;
            }
            else if (dp[l] > c - dp[r])
            {
                r--;
                continue;
            }
            else if(dp[l] == c-dp[r]) return true;
        }
        return false;

    }
};

但注意一点,dpl + dpr会溢出,所以用减法不会溢出;

相关推荐
飞Link2 小时前
什么是 AirSim?一文读懂无人机仿真平台
python·算法·无人机
一去不复返的通信er4 小时前
贪心算法-合并区间(LeetCode 56)
算法·leetcode·贪心算法
Shaoshing5 小时前
LeetCode100 49. 字母异位词分组
java·算法
YFJ_mily5 小时前
第二届区块链技术与基础模型国际学术会议(BTFM2027)
算法·区块链·能源·智能系统·rdlink研发家·机器人自动化
汤姆宝库6 小时前
收官深拆:第一性原理、产业电网与未来推演
大数据·人工智能·算法
moshuying8 小时前
块扩散 + GDN 改造:Clover 把「并行生成」塞进了 Agent 的长上下文里
前端·算法
没落之王8 小时前
历史系统建模:宋初太祖太宗「再立天庭」秩序重构推演
人工智能·算法·重构
INGNIGHT9 小时前
91 · 最小调整代价(序列dp)
数据结构·算法·leetcode
moshuying9 小时前
扩散模型开始写代码、开网页了:350 亿参数,速度比 Jev 快一倍
前端·算法