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,再判断两变化后是否满足要求 ...
随机推荐
- Activiti添加批注(comment)信息
在每次提交任务的时候需要描述一些批注信息,例如:请假流程提交的时候要描述信息为什么请假,如果领导驳回可以批注驳回原因等 1.添加批注 // 由于流程用户上下文对象是线程独立的,所以要在需要的位置设置, ...
- Luogu P1041 传染病控制(搜索)
P1041 传染病控制 题意 题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这 ...
- DOS命令大全【转】
见到网络上,觉得值得学习,特此收藏到这里,因为我几乎天天来这个网站 net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net u ...
- html选择器[获取兄弟(同级元素)]
1.html <div class="col-md-5"> <h4 class="m-t-md" id="dwzh"> ...
- python的meshgrid用法和3D库 mpl_toolkits.mplot3d 与PolynomialFeatures多项式库学习
meshgrid import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Ax ...
- linux系统添加定时任务
http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/crontab.html
- 新书《iOS应用逆向工程:分析与实战》
前无古人!小白福音!国内第一本iOS应用逆向工程类图书<iOS应用逆向工程:分析与实战>就要空降啦~! 你是否曾因应用上线的第一天即遭破解而无奈苦恼,想要加以防范,却又束手无策? 你是否曾 ...
- 不同浏览器Cookie有效期问题
昨天项目迁移了测试服务器,之后奇怪的问题出现了. IE.谷歌无法登陆,火狐可以登陆. 这个项目先后部署过两个测试服务器.一台正式服务器,登陆都是正常的,这次却突然出现这种奇怪的问题,很是纠结. 通过查 ...
- 元素显示v-show
<!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...
- FormData兼容IE10 360及DWR的异步上传原理
摘自:https://github.com/henryluki/FormData/blob/master/formdata.js if(!window.FormData) { (function(se ...