|
44 | 44 |
|
45 | 45 | <!-- 这里可写通用的实现逻辑 -->
|
46 | 46 |
|
| 47 | +**方法 1:排序** |
| 48 | + |
| 49 | +设 res 表示连续序列的最大长度,t 表示当前合法连续序列的长度,初始时 `res = t = 1`。 |
| 50 | + |
| 51 | +先排序数组,然后从下标 1 开始遍历数组,判断 `nums[i]` 与前一个数 `nums[i - 1]` 的大小关系: |
| 52 | + |
| 53 | +- 若 `nums[i] == nums[i - 1]`,直接跳过; |
| 54 | +- 若 `nums[i] - nums[i - 1] == 1`,说明是连续序列,t 自增,利用 `res = max(res, t)` 更新最大长度; |
| 55 | +- 否则 t 重置为 1,继续往下遍历。 |
| 56 | + |
| 57 | +此方法时间复杂度 `O(nlogn)`,空间复杂度 `O(1)`。 |
| 58 | + |
| 59 | +**方法 2:哈希表** |
| 60 | + |
| 61 | +设 res 表示连续序列的最大长度,初始为 0。哈希表 s 存放数组出现的每个元素。 |
| 62 | + |
| 63 | +遍历数组,以当前遍历到的元素 `nums[i]` 做为起点,循环判断 `nums[i] + 1`,`nums[i] + 2` ... 是否存在 s 中,并不断更新连续序列的最大长度。 |
| 64 | + |
| 65 | +在这个过程中,如果 `nums[i]`, `nums[i] + 1`, `nums[i + 2]`, ... 是一个连续序列,遍历下个元素 `nums[i] + 1` 时,其实无需再重复循环。因此,只需要判断 `nums[i] - 1` 是否在 s 中,是则直接跳过。 |
| 66 | + |
| 67 | +此方法时间复杂度 `O(n)`,空间复杂度 `O(n)`。 |
| 68 | + |
47 | 69 | <!-- tabs:start -->
|
48 | 70 |
|
49 | 71 | ### **Python3**
|
50 | 72 |
|
51 | 73 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
52 | 74 |
|
53 | 75 | ```python
|
| 76 | +class Solution: |
| 77 | + def longestConsecutive(self, nums: List[int]) -> int: |
| 78 | + n = len(nums) |
| 79 | + if n < 2: |
| 80 | + return n |
| 81 | + nums.sort() |
| 82 | + res = t = 1 |
| 83 | + for i in range(1, n): |
| 84 | + if nums[i] == nums[i - 1]: |
| 85 | + continue |
| 86 | + if nums[i] - nums[i - 1] == 1: |
| 87 | + t += 1 |
| 88 | + res = max(res, t) |
| 89 | + else: |
| 90 | + t = 1 |
| 91 | + return res |
| 92 | +``` |
54 | 93 |
|
| 94 | +```python |
| 95 | +class Solution: |
| 96 | + def longestConsecutive(self, nums: List[int]) -> int: |
| 97 | + s, res = set(nums), 0 |
| 98 | + for num in nums: |
| 99 | + if num - 1 not in s: |
| 100 | + t = 1 |
| 101 | + next = num + 1 |
| 102 | + while next in s: |
| 103 | + t += 1 |
| 104 | + next += 1 |
| 105 | + res = max(res, t) |
| 106 | + return res |
55 | 107 | ```
|
56 | 108 |
|
57 | 109 | ### **Java**
|
58 | 110 |
|
59 | 111 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
60 | 112 |
|
61 | 113 | ```java
|
| 114 | +class Solution { |
| 115 | + public int longestConsecutive(int[] nums) { |
| 116 | + int n = nums.length; |
| 117 | + if (n < 1) { |
| 118 | + return n; |
| 119 | + } |
| 120 | + Arrays.sort(nums); |
| 121 | + int res = 1, t = 1; |
| 122 | + for (int i = 1; i < n; ++i) { |
| 123 | + if (nums[i] == nums[i - 1]) { |
| 124 | + continue; |
| 125 | + } |
| 126 | + if (nums[i] - nums[i - 1] == 1) { |
| 127 | + t += 1; |
| 128 | + res = Math.max(res, t); |
| 129 | + } else { |
| 130 | + t = 1; |
| 131 | + } |
| 132 | + } |
| 133 | + return res; |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +```java |
| 139 | +class Solution { |
| 140 | + public int longestConsecutive(int[] nums) { |
| 141 | + Set<Integer> s = new HashSet<>(); |
| 142 | + for (int num : nums) { |
| 143 | + s.add(num); |
| 144 | + } |
| 145 | + int res = 0; |
| 146 | + for (int num : nums) { |
| 147 | + if (!s.contains(num - 1)) { |
| 148 | + int t = 1, next = num + 1; |
| 149 | + while (s.contains(next++)) { |
| 150 | + ++t; |
| 151 | + } |
| 152 | + res = Math.max(res, t); |
| 153 | + } |
| 154 | + } |
| 155 | + return res; |
| 156 | + } |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +### **C++** |
| 161 | + |
| 162 | +```cpp |
| 163 | +class Solution { |
| 164 | +public: |
| 165 | + int longestConsecutive(vector<int> &nums) { |
| 166 | + int n = nums.size(); |
| 167 | + if (n < 2) |
| 168 | + return n; |
| 169 | + sort(nums.begin(), nums.end()); |
| 170 | + int res = 1, t = 1; |
| 171 | + for (int i = 1; i < n; ++i) |
| 172 | + { |
| 173 | + if (nums[i] == nums[i - 1]) |
| 174 | + continue; |
| 175 | + if (nums[i] - nums[i - 1] == 1) |
| 176 | + { |
| 177 | + ++t; |
| 178 | + res = max(res, t); |
| 179 | + } |
| 180 | + else |
| 181 | + { |
| 182 | + t = 1; |
| 183 | + } |
| 184 | + } |
| 185 | + return res; |
| 186 | + } |
| 187 | +}; |
| 188 | +``` |
| 189 | +
|
| 190 | +```cpp |
| 191 | +class Solution { |
| 192 | +public: |
| 193 | + int longestConsecutive(vector<int> &nums) { |
| 194 | + unordered_set<int> s; |
| 195 | + for (int num : nums) |
| 196 | + s.insert(num); |
| 197 | + int res = 0; |
| 198 | + for (int num : nums) |
| 199 | + { |
| 200 | + if (!s.count(num - 1)) |
| 201 | + { |
| 202 | + int t = 1, next = num + 1; |
| 203 | + while (s.count(next++)) |
| 204 | + ++t; |
| 205 | + res = max(res, t); |
| 206 | + } |
| 207 | + } |
| 208 | + return res; |
| 209 | + } |
| 210 | +}; |
| 211 | +``` |
| 212 | + |
| 213 | +### **Go** |
| 214 | + |
| 215 | +```go |
| 216 | +func longestConsecutive(nums []int) int { |
| 217 | + n := len(nums) |
| 218 | + if n < 2 { |
| 219 | + return n |
| 220 | + } |
| 221 | + sort.Ints(nums) |
| 222 | + res, t := 1, 1 |
| 223 | + for i := 1; i < n; i++ { |
| 224 | + if nums[i] == nums[i-1] { |
| 225 | + continue |
| 226 | + } |
| 227 | + if nums[i]-nums[i-1] == 1 { |
| 228 | + t++ |
| 229 | + res = max(res, t) |
| 230 | + } |
| 231 | + } |
| 232 | + return res |
| 233 | +} |
| 234 | + |
| 235 | +func max(a, b int) int { |
| 236 | + if a > b { |
| 237 | + return a |
| 238 | + } |
| 239 | + return b |
| 240 | +} |
| 241 | +``` |
| 242 | + |
| 243 | +### **Go** |
| 244 | + |
| 245 | +```go |
| 246 | +func longestConsecutive(nums []int) int { |
| 247 | + s := make(map[int]bool) |
| 248 | + for _, num := range nums { |
| 249 | + s[num] = true |
| 250 | + } |
| 251 | + res := 0 |
| 252 | + for _, num := range nums { |
| 253 | + if !s[num-1] { |
| 254 | + t, next := 1, num+1 |
| 255 | + for s[next] { |
| 256 | + next++ |
| 257 | + t++ |
| 258 | + } |
| 259 | + res = max(res, t) |
| 260 | + } |
| 261 | + } |
| 262 | + return res |
| 263 | +} |
62 | 264 |
|
| 265 | +func max(a, b int) int { |
| 266 | + if a > b { |
| 267 | + return a |
| 268 | + } |
| 269 | + return b |
| 270 | +} |
63 | 271 | ```
|
64 | 272 |
|
65 | 273 | ### **...**
|
|
0 commit comments