leetcode-递增的三元子序列
给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列。
数学表达式如下:
如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1,
使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false 。
说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1) 。
示例 1:
输入: [1,2,3,4,5]
输出: true
示例 2:
输入: [5,4,3,2,1]
输出: false 思路:
class Solution {
public boolean increasingTriplet(int[] nums) {
if(nums.length<3)return false;
int smallest=nums[0];
boolean uper[]=new boolean[nums.length]; //利用数组记录长度为2的递增序列
for(int i=1;i<nums.length;i++){
if(nums[i]>smallest){
uper[i]=true;
}
smallest=Math.min(smallest,nums[i]);
}
int biggest=nums[nums.length-1];
//再次遍历数组,此次从最后开始,不断更新最大值,只要满足长度为2的递增序列,同时当前遍历数字小于最大值,则返回true
for(int i=nums.length-2;i>=0;i--){ //遍历是从倒数第二个数字开始的
if(nums[i]<biggest){
if(uper[i]==true)return true;
}
biggest=Math.max(nums[i],biggest);
}
return false;
}
}
空间复杂度O(1)的解法:
思路:
class Solution {
public boolean increasingTriplet(int[] nums) {
if(nums.length<3)return false;
int m1=Integer.MAX_VALUE,m2=Integer.MAX_VALUE;
for(int i=0;i<nums.length;i++){
//这里使用的是if (){}else if() { } else 语句,判断的逻辑是一开始是更新m1,m2,遍历两个数字时m1<m2(之后更新m1,m2的过程中不保证m1<m2),返回比m1,m2更大的数
if(m1>=nums[i])m1=nums[i];
else if(m2>=nums[i])m2=nums[i];
else return true;
}
return false;
}
}
在更新过程中也可使用此种逻辑表示:
if(nums[i]<=m1){
m1 =nums[i];
continue;
}
if(m1<nums[i]&&nums[i]<=m2){
m2 =nums[i];
continue;
}
if(nums[i]>m2){
return true;
总结:这道题的难点在于if else if语句的逻辑掌握和难以想到用两个数字代替一个递增序列的情况。
leetcode-递增的三元子序列的更多相关文章
- LeetCode:递增的三元子序列【334】
LeetCode:递增的三元子序列[334] 题目描述 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i ...
- Leetcode 334.递增的三元子序列
递增的三元子序列 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n- ...
- 【LeetCode】334#递增的三元子序列
题目描述 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1, 使得 ...
- Java实现 LeetCode 334 递增的三元子序列
334. 递增的三元子序列 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [Leetcode] 第334题 递增的三元子序列
一.题目描述 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1, ...
- [Swift]LeetCode334. 递增的三元子序列 | Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- 334 Increasing Triplet Subsequence 递增的三元子序列
给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列.正式的数学表达如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1, ...
- leetcode334 递增的三元子序列
class Solution { public: bool increasingTriplet(vector<int>& nums) { //使用双指针: int len=nums ...
- [LeetCode] 334. Increasing Triplet Subsequence 递增三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
随机推荐
- PHP 传值操作和传地址操作
PHP中有两种赋值操作 一种是传值操作 example: $a = 1; $b = $b; 这个就是传值 传地址是: example2: $a = 1; $b = &$a; 这两者有什么区别呢 ...
- tp5 migrate数据库迁移工具
tp5相对与tp3.2有很大的不同 migrate是其中一点,通过migrate程序员可以在php代码中创建数据库修改回滚等操作 首先下载migrate扩展,命令行到当前项目目录下执行 compose ...
- windows10下安装source insight 4.0(破解版)
1.从官网下载source insight4.0版本(不用下载,在后面已经把所有需要的文件都准备好了); 2.安装source insightt4.0; 3.使用下载好的sourceinsight4. ...
- JAVA随机数之多种方法从给定范围内随机N个不重复数
一.JAVA中生成随机数的方式 1.在j2se中使用Math.random()令系统随机选取一个0~1之间的double类型小数,将其乘以一个数,比如25,就能得到一个0~25范围内的随机数,这个在j ...
- django复习-3-请求与响应
一.请求request 前端向后端传递参数有几种方式? 提取URL的特定部分,如/weather/beijing/2018,可以在服务器端的路由中用正则表达式截取: "http://127. ...
- C#实现的协同过滤算法
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace SlopeOn ...
- MetaMask/json-rpc-engine
https://github.com/MetaMask/json-rpc-engine RpcEngine——MetaMask/json-rpc-engine https://github.com/M ...
- 开源仓库Harbor搭建及配置过程
1.Harbor介绍 Harbor是Vmvare中国团队开发的开源registry仓库,相比docker官方拥有更丰富的权限权利和完善的架构设计,适用大规模docker集群部署提供仓库服务. 2.安装 ...
- Python2.7-filecmp
filecmp 模块,定义了比较文件或目录的函数,比较文件只会有 True 和 False 两种结果,比较目录会返回目录下相同的文件,不同的文件,出错的文件.比较文件也可以用 difflib 模块,d ...
- (转)JVM调优常用命令(jstat、jmap、jstack)
原文:https://www.cnblogs.com/ityouknow/p/5714703.html 一.jstat jstat(JVM statistics Monitoring)是用于监视虚拟机 ...