@XQF
2018-03-07T23:01:12.000000Z
字数 598
阅读 928
数据结构与算法
public class Solution {
//可能溢出
public int max(int a, int b) {
return (a + b + Math.abs(a - b)) / 2;
}
public int min(int a, int b) {
return (a + b - Math.abs(a - b)) / 2;
}
//不会溢出
public int max1(int a, int b) {
return (int) ((long) a + (long) b + Math.abs((long) a - (long) b)) / 2;
}
public int min1(int a, int b) {
return (int) ((long) a + (long) b - Math.abs((long) a - (long) b)) / 2;
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.max(3, 5));
System.out.println(solution.min(3, 5));
System.out.println(solution.max1(3, 5));
System.out.println(solution.min1(3, 5));
}
}