367.有效的完全平方数
题目链接
static const auto io_speed_up = []()
{
ios::sync_with_stdio(0);
cin.tie(0);
return 0;
}();
class Solution {
public:
bool isPerfectSquare(int num) {
double ans = num;
while(ans * ans - num > 0.5){
ans = (ans + num/ans) / 2.0;
}
if((int)ans * (int)ans == num)return 1;
return 0;
}
};
371.两整数之和
题目链接
没有-0这一说,所以会报错,所以用unsigned int
class Solution {
public:
int getSum(int a, int b) {
int c;
while(b){
c = (unsigned int)(a&b)<<1;
a ^= b;
b = c;
}
return a;
}
};

本文解析了两个C++算法题目,包括判断有效完全平方数的算法实现和使用位运算进行两整数相加的方法。通过具体的代码示例,详细解释了算法的逻辑和优化技巧。
1155

被折叠的 条评论
为什么被折叠?



