【Leetcode】求1+2+...+n

文章介绍了一种在不允许使用乘除法、循环和条件判断语句的情况下,利用位运算计算1到n的和的方法。通过将等差数列求和公式转化为位操作,手动展开循环,实现了求和计算,并在给定范围内高效运行。

今天刷题看到个有趣的解法,记录下
求 1+2+…+n ,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

没想到还有展开选项:
由等差数列求和公式我们可以知道
1+2+⋯+n 等价于 n(n+1)/2等价于n(n+1)>>1,可以将两个数相乘用加法和位运算来模拟,循环语句,可以通过题目给的【1,10000】判断最大位数14,来手动展开

int a=n,b=n+1,ans=0;

    (b&1) && (ans+=a); //b&1 取最低位
    a<<=1;
    b>>=1;

    (b&1) && (ans+=a);
    a<<=1;
    b>>=1;

    (b&1) && (ans+=a);
    a<<=1;
    b>>=1;

    (b&1) && (ans+=a);
    a<<=1;
    b>>=1;

    (b&1) && (ans+=a);
    a<<=1;
    b>>=1;

    (b&1) && (ans+=a);
    a<<=1;
    b>>=1;

    (b&1) && (ans+=a);
    a<<=1;
    b>>=1;

    (b&1) && (ans+=a);
    a<<=1;
    b>>=1;

    (b&1) && (ans+=a);
    a<<=1;
    b>>=1;

    (b&1) && (ans+=a);
    a<<=1;
    b>>=1;

    (b&1) && (ans+=a);
    a<<=1;
    b>>=1;

    (b&1) && (ans+=a);
    a<<=1;
    b>>=1;

    (b&1) && (ans+=a);
    a<<=1;
    b>>=1;

    (b&1) && (ans+=a);
    a<<=1;
    b>>=1;

    return ans>>1; 

Accepted
35/35 cases passed (4 ms)
Your runtime beats 26.14 % of c submissions
Your memory usage beats 98.15 % of c submissions (5.2 MB)

### LeetCode C++ Problems and Solutions #### Square Root Calculation Implementation For calculating the square root of a number `x` on LeetCode using C++, one approach involves utilizing mathematical functions such as logarithms and exponentials. The implementation checks if `x` is zero to handle edge cases directly returning 0 when true. For other values, it calculates an approximate integer value by taking half the natural logarithm of `x`, then applying exponential function[^1]. Verification ensures that `(ans + 1)` squared does not exceed `x`; otherwise, returns `ans`. ```cpp class Solution { public: int mySqrt(int x) { if (x == 0) { return 0; } int ans = exp(0.5 * log(x)); return ((long long)(ans + 1) * (ans + 1) <= x ? ans + 1 : ans); } }; ``` #### Array Rotation Methodology Regarding array rotation within LeetCode's problem set implemented in C++, this solution rotates elements efficiently without additional space complexity beyond O(1). By reversing parts of the vector after computing effective rotations needed (`k % size`), three reversals achieve desired outcome: full reversal followed by segment-specific ones up until index `k` and from `k` onwards respectively[^2]. ```cpp #include <iostream> #include <vector> using namespace std; class Solution { public: void rotate(vector<int>& nums, int k) { int n = nums.size(); k %= n; reverse(nums.begin(), nums.end()); reverse(nums.begin(), nums.begin() + k); reverse(nums.begin() + k, nums.end()); } }; // Note: Test case verification code omitted for brevity. ``` #### Daily Temperatures Problem Approach In solving the "Daily Temperatures" challenge presented at LeetCode with C language adaptation available but focusing here conceptually similar methods apply including stack-based algorithms or optimized iteration techniques aimed at minimizing node traversals while maintaining efficiency standards expected during interviews preparation phase[^3].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Z_shsf

来包瓜子嘛,谢谢客官~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值