LeetCode之Sum of Two Integers
发布日期:2021-06-29 14:08:15 浏览次数:2 分类:技术文章

本文共 534 字,大约阅读时间需要 1 分钟。

1、题目

 

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

Example:

Given a = 1 and b = 2, return 3.

Credits:

Special thanks to for adding this problem and creating all test cases.

to see which companies asked this question.

 

 

 

 

2、代码实现

 

public class Solution {    public int getSum(int a, int b) {    	 while(b != 0)      	    {      	        int tmp =(a & b) << 1;     	        //a是每次相加,相当于和    	        a = a ^ b;    	        //b相当于是进位    	        b = tmp;    	    }      	    return a;       }}

 

 

 

 

 

 

 

转载地址:https://chenyu.blog.csdn.net/article/details/69661874 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode之Maximum Depth of Binary Tree
下一篇:LeetCode之Detect Capital

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年05月01日 15时10分07秒