From d8cde716893121498a6f7928177d3c5e37fe9423 Mon Sep 17 00:00:00 2001 From: duncanmichel <25329325+duncanmichel@users.noreply.github.com> Date: Thu, 2 May 2019 14:52:11 -0400 Subject: [PATCH 1/2] Create SumOfTwoIntegers.py --- LeetCode/SumOfTwoIntegers.py | 72 ++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 LeetCode/SumOfTwoIntegers.py diff --git a/LeetCode/SumOfTwoIntegers.py b/LeetCode/SumOfTwoIntegers.py new file mode 100644 index 0000000..06bbc85 --- /dev/null +++ b/LeetCode/SumOfTwoIntegers.py @@ -0,0 +1,72 @@ +""" +Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. +Example 1: +Input: a = 1, b = 2 +Output: 3 +Example 2: +Input: a = -2, b = 3 +Output: 1 +""" + +class Solution(object): + def getSum(self, a, b): + def plus(x,y): + carry = 0 + place = 1 + ans = 0 + while x > 0 and y > 0: + xdigit = x%2 + ydigit = y%2 + thisdigit = xdigit^ydigit^carry + carry = 1 if (xdigit & ydigit)|(xdigit & carry)|(ydigit & carry) else 0 + thisdigit *= place + ans ^= thisdigit + place <<= 1 + x //= 2 + y //= 2 + while x > 0: + temp = (x%2 ^ carry) * place + ans ^= temp + place <<= 1 + carry = 1 if x%2 & carry else 0 + x //= 2 + while y > 0: + temp = (y%2 ^ carry) * place + ans ^= temp + place <<= 1 + carry = 1 if y%2 & carry else 0 + y //= 2 + return ans + + def minus(x,y): + return 0 + + astr = bin(a) + bstr = bin(b) + if astr[0] == "-" and bstr[0] == "-": + return -1 * plus(abs(a),abs(b)) + elif astr[0] == "-": + if abs(a) > b: + return -1 * minus(abs(a),b) + else: + return minus(b,abs(a)) + elif bstr[0] == "-": + if abs(b) > a: + return -1 * minus(abs(b),a) + else: + return minus(a,abs(b)) + + return plus(a,b) + +""" +MY Solution: + +""" + +""" +Fastest Solution (): + + +Smallest Memory (): + +""" From c8d482a10283c295d761147d44febd305b4c1522 Mon Sep 17 00:00:00 2001 From: duncanmichel <25329325+duncanmichel@users.noreply.github.com> Date: Thu, 2 May 2019 15:40:07 -0400 Subject: [PATCH 2/2] Update SumOfTwoIntegers.py --- LeetCode/SumOfTwoIntegers.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/LeetCode/SumOfTwoIntegers.py b/LeetCode/SumOfTwoIntegers.py index 06bbc85..e33559a 100644 --- a/LeetCode/SumOfTwoIntegers.py +++ b/LeetCode/SumOfTwoIntegers.py @@ -8,9 +8,7 @@ Output: 1 """ -class Solution(object): - def getSum(self, a, b): - def plus(x,y): +def plus(x,y): carry = 0 place = 1 ans = 0 @@ -38,8 +36,32 @@ def plus(x,y): y //= 2 return ans - def minus(x,y): - return 0 + def minus(larger,smaller): + print(larger,"minus",smaller) + mask = 1 + place = 1 + ans = 0 + while larger > 0 and smaller > 0: + ldigit = larger % 2 + sdigit = smaller % 2 + if not mask: + if ldigit: mask = 1 + ldigit ^= mask + temp = (ldigit ^ sdigit) * place + ans ^= temp + place <<= 1 + larger //= 2 + smaller //= 2 + while larger > 0: + ldigit = larger % 2 + if not mask: + if ldigit: mask = 1 + ldigit ^= mask + temp = ldigit * place + ans ^= temp + place <<= 1 + larger //= 2 + return ans astr = bin(a) bstr = bin(b)