Max of 3 Numbers(lintcode 283)

Description

Given 3 integers, return the max of them.

Example

Given num1 = 1, num2 = 9, num3 = 0, return 9.

Interface

public class Solution {
    /**
     * @param num1 an integer
     * @param num2 an integer
     * @param num3 an integer
     * @return an integer
     */
    public int maxOfThreeNumbers(int num1, int num2, int num3) {
        // Write your code here
    }
}

Idea

We can also only use if statement or Math.max() function.

Solution

public class Solution {
    /**
     * @param num1 an integer
     * @param num2 an integer
     * @param num3 an integer
     * @return an integer
     */
    public int maxOfThreeNumbers(int num1, int num2, int num3) {
        // Write your code here
        if (num1 < num2) {
            return Math.max(num2, num3);
        } else {
            return Math.max(num1, num3);
        }
    }
}

results matching ""

    No results matching ""