下面是我的代码。.如果下面也有问题,请更正我。
public int solution(int[] A) {
int count = 0;
List<Integer> arr = new ArrayList<Integer>();
ArrayList<Integer> arrt = new ArrayList<Integer>();
for (int i : A) {
arr.add(i);
}
// first check if its already in correct sequence
boolean check = false;
for (int j = 0; j < A.length-2 ; j++) {
if ((arr.get(j) - arr.get(j + 1) > 0) && (arr.get(j + 1) - arr.get(j + 2) < 0)) {
check = true;
} else if ((arr.get(j) - arr.get(j + 1) < 0) && (arr.get(j + 1) - arr.get(j + 2) > 0)) {
check = true;
} else {
check = false;
break;
}
}
if (check) {
return 0;
}
List<Integer> ab = new ArrayList<Integer>();
for (int i = 0; i < A.length; i++) {
ab.clear();
ab.addAll(arr);
ab.remove(i);
int f = 0;
boolean okay = false;
while (f < A.length - 3) {
if (!okay && f != 0) {
break;
}
if ((ab.get(f) - ab.get(f + 1) > 0) && (ab.get(f + 1) - ab.get(f + 2) < 0)) {
okay = true;
} else if ((ab.get(f) - ab.get(f + 1) < 0) && (ab.get(f + 1) - ab.get(f + 2) > 0)) {
okay = true;
} else {
okay = false;
}
f++;
}
if (okay) {
count++;
}
}
if (count == 0)
count = -1;
return count;
}
,
这是java中的简短解决方案。
class Solution {
public int solution(int[] A) {
if (A.length < 3) {
return A[0] != A[1] ? 0 : 1;
}
int count = 0;
for (int i = 0; i < A.length - 2 ; i += 2) {
int a = A[i];
int b = A[i+1];
int c = A[i + 2];
if (!(a - b > 0 && b - c < 0) && !(a - b < 0 && b - c > 0)) {
count ++;
}
}
return count;
}
}
,
这是我的解决方案。
class Solution {
public int solution(int[] A) {
// check if the input array in the correct order
int count = 0;
for(int i = 0; i < A.length - 2; i++) {
if(A[i] < A[i+1]) {
if(!(A[i+1] > A[i+2])) {
count++;
}
}
else if(A[i] > A[i+1]) {
if(!(A[i+1] < A[i+2])) {
count++;
}
}
}
if(count == 0)
return count;
// if false then copy to Arraylist and do the removal
List<Integer> inputArr = new ArrayList<Integer>();
for(int item : A) {
inputArr.add(item);
}
return doTheRemoval(inputArr);
}
private int doTheRemoval(List<Integer> inputArr) {
int count = -1;
for(int i = 0; i < inputArr.size(); i++) {
// remove tree sequencely
inputArr.remove(i);
// check the rest
for(int j = 0; j < inputArr.size() - 2; j++) {
int a = inputArr.get(j);
int b = inputArr.get(j + 1);
int c = inputArr.get(j + 2);
if((a - b < 0 && b - c > 0) || (a - b > 0 && b - c < 0)) {
if(count == -1) {
count = 0;
}
count++;
}
}
}
return count;
}
}
,
这是我在 c 中的解决方案:
#include <stdio.h>
char aestheticallyPleasantCheck (int A[],int N) {
int x;
char pleasant = 0;
for (x = 0; x < N - 2; x++){
if ((A[x] < A[x + 1] && A[x + 1] > A[x + 2]) || (A[x] > A[x + 1] && A[x + 1] < A[x + 2])){
pleasant = 1;
} else {
break;
}
}
if (pleasant == 1 && x == N - 2) {
return 0;
} else {
return 1;
}
}
int solution (int A[],int N) {
int count = 0;
int ArrayCopy[N];
if (aestheticallyPleasantCheck (A,N) == 0)
{
return 0;
}
for (int i = 0; i < N; i++){
for (int m = 0; m < N; m++){
ArrayCopy[m] = A[m];
}
for (int m = i; m < N - 1; m++){
ArrayCopy[m] = ArrayCopy[m + 1];
}
if (aestheticallyPleasantCheck (ArrayCopy,N - 1) == 0){
count++;
}
}
if (count == 0){
count = -1;
}
return count;
}
/*
Example test: [3,4,5,3,7]
Example test: [1,2,4]
Example test: [1,1,2]
*/
main ()
{
int A[] = { 3,7 }; // Expected 3 .^.^
int B[] = { 1,4 }; //
int C[] = { 1,2 }; //
int D[] = { 5,7,8,9 };
printf ("Got %d : Expected 3\n",solution (A,sizeof (A) / sizeof (int)));
printf("Got %d : Expected -1\n",solution( B,sizeof(B)/sizeof(int)));
printf("Got %d : Expected 0\n",solution( C,sizeof(C)/sizeof(int)) );
printf ("Got %d : Expected -1\n",solution (D,sizeof (D) / sizeof (int)));
return 0;
}
本文链接:https://www.f2er.com/2128589.html