LeetCode#665 非递减数列

题目:

给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列。

我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i < n),满足 array[i] <= array[i + 1]。

思路:

4,2,3
-1,4,2,3
2,3,3,2,4

分析上面的这三个例子可得:

当出现当前的数字比前一个数字小的时候:

如果前边第二个数字比当前的数字小或者等于就修改前边第一个数字(第二个例子)

如果前边第二个数字比当前的数字大就修改当前的数字(第三个例子)

如果当前的数字是整个数组的第二个数字,就直接修改第一个数字就好了

import java.util.*;

class Solution {
    public boolean checkPossibility(int[] nums) {
        //4 2 3
        int cnt = 0;
        for(int i=1; i<nums.length && cnt<2; i++) {
            if(nums[i]<nums[i-1]) {
                cnt++;
                if(i-1==0) {
                    nums[i-1] = nums[i];
                }else {
                    if(nums[i-2]<=nums[i]) {
                        nums[i-1] = nums[i];
                    }else {
                        nums[i] = nums[i-1];
                    }
                }
            }
        }
        return cnt < 2;
    }
}

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
        int x = scan.nextInt();
        int[] it = new int[x];
        for(int i=0; i<x; i++) {
            it[i] = scan.nextInt();
        }
        Solution solution = new Solution();
        if(solution.checkPossibility(it)) {
            System.out.println("True");
        }else {
            System.out.println("False");
        }
    }

}

原文地址:https://www.cnblogs.com/sykline/p/12207255.html

时间: 2024-08-30 09:51:33

LeetCode#665 非递减数列的相关文章

leetcode 665. 非递减数列(Non-decreasing Array)

目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i < n),满足 array[i] <= array[i + 1]. 示例 1: 输入: [4,2,3] 输出: True 解释: 你可以通过把第一个4变成1来使得它成为一个非递减数列. 示例 2: 输入: [4,2,1] 输出: False 解释:

顺序表的非递减数列合并

#include<stdio.h> /*包含输入输出头文件*/ #define ListSize 100 typedef int DataType; typedef struct { DataType list[ListSize]; int length; }SeqList; void InitList(SeqList *L) /*将线性表初始化为空的线性表只需要把线性表的长度length置为0*/ { L->length=0; /*把线性表的长度置为0*/ } int ListEmpt

链式表的非递减数列合并

/*包含头文件*/ #include<stdio.h> #include<malloc.h> #include<stdlib.h> /*宏定义和单链表类型定义*/ #define ListSize 100 typedef int DataType; typedef struct Node { DataType data; struct Node *next; }ListNode,*LinkList; void MergeList(LinkList A,LinkList

Gym 100952H Special Palindrome 非递减的回文串、dfs打表、查数列网站OEIS

H - H Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice Gym 100952H Description standard input/output Statements A sequence of positive and non-zero integers called palindromic if it can be read the s

【编程题目】一个数组是由一个递减数列左移若干位形成的,在这种数组中查找某一个数。☆

48.微软(运算):一个数组是由一个递减数列左移若干位形成的,比如{4,3,2,1,6,5}是由{6,5,4,3,2,1}左移两位形成的,在这种数组中查找某一个数. 我的思路: 非常麻烦:先是用二分法找最大的数的位置,再定位要找的数在哪个递减区间里,最后用普通的二分查找法找到.代码如下: /* 48.微软(运算): 一个数组是由一个递减数列左移若干位形成的,比如{4,3,2,1,6,5} 是由{6,5,4,3,2,1}左移两位形成的,在这种数组中查找某一个数. */ #include <stdi

Codeforces Round #323 (Div. 2) D. Once Again... 暴力+最长非递减子序列

                                                                              D. Once Again... You are given an array of positive integers a1, a2, ..., an × T of length n × T. We know that for any i > n it is true that ai = ai - n. Find the length of

非递减顺序表的合并

//顺序表的合并 //输入元素函数 put //输出元素函数 output //合并 Merge #include<stdio.h> #include<stdlib.h> #include<algorithm> using namespace std; #define LIST_INIT_SIZE 80 #define LISTINCREMENT 10 typedef struct { int *elem; int length; //有效长度 int size; //

LIS严格递增和非递减模板

2017-09-10 16:51:03 writer:pprp 严格递增的LIS模板 #include<stdio.h> #include<string.h> #include<algorithm> #include <vector> #include <iostream> using namespace std; int a[10] = {0,1,5,3,6,9}; vector<int> v; int main() { v.cle

17、把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。

把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1. NOTE:给出的所有元素都大于0,若数组大小为0,请返回0. eg: 输入 3 4 5 1 2 输出 1 思路:用二分法查找最小元素 三种情况: (1)rotateArray[mid] >rotateArray[high]: like:[x,x,x,6,x,x,2],此时最小数字一