• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode674.最长连续递增序列|LongestContinuousIncreasingSubsequence ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

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

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

Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).

Example 1:

Input: [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. 
Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4.  

Example 2:

Input: [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2], its length is 1.  

Note: Length of the array will not exceed 10,000.


给定一个未经排序的整数数组,找到最长且连续的的递增序列。

示例 1:

输入: [1,3,5,4,7]
输出: 3
解释: 最长连续递增序列是 [1,3,5], 长度为3。
尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为5和7在原数组里被4隔开。 

示例 2:

输入: [2,2,2,2,2]
输出: 1
解释: 最长连续递增序列是 [2], 长度为1。

注意:数组长度不会超过10000。


Runtime: 68 ms
Memory Usage: 19.1 MB
 1 class Solution {
 2     func findLengthOfLCIS(_ nums: [Int]) -> Int {
 3         if nums.count == 0 {
 4             return 0
 5         }
 6         if nums.count == 1 {
 7             return 1
 8         }
 9         var maxLength = 1
10         var count = 1
11         for i in 1..<nums.count {
12             if nums[i] > nums[i - 1] {
13                 count = count + 1
14             } else {
15                 count = 1
16             }
17             maxLength = max(maxLength, count)
18         }
19         return maxLength
20     }
21 }

68ms

 1 class Solution {
 2     func findLengthOfLCIS(_ nums: [Int]) -> Int {
 3         var left = 0
 4         var right = 0
 5         var index = 0
 6         var result = 0
 7         
 8         while right < nums.count {
 9             while index < right {
10                 if nums[index] >= nums[right] {
11                     left = index + 1
12                 }
13                 index += 1
14             }
15             result = max(result, right-left+1)
16             right += 1
17         }
18         return result
19     }
20 }

72ms

 1 class Solution {
 2     func findLengthOfLCIS(_ nums: [Int]) -> Int {
 3         if nums.isEmpty {
 4             return 0
 5         }
 6         
 7         if nums.count == 1 {
 8             return 1
 9         }
10         var count = 1
11         var maxCount = count
12         for i in 1...nums.count - 1 {
13             if nums[i] > nums[i - 1] {
14                 count += 1
15             }
16             else {
17                 if count > maxCount {
18                     maxCount = count
19                 }
20                 count = 1
21             }
22         }
23         print(maxCount)
24         return (maxCount > count ? maxCount : count)
25     }
26 }

76ms

 1 class Solution {
 2     func findLengthOfLCIS(_ nums: [Int]) -> Int {
 3         var longest = 0
 4         var currentLength = 0
 5         for (i, num) in nums.enumerated() {
 6             if i > 0 && num > nums[i - 1] {
 7                 currentLength += 1
 8             } else {
 9                 currentLength = 1
10             }
11             longest = max(longest, currentLength)
12         }
13         return longest
14     }
15 }

84ms

 1 class Solution {
 2     func findLengthOfLCIS(_ nums: [Int]) -> Int 
 3     {
 4         var temp_increasing_subsequence_length = 0
 5         var max_increasing_subsequence_length = 0
 6         
 7         guard nums.count >= 2 
 8         else
 9         {
10             return nums.count
11         }
12         
13         for i in 0...(nums.count - 2)
14         {
15             if (nums[i] < nums[i + 1])
16             {
17                 temp_increasing_subsequence_length += 1
18             }
19             else
20             {
21                 temp_increasing_subsequence_length = 0
22             }
23             
24             max_increasing_subsequence_length = max(max_increasing_subsequence_length, temp_increasing_subsequence_length + 1)
25         }
26         
27         return max_increasing_subsequence_length
28     }
29 }

92ms

 1 class Solution {
 2     func findLengthOfLCIS(_ nums: [Int]) -> Int {
 3         var dp = [Int](repeating: 0, count: nums.count) 
 4         var res = 0
 5         for (i , n) in nums.enumerated() {
 6             if i == 0 {
 7                 dp[i] = 1
 8             }else if nums[i] > nums[i - 1]{
 9                 dp[i] = dp[i - 1] + 1
10                 
11             }else{
12                 dp[i] = 1
13             }
14             res = max(res, dp[i])
15         }
16         return res
17     }
18 }

104ms

 1 class Solution {
 2     func findLengthOfLCIS(_ nums: [Int]) -> Int {
 3   if(nums.isEmpty){
 4             return 0
 5         }
 6         if(nums.count == 1){
 7             return 1
 8         }
 9         
10         var end:Int = 0
11         var temp = 1
12         var resultNum = 1
13         
14         for i in 1..<nums.count{
15             if(nums[i] > nums[end]){
16                 end = i
17                 temp += 1
18             }else{
19                 resultNum = max(temp, resultNum);
20                 temp = 1
21                 end = i
22             }
23             
24         }
25         
26         return max(temp, resultNum);
27     }
28 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap