665. Non-decreasing Array

Input: [4,2,3]
Output: True
Explanation: You could modify the first 4 to 1 to 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的更多相关文章

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

    665. 非递减数列 665. Non-decreasing Array 题目描述 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是 ...

  2. LeetCode 665. Non-decreasing Array (不递减数组)

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

  3. Java实现 LeetCode 665 非递减数列(暴力)

    665. 非递减数列 给你一个长度为 n 的整数数组,请你判断在 最多 改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 < ...

  4. 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 ...

  5. Leetcode 665.非递减数列

    非递减数列 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i ...

  6. leetcode算法总结

    算法思想 二分查找 贪心思想 双指针 排序 快速选择 堆排序 桶排序 搜索 BFS DFS Backtracking 分治 动态规划 分割整数 矩阵路径 斐波那契数列 最长递增子序列 最长公共子系列 ...

  7. 【LEETCODE】51、数组分类,简单级别,题目:581,830,1010,665

    package y2019.Algorithm.array; /** * @ClassName FindUnsortedSubarray * @Description TODO 581. Shorte ...

  8. 【LeetCode】665. 非递减数列 Non-decreasing Array(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:数组,array,非递减,遍历,python,C++ 目录 题目描述 题目大意 解题方法 一.错误代码 二.举例分析 ...

  9. 665. Non-decreasing Array - LeetCode

    Question 665. Non-decreasing Array Solution 题目大意: 思路:当前判断2的时候可以将当前元素2变为4,也可以将上一个元素4变为2,再判断两变化后是否满足要求 ...

随机推荐

  1. Linux-c对一个十六进制数的某一位取反

    enum SWITCH_FLAG { SWITCH_ALL_FLAG = , SWITCH_WEB_FLAG = , …… } unsigned int switch_by_bit_value = 0 ...

  2. opencv-图像遍历

    #include "stdafx.h" #include<opencv2/opencv.hpp> #include<iostream> #include&l ...

  3. uoj349 即时战略

    题意:这是一道交互题.交互库中有一棵树.一开始只有1节点已知.需要在T次询问内使得n个节点都已知.一次询问explore(x,y),返回从x到y路径上第一个点,并将返回点标记为已知. 数据有区分. 标 ...

  4. 解决ajax请求跨域

    跨域大部分需要通过后台解决,引起跨域的原因: 3个问题同时满足 才可能产生跨域问题,即跨域(协议,主机名,端口号中有一个不同就产生跨域) 下面是解决方法 方法一 // ajax请求跨域 /* *解决a ...

  5. leetcode-95-不同的二叉搜索树(卡特兰数)

    题目描述: 方法一:动态规划 O(n^2) O(n) class Solution: def numTrees(self, n: int) -> int: dp = [0]*(n+1) dp[0 ...

  6. SPSS与Streams的集成实现实时预测

    SPSS与Streams的集成实现实时预测 SPSS Modeler 是一个数据挖掘工作台,提供了一个可了解数据并生成预测模型的最先进的环境.Streams 提供了一个可伸缩的高性能环境,对不断变化的 ...

  7. 组件:基础的基础组件(Component,Portlet)

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...

  8. java.io.FileNotFoundException: E:\work\work (拒绝访问。)

    转载自:https://blog.csdn.net/YQS_Love/article/details/51959776 一.问题 在使用FileInputStream或FileOutputStream ...

  9. Struts2入门问题

    一 使用Struts 2 开发程序的基本步骤 加载Struts2 类库 配置web.xml文件 开发视图层页面 开发控制层Action 配置struts.xml文件 部署.运行项目 第一步先导架包:在 ...

  10. PKU--2184 Cow Exhibition (01背包)

    题目http://poj.org/problem?id=2184 分析:给定N头牛,每头牛都有各自的Si和Fi 从这N头牛选出一定的数目,使得这些牛的 Si和Fi之和TS和TF都有TS>=0 F ...