,敢教日月换新天。为有牺牲多壮志

[Swift]LeetCode334. 递增的三元子序列 | Increasing Triplet Subsequence

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10261970.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.

Formally the function should:

Return true if there exists i, j, k 
such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false.

Note: Your algorithm should run in O(n) time complexity and O(1) space complexity.

Example 1:

Input: [1,2,3,4,5]
Output: true

Example 2:

Input: [5,4,3,2,1]
Output: false

给定一个未排序的数组,判断这个数组中是否存在长度为 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

16ms
复制代码
 1 class Solution {
 2     func increasingTriplet(_ nums: [Int]) -> Bool {
 3         guard nums.count >= 3 else {
 4             return false
 5         }
 6         
 7         var first = Int.max
 8         var second = Int.max
 9         
10         for num in nums {
11             if num <= first {
12                 first = num
13             } else if num <= second {
14                 second = num
15             } else {
16                 return true
17             }
18         }
19         return false
20     }
21 }
复制代码

20ms

复制代码
 1 class Solution {
 2        func increasingTriplet(_ nums: [Int]) -> Bool {
 3    
 4     guard nums.count >= 3 else {
 5       
 6       return false
 7     }
 8     
 9     var minvalue = nums[0]
10     
11     var middle = Int.max
12     
13     var minvalue1 = nums[0]
14     
15     for i in 1..<(nums.count - 1) {
16       
17       if nums[i] <= minvalue1 {
18         
19         if middle != Int.max {
20           
21           minvalue1 = nums[i]
22         }
23         else {
24           
25           minvalue = nums[i]
26           
27           minvalue1 = minvalue
28           
29           middle = Int.max
30         }
31       }
32       else if nums[i] <= middle {
33         
34         middle = nums[i]
35         
36         minvalue = minvalue1
37       }
38       else {
39         
40         return true
41       }
42     }
43     
44     return minvalue < middle && middle < nums.last!
45   }
46 }
复制代码

48ms

复制代码
 1 class Solution {
 2     func increasingTriplet(_ nums: [Int]) -> Bool {
 3         if nums.count < 3 {
 4             return false
 5         }
 6         var minI = nums[0]
 7         var bigMin : Int? = nil
 8         
 9         for i in 1..<nums.count {
10             if bigMin != nil && nums[i] > bigMin! {
11                 return true
12             }
13             
14             if nums[i] < minI {
15                 minI = nums[i]
16             }
17             if nums[i] > minI {
18                 if bigMin == nil {
19                     bigMin = nums[i]
20                 }else {
21                     bigMin = min(nums[i], bigMin!)
22                 }
23             }
24         }
25         
26         return false
27     }
28 }
复制代码

56ms

复制代码
 1 class Solution {
 2     func increasingTriplet(_ nums: [Int]) -> Bool {
 3         var minInt:Int = Int.max
 4         var maxInt:Int = Int.max
 5         for item in nums {
 6             if minInt >= item {
 7                 minInt = item
 8             }else if maxInt >= item {
 9                 maxInt = item
10             }else{
11                 return true
12             }
13         }
14         return false
15     }
16 }
复制代码

56ms

复制代码
 1 class Solution {
 2     func increasingTriplet(_ nums: [Int]) -> Bool {
 3         guard nums.count > 2 else {
 4             return false
 5         }
 6 
 7         var n1 = Int.max
 8         var n2 = Int.max - 1
 9 
10         for n in nums {
11             if n <= n1 {
12                 n1 = n
13             } else if n < n2 {
14                 n2 = n
15             } else if n1 < n2 && n2 < n {
16                 return true
17             }
18         }
19         
20         return false
21     }
22 }
复制代码

 

posted @   为敢技术  阅读(276)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°