您的位置:首页 > 产品设计 > UI/UE

Increasing Triplet Subsequence

2016-06-14 05:17 489 查看
这也是一道智力题,没那么复杂,也没那么直白。

public class Solution {
public boolean increasingTriplet(int[] nums) {
if (nums == null || nums.length < 3) {
return false;
}
int firstMin = Integer.MAX_VALUE, secondMin = Integer.MAX_VALUE;
for (int i: nums) {
if (i <= firstMin) {
firstMin = i;
} else if (i <= secondMin) {
secondMin = i;
} else {
return true;
}
}
return false;

// int n1 = Integer.MIN_VALUE, n2 = Integer.MIN_VALUE, n3 = Integer.MIN_VALUE;
// for (int i: nums) {
// if (i > n1) {
// n3 = n2;
// n2 = n1;
// n1 = i;
// } else if (i > n2) {
// n3 = n2;
// n2 = i;
// } else if (i > n3) {
// n3 = i;
// }
// }
// //if (n1 > n2 && n2 > n3) {
// if (n1 > n2 && n2 > n3 && n3 != Integer.MIN_VALUE) {
// return true;
// } else {
// return false;
// }
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: