leetcode 665. Non-decreasing Array

来源:互联网 发布:java temp dir 编辑:程序博客网 时间:2024/05/24 06:05

原题:

Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.

We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n).

Example 1:

Input: [4,2,3]Output: TrueExplanation: You could modify the first 4 to 1 to get a non-decreasing array.

Example 2:

Input: [4,2,1]Output: FalseExplanation: You can't get a non-decreasing array by modify at most one element.

Note: The n belongs to [1, 10,000].

又做了一道看似简单但是满是大坑的题目

代码如下:

bool checkPossibility(int* nums, int numsSize) {    if(numsSize<2)        return true;    int flag=0;    int n;    for(n=1;n<numsSize;n++)    {        if(*(nums+n)<*(nums+n-1))        {            flag=1;            break;        }    }    if(flag==1)    {        int temp=0;        int p=*(nums+n);        printf("%d",p);        *(nums+n)=*(nums+n-1);        for(int m=n;m<numsSize;m++)        {            if(*(nums+m)<*(nums+m-1))            {                temp=1;                break;            }        }        if(temp==0)            return true;        *(nums+n-1)=p;        *(nums+n)=p;        temp=0;        for(int m=1;m<numsSize;m++)        {            if(*(nums+m)<*(nums+m-1))            {                temp=1;                break;            }        }        if(temp==0)            return true;        return false;    }    return true;}

我反正直接给Y的把原值给改了,然后在遍历一遍。,。 也是简单粗暴。

原创粉丝点击