分析
流的解法 AC
不太懂他的原理,参考大佬的解法;作者:https://www.lanqiao.cn/users/1328693/
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 在此输入您的代码...
int num = scan.nextInt();
int[] arr = new int[num];
for (int i = 0; i < arr.length; i++) {
arr[i] = scan.nextInt();
}
int sum = 0;
for (int i = 1; i < arr.length - 1; i++) {
if (arr[i] > Arrays.stream(arr, 0, i).min().getAsInt()
&& arr[i] < Arrays.stream(arr, i, arr.length).max().getAsInt())
sum++;
}
System.out.println(sum);
}
}
三层循环(三指针) c++ AC
#include <bits/stdc++.h>
using namespace std;
int n;
int vis[1010];
int main() {
cin >> n;
int a[n];
for(int i=0; i<n; i++) {
cin>>a[i];
}
int ans=0;
for(int i=0; i<n-2; i++) {
for(int j=i+1; j<n-1; j++) {
for(int k=j+1; k<n; k++) {
if(a[i]<a[j]&&a[j]<a[k])
vis[j]=1;
}
}
}
for(int i=0; i<n; i++) {
if(vis[i]==1)
ans++;
}
cout<<ans;
return 0;
}
三层循环java(三指针)TLE
也相当于组合,只不过为取出的个数比较少的适用;
import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] a = new int[n];
int index;
for (int i = 0; i < n; i++) {
a[i] = scan.nextInt();
}
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < n - 2; i++) {
for (int j = i + 1; j < n - 1; j++) {
index = j;
for (int k = j + 1; k < n; k++) {
if (a[j] > a[i] && a[j] < a[k]) {
set.add(index);
}
}
}
}
System.out.println(set.size());
}
}
三层循环java(三指针)AC
==不使用Java的容器,因为hashset底层为红黑树,容易超时;使用一个数组vis标记满足条件的中心值,遍历他,查找值为1的个数;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] a = new int[n];
int[] vis = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scan.nextInt();
}
for (int i = 0; i < n - 2; i++) {
for (int j = i + 1; j < n - 1; j++) {
for (int k = j + 1; k < n; k++) {
if (a[j] > a[i] && a[j] < a[k]) {
vis[j] = 1;
}
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (vis[i] == 1)
ans++;
}
System.out.println(ans);
}
}
组合型枚举(dfs) TLE超时
主要去参考这个组合思路
写法一
注意问的是元素个数,相同的元素但是在不同的位置也算,例如1 2 2 3;那么 两个2都是符合要求的,即答案 为2,不是1;
所以有一个 数组in去记录下标,在搜索的时候,在a数组选中的元素同时,用in数组去记录下标u;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
static Set<Integer> set = new HashSet<>();//防止中间值元素的重复
static int n;
static int[] a = new int[1100];
static int cnt = 0;
static int[] t = new int[1100];//因为需要搜到最后,然后才 cnt--到3输出
static int[] in = new int[1100];//记录下标
static void dfs(int u) {
if (u == n) {
if (cnt != 3)
return;
//System.out.println(t[0]+" "+t[1]+" "+t[2]);
if (t[1] > t[0] && t[1] < t[2])
set.add(in[1]);
return;
}
t[cnt] = a[u];
in[cnt] = u;
cnt++;
dfs(u + 1);
cnt--;
dfs(u + 1);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
for (int i = 0; i < n; i++) {
a[i] = scan.nextInt();
}
dfs(0);
System.out.println(set.size());
}
}
写法二
此方法参考赵金涛的,作者:https://blog.csdn.net/weixin_52603147?type=blog
import java.util.*;
public class Mian{
static int a[]=new int[1005];
static int n;
static int ans=0;
static HashSet<Integer> set=new HashSet<>();
static boolean v[]=new boolean[1005];
static int b[]=new int[5];
static int bb[]=new int[5];
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
for(int i=1;i<=n;i++){
a[i]=sc.nextInt();
}
dfs(1);
System.out.println(set.size());
}
public static void dfs(int x){
if(x>3){
if(b[2]>b[1]&&b[2]<b[3]){
set.add(bb[2]);
}
return;
}
for(int i=1;i<=n;i++){
if(!v[i]&&i>bb[x-1]){
v[i]=true;
b[x]=a[i];
bb[x]=i;
dfs(x+1);
v[i]=false;
}
}
}
}