问题描述
- java面试题金山实习生
-
.输入4个数,构成两个区间,输出他们的整数交集区间。例如:1,10 , 5 , 15.输出【5,10】;写出思路和代码
解决方案
/**
* 前提 a<=b c<=d
*
* @param a
* @param b
* @param c
* @param d
* @return
*/
public static String getResult(int a, int b, int c, int d) {
String result = "";
if (b < c) {
result = "";
}
if (a > d) {
result = "";
}
if (a < c && b > c && b < d) {
result = "[" + c + "," + b + "]";
}
if (a > c && b > d) {
result = "[" + a + "," + d + "]";
}
if (a > c && b < d) {
result = "[" + a + "," + b + "]";
}
if (a < c && b > d) {
result = "[" + c + "," + d + "]";
}
return result;
}
时间: 2024-09-15 01:10:46