Closed rygh4775 closed 5 years ago
Approach 1 : use if
def getMaxNum(num1:int, num2:int) -> int:
if num1-num2 < 0 :
return num2
else:
return num1
Approach 2 : use sign info
def getMaxNum(num1:int, num2:int) -> int:
sign = isMinus(num1-num2)
return (sign^1)*num1 + sign*num2
def isMinus(num : int) -> int:
return num >> 31 & 0x1
print (getMaxNum(1,2))
print (getMaxNum(1000,1000))
print (getMaxNum(99,100))
print (getMaxNum(-1,1))
print (getMaxNum(-100,0))
output 2 1000 100 1 0
Number Max: Write a method that finds the maximum of two numbers. You should not use if-else or any other comparison operator