一开始看题直接暴力 3个for
不出所料超时了
public boolean find132pattern(int[] nums) {
int one = 0, two = 0, three = 0;
for(int i = 0; i < nums.length; i ++){
one = nums[i];
for (int j = i+1; j < nums.length; j++) {
if (nums[j] > one) {
two = nums[j];
for (int k = j+1; k < nums.length; k++) {
if (nums[k] < two && nums[k] > one) {
return true;
}
}
}
}
}
return false;
}
然后使用一个数组存放第一数的最小值下标
过了
public boolean find132pattern(int[] nums) {
int length = nums.length;
int[] min = new int[length];
min[0] = nums[0];
for (int i = 1; i < length; ++i){
min[i] = Math.min(min[i - 1], nums[i]);
}
int middle = 0;
for (int i = 1; i <length; i++) {
if (nums[i] > min[i]){
middle = nums[i];
for (int j = length - 1; j > i; --j){
if (nums[j] < middle && nums[j] > min[i]){
return true;
}
}
}
}
return false;
}