Skip to content

Commit c1ff28e

Browse files
committed
feat: add solutions to lc/lcof2 problem:Longest Consecutive Sequence
1 parent 3810f9e commit c1ff28e

File tree

26 files changed

+1631
-51
lines changed

26 files changed

+1631
-51
lines changed

lcof2/剑指 Offer II 119. 最长连续序列/README.md

+208
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,230 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

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+
4769
<!-- tabs:start -->
4870

4971
### **Python3**
5072

5173
<!-- 这里可写当前语言的特殊实现逻辑 -->
5274

5375
```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+
```
5493

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
55107
```
56108

57109
### **Java**
58110

59111
<!-- 这里可写当前语言的特殊实现逻辑 -->
60112

61113
```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+
}
62264

265+
func max(a, b int) int {
266+
if a > b {
267+
return a
268+
}
269+
return b
270+
}
63271
```
64272

65273
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int longestConsecutive(vector<int> &nums) {
4+
unordered_set<int> s;
5+
for (int num : nums)
6+
s.insert(num);
7+
int res = 0;
8+
for (int num : nums)
9+
{
10+
if (!s.count(num - 1))
11+
{
12+
int t = 1, next = num + 1;
13+
while (s.count(next++))
14+
++t;
15+
res = max(res, t);
16+
}
17+
}
18+
return res;
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func longestConsecutive(nums []int) int {
2+
s := make(map[int]bool)
3+
for _, num := range nums {
4+
s[num] = true
5+
}
6+
res := 0
7+
for _, num := range nums {
8+
if !s[num-1] {
9+
t, next := 1, num+1
10+
for s[next] {
11+
next++
12+
t++
13+
}
14+
res = max(res, t)
15+
}
16+
}
17+
return res
18+
}
19+
20+
func max(a, b int) int {
21+
if a > b {
22+
return a
23+
}
24+
return b
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int longestConsecutive(int[] nums) {
3+
Set<Integer> s = new HashSet<>();
4+
for (int num : nums) {
5+
s.add(num);
6+
}
7+
int res = 0;
8+
for (int num : nums) {
9+
if (!s.contains(num - 1)) {
10+
int t = 1, next = num + 1;
11+
while (s.contains(next++)) {
12+
++t;
13+
}
14+
res = Math.max(res, t);
15+
}
16+
}
17+
return res;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def longestConsecutive(self, nums: List[int]) -> int:
3+
s, res = set(nums), 0
4+
for num in nums:
5+
if num - 1 not in s:
6+
t = 1
7+
next = num + 1
8+
while next in s:
9+
t += 1
10+
next += 1
11+
res = max(res, t)
12+
return res

0 commit comments

Comments
 (0)