[Swift]LeetCode674. 最長連續遞增序列 | Longest Continuous Increasing Subsequence

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-zomrxnpm-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

Example 1:github

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.微信


給定一個未經排序的整數數組,找到最長且連續的的遞增序列。spa

示例 1:code

輸入: [1,3,5,4,7]
輸出: 3
解釋: 最長連續遞增序列是 [1,3,5], 長度爲3。
儘管 [1,3,5,7] 也是升序的子序列, 但它不是連續的,由於5和7在原數組裏被4隔開。 

示例 2:htm

輸入: [2,2,2,2,2]
輸出: 1
解釋: 最長連續遞增序列是 [2], 長度爲1。

注意:數組長度不會超過10000。blog


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 }
相關文章
相關標籤/搜索