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(object): def getSum(self, a, b): """ :type a: int :type b: int :rtype: int """ # 32 bits integer max MAX = 0x7FFFFFFF # 32 bits interger min MIN = 0x80000000 # mask to get last 32 bits mask = 0xFFFFFFFF while b != 0: # ^ get different bits and & gets double 1s, << moves carry a, b = (a ^ b) & mask, ((a & b) << 1) & mask # if a is negative, get a's 32 bits complement positive first # then get 32-bit positive's Python complement negative return a if a <= MAX else ~(a ^ mask)
相关推荐
Calculate A + B. Each line will contain two integers A and B. Process to end of file. For each case, output A + B in one line.
Given an array of integers where all the numbers are appearing twice find the only two numbers which appears once Chapter 12. Multiply two numbers without using arithmetic operators Chapter 13. ...
Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1 ) which means the number of test cases. Then T lines follow, each ...
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same ...
Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1) which means the number of test cases. Then T lines follow, each ...
Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where...
输入 Input two integers A and B, process to the end of the file. (Watch the Sample Input) 输出 For each case, output A + B in one line.(Watch the Sample Output)
The first line contains two integers n and m (1≤n≤2000,0≤m≤2000) -- the number of vertices and the number of edges. For the next m lines, each line contains two integers ui and vi, which means ...
An Exposition of the Eisenstein Integers An Exposition of the Eisenstein Integers
ADD (): Pops the top two integers from the stack and pushes their sum. SUB (): Pops the top two integers from the stack and pushes the difference (lower minus upper). MUL (): Pops the top two ...
Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the ...
Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t=4, n=6, and the list is [4,3,2,2,1,1], then there are four ...
R code. There are two functions: evenProduct(num) gives product of all even positive integers less then or equal to ...evenProduct(num) gives sum of all even positive integers less then or equal to num
// Addition program that displays the sum of two integers. #include <iostream> // allows program to perform input and output // function main begins program execution int main() { // variable ...
Use an approximate algorithm to find the sum of subsets. S is a set of positive integers. Find a subset of S whose sum should be as large as possible but cannot exceed t
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same ...
Verse Communications84 La EspiralOrinda, CA 94563 USAnate@verse.comABSTRACTSorted lists of integers are commonly used in inverted in- dexes and database systems. They are often compres
That definition seems so simple: they are numbers that cannot be expressed as a ratio of two integers, or that have decimal expansions that are neither infinite nor recurring. But, as The Irrationals...
在多个JVM上分布Java应用程序该库...用法这是一个简单的例子public class Demo { static public interface Worker extends Serializable { // Just compute the sum of two integers public Integer add ( Integer a ,