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的把原值给改了,然后在遍历一遍。,。 也是简单粗暴。
阅读全文
0 0
- LeetCode 665. Non-decreasing Array
- leetcode 665. Non-decreasing Array
- leetcode 665. Non-decreasing Array
- leetcode 665. Non-decreasing Array
- leetcode 665. Non-decreasing Array
- LeetCode Non-decreasing Array
- leetcode-Non-decreasing Array
- leetCode-Non-decreasing Array
- LeetCode——665. Non-decreasing Array
- [LeetCode]665.Non-decreasing Array ❤
- 665. Non-decreasing Array
- 665. Non-decreasing Array
- 665. Non-decreasing Array
- 665. Non-decreasing Array
- 665. Non-decreasing Array
- 665. Non-decreasing Array
- 665. Non-decreasing Array
- 665. Non-decreasing Array
- HMM经典介绍论文【Rabiner 1989】翻译(十六)——放大
- Multidex(一)之源码解析
- 第三方分享及授权登录
- 双源融混
- Nginx简单使用
- leetcode 665. Non-decreasing Array
- SpringBoot非官方教程 | 第二十一篇: springboot集成JMS
- 部署Maven项目到tomcat报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderLi
- 如果没有UX经验,如何创建个人UX作品集?
- 最强Android Studio使用小技巧和快捷键
- 接口、抽象类
- lwj_C#_建立一个数学MathTool类包含的方法
- sed 与 for 循环
- Java中使用Jedis操作Redis