您当前的位置: 首页 > 

IT之一小佬

暂无认证

  • 0浏览

    0关注

    1192博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

最大数值(不能使用比较运算符)

IT之一小佬 发布时间:2021-06-30 10:33:10 ,浏览量:0

编写一个方法,找出两个数字ab中最大的那一个。不得使用if-else或其他比较运算符。

示例:

输入: a = 1, b = 2
输出: 2

示例代码1:

class Solution(object):
    def maximum(self, a, b):
        """
        :type a: int
        :type b: int
        :rtype: int
        """
        li = []
        li.append(a)
        li.append(b)
        li.sort(reverse=True)
        return li[0]

这里想到了列表的排序,利用列表的sort方法对列表进行排序,再把最大数取出来

示例代码2:

class Solution(object):
    def maximum(self, a, b):
        """
        :type a: int
        :type b: int
        :rtype: int
        """
        return ((a+b)+abs(a-b))/2
关注
打赏
1665675218
查看更多评论
立即登录/注册

微信扫码登录

0.0450s