Sum of Two Integers
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sum of Two Integers相关的知识,希望对你有一定的参考价值。
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.
个人思路:绕开+、-,利用循环和 += 与 -= 。
class Solution { public: int getSum(int a, int b) { double x = a, y = b; double c = fabs(y); for (double i = 1; i <= c; i++) { if (b > 0) a++; if (b < 0) a--; } return a; } };
错误原因:特殊值的计算。a = 2147483647, b = -2147483647。Time Limit Exceeded
自己在电脑上跑结果是对的,然而循环次数太多了,浪费时间,比较笨的一种方法,年轻人,还是太傻太天真,还需要学习一个。
其他思路:
1.利用下标偏移自带加法 by dimle
int getSum(int a, int b) { auto c = (char*)(a); return (int)((int64_t)(&c[b])); }
把a变成一个地址为a的指针c,把它看成数组,偏移b后,地址变成a + b,再把地址转成int。(补充学习:<inttypes.h>)
2.利用位运算 by vdvvdd
class Solution { public: int getSum(int a, int b) { int sum = a; while (b != 0) { sum = a ^ b;//calculate sum of a and b without thinking the carry b = (a & b) << 1;//calculate the carry a = sum;//add sum(without carry) and carry } return sum; } };
解释:
class Solution { public: int getSum(int a, int b) { // Take 1 + 2 for example in 4 bits. // Same as 0001 + 0010 // Binary addition requires going through each bit position and // generating a carry in bit, carry out bit and sum bit. // For example, in bit position 0 in 0001 + 0010, // carry in: 0, sum bit: carry in + 1 + 0 = 1, carry out: 0 // For bit position 1, // carry in: 0, sum bit: carry in + 0 + 1 = 1, carry out: 0 // Using a truth table, we can figure out that // sum bit = carry in xor bit_a xor bit_b. // carry out = (carry in AND bit_a) OR (carry in AND bit_b) OR (bit_a AND bit_b) // carry out becomes the carry in for the next bit position. int result = 0x0; int sum_bit = 0x0; int carry_bit = 0x0; int bitmask = 0x1; int bit_a = 0x0; int bit_b = 0x0; int i = 0; // Keep track of what bit position im in. while (bitmask != 0x0) { // Isolate bits in each bit position. bit_a = (a & bitmask) >> i; bit_b = (b & bitmask) >> i; // Calculate sum, carry_bit is a carry in now. sum_bit = ((carry_bit ^ bit_a) ^ bit_b); // Shift sum_bit to correct bit position, OR into result. result |= (sum_bit << i); // Calculate carry out, carry_bit is now a carry out after calculation. carry_bit = ((bit_a & bit_b) | (carry_bit & bit_a)) | (carry_bit & bit_b); // Shift bitmask over 1 to the left. bitmask <<= 1; // Increment bit position. ++i; } return result; } };
3. 另一种位运算 by cjchan0210
class Solution { public: int getSum(int a, int b) { if(a && b) return getSum(a^b, (a&b) << 1); else return a|b; } }; viewed as binary, c = (a&b)<<1 means carry bits, and d = a^b means sum without carry obviously... and then get the sum of c and d... if a or b is 0, return a or b, so that is a|b
以上是关于Sum of Two Integers的主要内容,如果未能解决你的问题,请参考以下文章
leetcode371. Sum of Two Integers
LeetCode之371. Sum of Two Integers
LeetCode: 371 Sum of Two Integers(easy)