博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift]LeetCode128. 最长连续序列 | Longest Consecutive Sequence
阅读量:5057 次
发布时间:2019-06-12

本文共 4881 字,大约阅读时间需要 16 分钟。

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

Your algorithm should run in O(n) complexity.

Example:

Input: [100, 4, 200, 1, 3, 2]Output: 4Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.

给定一个未排序的整数数组,找出最长连续序列的长度。

要求算法的时间复杂度为 O(n)

示例:

输入: [100, 4, 200, 1, 3, 2]输出: 4解释: 最长连续序列是 [1, 2, 3, 4]。它的长度为 4。

16ms
1 class Solution { 2     func longestConsecutive(_ nums: [Int]) -> Int { 3         guard nums.count > 0 else { return 0 } 4         var nums = nums.sorted(){$0 < $1} 5          6         var longestStreak = 1 7         var currentStreak = 1 8         for i in 1..

20ms

1 class Solution { 2     func longestConsecutive(_ nums: [Int]) -> Int { 3         let set = Set(nums) 4          5         var longestStreak = 0 6          7         for s in set { 8             if !set.contains(s - 1) { 9                 var currentNumber = s10                 var currentStreak = 111                 12                 while set.contains(currentNumber + 1) {13                     currentNumber += 114                     currentStreak += 115                 }16                 17                 longestStreak = max(longestStreak, currentStreak)18             }19         }20         21         return longestStreak22     }23 }

24ms

1 class Solution { 2     func longestConsecutive(_ nums: [Int]) -> Int { 3         var numSet = Set(nums) 4         var longest = 0 5         for num in nums { 6             if numSet.contains(num) { 7                 var left = num - 1 8                 var right = num + 1 9                 numSet.remove(num)10                 while numSet.contains(left) {11                     numSet.remove(left)12                     left -= 113                 }14                 while numSet.contains(right) {15                     numSet.remove(right)16                     right += 117                 }18                 longest = max(longest, right - left - 1)19             }20         }21         return longest22     }23 }

36ms

1 class Solution { 2     func longestConsecutive(_ nums: [Int]) -> Int { 3         var set = Set(nums), longest = 0 4          5         for num in nums { 6             if set.contains(num) { 7                 set.remove(num) 8                 let distance = 1 + findConsecutive(&set, num, 1) + findConsecutive(&set, num, -1) 9                 longest = max(longest, distance)10             }11         }12         13         return longest14     }15     16     fileprivate func findConsecutive(_ set: inout Set
, _ num: Int, _ step: Int) -> Int {17 var len = 0, num = num + step18 19 while set.contains(num) {20 set.remove(num)21 len += 122 num += step23 }24 25 return len26 }27 }

 48ms

1 class Solution { 2     func longestConsecutive(_ nums: [Int]) -> Int { 3         guard nums.count > 1 else { 4             return nums.count 5         } 6  7         let sorted = nums.sorted() 8         var last = 0 9         var maxLength = 110         var duplicated = 011 12         for idx in 1 ..< sorted.count {13             if sorted[idx] == sorted[idx - 1] {14                 duplicated += 115             } else if sorted[idx] == sorted[idx - 1] + 1 {16                 let length = idx - duplicated - last + 117                 maxLength = max(maxLength, length)18             } else {19                 duplicated = 020                 last = idx21             }22         }23 24         return maxLength25     }26 }

56ms

1 public struct HashSet
{ 2 private var dictionary = Dictionary
() 3 4 public mutating func insert(element: T) { 5 dictionary[element] = true 6 } 7 public mutating func remove(element: T) { 8 dictionary[element] = nil 9 }10 public func contains(element: T) -> Bool {11 return dictionary[element] != nil12 }13 public func allElements() -> [T] {14 return Array(dictionary.keys)15 }16 public var count: Int {17 return dictionary.count18 }19 public var isEmpty: Bool {20 return dictionary.isEmpty21 }22 }23 24 /*25 * Copyright (c) 2018 Brad McEvilly26 */27 class Solution {28 func longestConsecutive(_ nums: [Int]) -> Int {29 30 var maxStreak:Int=031 var numSet = HashSet
()32 33 // validate input34 if (0 == nums.count || nums==nil){35 //print("validation for nums - nums:\(nums)")36 return maxStreak37 }38 39 for i in (0..

 

转载于:https://www.cnblogs.com/strengthen/p/9958427.html

你可能感兴趣的文章
Windows7中双击py文件运行程序
查看>>
Market entry case
查看>>
bzoj1230 开关灯 线段树
查看>>
LinearLayout
查看>>
学习python:day1
查看>>
css3动画属性
查看>>
第九次团队作业-测试报告与用户使用手册
查看>>
Equal Sides Of An Array
查看>>
CentOS笔记-用户和用户组管理
查看>>
Mongodb 基本命令
查看>>
Qt中QTableView中加入Check列实现
查看>>
“富豪相亲大会”究竟迷失了什么?
查看>>
控制文件的备份与恢复
查看>>
返回代码hdu 2054 A==B?
查看>>
Flink独立集群1
查看>>
iOS 8 地图
查看>>
20165235 第八周课下补做
查看>>
[leetcode] 1. Two Sum
查看>>
iOS 日常工作之常用宏定义大全
查看>>
PHP的SQL注入技术实现以及预防措施
查看>>