【双指针】【数之和】 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会溢出,所以用减法不会溢出;

相关推荐
kyle~1 小时前
模型预测控制(MPC)
算法·机器人·控制·传感器
牢姐与蒯3 小时前
cpp算法之哈希
c++·算法·哈希算法
开开心心就好3 小时前
两张表格数据对比工具,不用写函数也能查
算法·spring·智能手机·pdf·电脑·intellij-idea·音视频
电科_银尘4 小时前
【大模型算法与工程 02】线性回归与逻辑回归:从损失函数到决策边界
算法·逻辑回归·线性回归
玄昌盛不会编程5 小时前
LeetCode——2243. 计算字符串的数字和
java·算法·leetcode
电科_银尘8 小时前
【大模型算法与工程 01】机器学习的问题定义:监督、无监督、自监督与强化学习
人工智能·算法·机器学习
电科_银尘8 小时前
【大模型算法与工程 05】分类、回归与排序指标:怎样评价一个模型
算法·分类·回归
罗西的思考21 小时前
【轻量化复刻大模型 / KIMI K3】Multi-Token Prediction(MTP / EAGLE-3)(2)
人工智能·算法·机器学习
罗西的思考21 小时前
【轻量化复刻大模型 / KIMI K3】Multi-Token Prediction(MTP / EAGLE-3)(1)
人工智能·算法·机器学习
武子康1 天前
H3-World 接入 Game Agent:谁来判断门真的打开了?
人工智能·算法·机器学习