leetcode 665
665. Non-decreasing Array
Input: [4,2,3]
Output: True
Explanation: You could modify the first4to1to get a non-decreasing array.递增
思路:贪心思想,找异常值,存在两个以上则返回false。如果当前的值比前一的值小,并且也比前两的值小,此时只需更改当前值,而不更改前两个值。
更改规则是用前一的值代替当前值。
两种情况
例如: 2 2 1 -》 2 2 2
0 2 1 -》 0 1 1
bool checkPossibility(vector<int>& nums) {
int cnt = ; //如果存在两个以上的异常值则直接返回false
for(int i = ; i < nums.size() && cnt<= ; i++){
if(nums[i-] > nums[i]){ //存在异常值
cnt++;
if(i-< || nums[i-] <= nums[i])nums[i-] = nums[i]; //i-2处理第一个边界值,这种||技巧经常用到
else nums[i] = nums[i-]; //have to modify nums[i]
}
}
return cnt<=;
}
669. Trim a Binary Search Tree
Input:
3
/ \
0 4
\
2
/
1 L = 1
R = 3 Output:
3
/
2
/
1
The code works as recursion. If the root value in the range [L, R]
we need return the root, but trim its left and right subtree;
else if the root value < L
because of binary search tree property, the root and the left subtree are not in range;
we need return trimmed right subtree.
else
similarly we need return trimmed left subtree. Without freeing memory class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R) {
if (root == NULL) return NULL;
if (root->val < L) return trimBST(root->right, L, R);
if (root->val > R) return trimBST(root->left, L, R);
root->left = trimBST(root->left, L, R);
root->right = trimBST(root->right, L, R);
return root;
}
};

总结:树的遍历bfs,一般结构:
当前root
////////////////////////////
中间逻辑,比如深一层的遍历
///////////////////////////
对当前root进行操作,例如左右边子树的赋值操作
leetcode 665的更多相关文章
- LeetCode 665. 非递减数列(Non-decreasing Array)
665. 非递减数列 665. Non-decreasing Array 题目描述 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是 ...
- LeetCode 665. Non-decreasing Array (不递减数组)
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- Java实现 LeetCode 665 非递减数列(暴力)
665. 非递减数列 给你一个长度为 n 的整数数组,请你判断在 最多 改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 < ...
- Leetcode 665. Non-decreasing Array(Easy)
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- Leetcode 665.非递减数列
非递减数列 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i ...
- leetcode算法总结
算法思想 二分查找 贪心思想 双指针 排序 快速选择 堆排序 桶排序 搜索 BFS DFS Backtracking 分治 动态规划 分割整数 矩阵路径 斐波那契数列 最长递增子序列 最长公共子系列 ...
- 【LEETCODE】51、数组分类,简单级别,题目:581,830,1010,665
package y2019.Algorithm.array; /** * @ClassName FindUnsortedSubarray * @Description TODO 581. Shorte ...
- 【LeetCode】665. 非递减数列 Non-decreasing Array(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:数组,array,非递减,遍历,python,C++ 目录 题目描述 题目大意 解题方法 一.错误代码 二.举例分析 ...
- 665. Non-decreasing Array - LeetCode
Question 665. Non-decreasing Array Solution 题目大意: 思路:当前判断2的时候可以将当前元素2变为4,也可以将上一个元素4变为2,再判断两变化后是否满足要求 ...
随机推荐
- printk函数
一个不同是 printk 允许你根据消息的严重程度对其分类, 通过附加不同的记录级别或者 优先级在消息上. 你常常用一个宏定义来指示记录级别. 例如, KERN_INFO, 我们之前曾 在一些打印语句 ...
- 【左偏树】[APIO2012]派遣
题意可真的是有毒 第一眼树形背包可做?(反正我没用树形背包打过,边上巨佬打的背包似乎没拿分) 后来发现可以贪心搞,我们先把一个节点所有的儿子都取进去,之后不行的话再从大的开始拿走就好了 问题就变成了了 ...
- 【珍惜时间】iReport
项目很点意思,感觉很高超的样子 先放下项目的github地址:https://github.com/tctangyanan/iReport 感谢各位伟大的程序员无私的分享自己的技术 老规矩,我们会运行 ...
- jvisualvm图解【转】
jvisualvm图解[转] http://blog.csdn.net/a19881029/article/details/8432368 jvisualvm能干什么:监控内存泄露,跟踪垃圾回收, ...
- LinkedHashMap笔记
一.最基本元素存储单元 /** * HashMap.Node subclass for normal LinkedHashMap entries. */ static class Entry<K ...
- LintCode_1 单例模式
从今天开始我的LintCode之旅,由于C/C++好久没有使用了,语法生疏不说,低级错误频繁出现,因此在做题之后,还会有部分时间复习语法项目. ---------------------------- ...
- Data too long for column
一篇文章的正文,需要很多字数,默认就是255,不够 @Lob @Basic @Type(type = "text") @Column(name = "detail&quo ...
- Delphi遍历进程-Win32API
本博客的Delphi代码使用的版本均为DelphiXE10.x 1.1 .枚举进程 通过进程名称获取指定的进程ID,代码很详细,不再赘述 unit Uuitls; interface uses TlH ...
- cdh_hadoop下载地址
http://archive.cloudera.com/cdh5/cdh/5/
- spring boot 项目添加maven依赖时provided
spring boot 项目是自带tomcat 的,但是我们有时候是需要把项目打包成war 然后放到独立的tomcat中运行的,这个时候我们就需要将它自带的tomcat给排除开,这时候我们就可以使用& ...