leetcode 371 python
2017-02-26 17:28
190 查看
Calculate the sum of two integers a and b, but you are
not allowed to use the operator
Example:
Given a = 1 and b = 2, return 3.
not allowed to use the operator
+and
-.
Example:
Given a = 1 and b = 2, return 3.
class Solution(object): def getSum(self, a, b): """ :type a: int :type b: int :rtype: int """ if b == 0: return a if a == 0: return b c = a ^ b d = 0xFFFFFFFF &((a&b) << 1) if d > 0x7FFFFFFF: s = -(~(d - 1) & 0xFFFFFFFF) else: s = d return Solution().getSum(c,s)http://www.cnblogs.com/zhengyun_ustc/archive/2009/10/14/shifting.html http://stackoverflow.com/questions/7201207/python-bitshift-32-bit-constraint
相关文章推荐
- 【LeetCode with Python】 Wildcard Matching
- 【LeetCode with Python】 Generate Parentheses
- 【LeetCode with Python】 Remove Duplicates from Sorted Array
- 【LeetCode with Python】 Binary Tree Preorder Traversal
- 【LeetCode with Python】 Maximum Depth of Binary Tree
- 【LeetCode with Python】 Binary Tree Inorder Traversal
- 【LeetCode with Python】 Pow(x, n)
- 【LeetCode with Python】 Maximum Subarray
- 【LeetCode with Python】 Insertion Sort List
- 【LeetCode with Python】 Best Time to Buy and Sell Stock
- 【LeetCode with Python】 Swap Nodes in Pairs
- 【LeetCode with Python】 Reverse Words in a String
- 【LeetCode with Python】 Remove Duplicates from Sorted List
- 【LeetCode with Python】 String to Integer (atoi)
- 【LeetCode with Python】 LRU Cache
- 【LeetCode with Python】 Remove Element
- 【LeetCode with Python】 Validate Binary Search Tree
- 【LeetCode with Python】 Pascal's Triangle
- 【LeetCode with Python】 Balanced Binary Tree
- 【LeetCode with Python】 Minimum Depth of Binary Tree