题目描述

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

示例:

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

题目链接:https://leetcode-cn.com/problems/maximum-lcci/

题解

这道题说实话我一时间没有思路,突然在一个Python的老哥那里找到了想法,用异常处理,通过新建数组是否发生异常检测数值的正负,从而判断大小。虽然有点曲线救国,甚至有点骚,但是好在也能跑,感觉还挺神奇的,就记录一下。当然这不是题目想要我们去干的事情,但是起码他通过了,而且甚至还有点牛逼。

  • 执行用时:1 ms, 在所有 Java 提交中击败了100.00%的用户
  • 内存消耗:35.2 MB, 在所有 Java 提交中击败了69.71%的用户

代码

class Solution {
    public static int maximum(int a, int b) {
        long la = a;
        long lb = b;
        int[] temp;
        try {
            temp = new int[a%10];
            //a大于0
            try {
                int c = (int) ((la - lb) % 10);
                temp = new int[c];
                return a;
            } catch (Exception e) {
                return b;
            }
        } catch (Exception e) {
            try {
                temp = new int[b%10];
                return b;
            } catch (Exception e1) {
                try {
                    int c = (int) ((la - lb) % 10);
                    temp = new int[c-1];
                    return a;
                } catch (Exception e2) {
                    return b;
                }
            }
        }
    }
}
最后修改:2021 年 04 月 30 日
如果觉得我的文章对你有用,请随意赞赏