linecode 1. A + B 问题 java实现

本文介绍了一种不使用传统的加法运算符实现两整数相加的方法。通过位运算中的异或和按位与操作结合左移实现加法运算。此方法适用于32位整数相加。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符。

注意事项

你不需要从输入流读入数据,只需要根据aplusb的两个参数a和b,计算他们的和并返回就行。

说明

a和b都是 32位 整数么?

  • 是的

我可以使用位运算符么?

  • 当然可以
样例

如果 a=1 并且 b=2,返回3


思路:利用二进制异或(^)运算实现相加但是相加的结果不包含进位;

利用按位相与(&)为真时左移进位

public class Solution {
    /**
     * @param a: An integer
     * @param b: An integer
     * @return: The sum of a and b
     */
   int aplusb(int a, int b) {
    // write your code here
    while(b!=0){
        //异或运算,不进位
        int sum =a^b;
        //移位运算
        int Displacement=(a&b)<<1;
        a=sum;
        b=Displacement;
    }
    return a;
  }
}

[LOG-PATH]: /opt/dolphinscheduler/logs/20250625/18033207638820_32-350-1135.log, [HOST]: Host{address='dolphinscheduler-worker-0.dolphinscheduler-worker-headless:1234', ip='dolphinscheduler-worker-0.dolphinscheduler-worker-headless', port=1234} [INFO] 2025-06-25 09:33:27.612 +0800 - Begin to pulling task [INFO] 2025-06-25 09:33:27.616 +0800 - Begin to initialize task [INFO] 2025-06-25 09:33:27.620 +0800 - Set task startTime: Wed Jun 25 09:33:27 CST 2025 [INFO] 2025-06-25 09:33:27.620 +0800 - Set task envFile: /opt/dolphinscheduler/conf/dolphinscheduler_env.sh [INFO] 2025-06-25 09:33:27.622 +0800 - Set task appId: 350_1135 这日志输出的不是具体的值是字母 # 定义目标时间(与SQL中的格式保持一致) target_time="2025-03-10 16:20:23" current_time="2025-06-25 09:33:09" # 计算目标时间的时间戳(秒) target_ts=$(date -d "$target_time" +%s 2>/dev/null) # 获取当前时间的时间戳(秒) current_ts=$(date -d "$current_time" +%s 2>/dev/null) # 计算时间差(秒) diff_seconds=$((current_ts - target_ts)) # 输出结果(与SQL逻辑一致) echo '${setValue(TIME_DIFF=$ diff_seconds)}' [INFO] 2025-06-25 09:33:27.623 +0800 - End initialize task [INFO] 2025-06-25 09:33:27.627 +0800 - Set task status to TaskExecutionStatus{code=1, desc='running'} [INFO] 2025-06-25 09:33:27.627 +0800 - TenantCode:liwang check success [INFO] 2025-06-25 09:33:27.629 +0800 - ProcessExecDir:/tmp/dolphinscheduler/exec/process/liwang/15121783010432/18033207638820_32/350/1135 check success [INFO] 2025-06-25 09:33:27.629 +0800 - Resources:{} check success [INFO] 2025-06-25 09:33:27.629 +0800 - Task plugin: SHELL create success [INFO] 2025-06-25 09:33:27.629 +0800 - shell task params {"localParams":[],"rawScript":"echo ${TIME_DIFF}","resourceList":[]} [INFO] 2025-06-25 09:33:27.629 +0800 - Success initialized task plugin instance success [INFO] 2025-06-25 09:33:27.629 +0800 - Success set taskVarPool: [{"prop":"TIME_DIFF","direct":"IN","type":"INTEGER","value":"$ diff_seconds"},{"prop":"SYSTIME","direct":"IN","type":"TIME","value":"2025-06-25 09:33:09"},{"prop":"LAST_UPDATE_DATE","direct":"IN","type":"TIME","value":"2025-03-10 16:20:23"}] [INFO] 2025-06-25 09:33:27.632 +0800 - raw script : echo $ diff_seconds [INFO] 2025-06-25 09:33:27.632 +0800 - task execute path : /tmp/dolphinscheduler/exec/process/liwang/15121783010432/18033207638820_32/350/1135 [INFO] 2025-06-25 09:33:27.637 +0800 - Begin to create command file:/tmp/dolphinscheduler/exec/process/liwang/15121783010432/18033207638820_32/350/1135/350_1135.command [INFO] 2025-06-25 09:33:27.642 +0800 - Success create command file, command: #!/bin/bash BASEDIR=$(cd `dirname $0`; pwd) cd $BASEDIR source /opt/dolphinscheduler/conf/dolphinscheduler_env.sh /tmp/dolphinscheduler/exec/process/liwang/15121783010432/18033207638820_32/350/1135/350_1135_node.sh [INFO] 2025-06-25 09:33:27.651 +0800 - task run command: sudo -u liwang -i bash /tmp/dolphinscheduler/exec/process/liwang/15121783010432/18033207638820_32/350/1135/350_1135.command [INFO] 2025-06-25 09:33:27.651 +0800 - process start, process id is: 210283 [INFO] 2025-06-25 09:33:27.665 +0800 - process has exited. execute path:/tmp/dolphinscheduler/exec/process/liwang/15121783010432/18033207638820_32/350/1135, processId:210283 ,exitStatusCode:0 ,processWaitForStatus:true ,processExitValue:0 [INFO] 2025-06-25 09:33:27.666 +0800 - Send task execute result to master, the current task status: TaskExecutionStatus{code=7, desc='success'} [INFO] 2025-06-25 09:33:27.668 +0800 - Remove the current task execute context from worker cache [INFO] 2025-06-25 09:33:27.668 +0800 - The current execute mode isn't develop mode, will clear the task execute file: /tmp/dolphinscheduler/exec/process/liwang/15121783010432/18033207638820_32/350/1135 [INFO] 2025-06-25 09:33:27.674 +0800 - Success clear the task execute file: /tmp/dolphinscheduler/exec/process/liwang/15121783010432/18033207638820_32/350/1135 [INFO] 2025-06-25 09:33:28.653 +0800 - -> $ diff_seconds [INFO] 2025-06-25 09:33:28.654 +0800 - FINALIZE_SESSION
最新发布
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值