力扣2657-剑指offer II 119 : 最长连续子序列
- 题目
- 代码带注释
- 考察知识点
- 1.vector容器的创建,添加容器的方法两种
- 2.考察sort用法
- 3.如何打印vector容器中的元素
题目
给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。
代码带注释
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84#include<bits/stdc++.h> using namespace std; class Solution { public: int longestConsecutive(vector<int>& nums) { int res = 0; vector<int> resArr_; int maxRes = 0; sort(nums.begin(),nums.end()); int numsLen = nums.size(); if(numsLen == 0) return 0; //如果此容器的数据个数不为零的话,其最长连续子序列至少是1 res += 1 ; cout<<"numsLen:"<<numsLen<<endl; for(vector<int>::iterator it = nums.begin();it != nums.end();it++){ cout<<*it<<endl; } /** * @brief 核心部分 * 1.因为是两个数之间的比较 那么循环的次数是 数据长度减去1 * 2.如果相差1或者-1的情况下就把连续序列长度加1 -1是考虑 0 - 1的情况 * 3.如果相差为0 那么考虑 12333333456 的连续子序列中间插上了连续的数字 连续子序列长度不变 * 4.不是以上的情况 那就把得到的结果先放进容器中 并且将这次的连续子序列长度改为1 */ for(int i = 0;i < numsLen - 1;i++){ if((nums[i+1] - nums[i]) == 1 || (nums[i+1] - nums[i] == -1)) { cout<<"nums[i]:"<<nums[i]<<" nums[i+1]:"<<nums[i+1]<<endl; res++; cout<<"res: "<<res<<endl; } else if(nums[i+1] - nums[i] == 0){ continue; } else{ resArr_.push_back(res); res = 1; } } //最后需要把最新一次的连续子序列长度尾插进去容器中 resArr_.push_back(res); //比较存在数组中最长连续子序列的大小 sort(resArr_.begin(),resArr_.end()); for(vector<int>::iterator it = resArr_.begin();it != resArr_.end();it++){ cout<<"resarr:"<<*it<<endl; } return resArr_[resArr_.size() - 1]; } }; int main(){ system("chcp 65001"); vector<int> a; a.push_back(1); a.push_back(2); a.push_back(0); a.push_back(1); //[9,1,4,7,3,-1,0,5,8,-1,6] // a.push_back(100); // a.push_back(4); // a.push_back(200); // a.push_back(1); // a.push_back(3); // a.push_back(2); // a.push_back(9); // a.push_back(1); // a.push_back(4); // a.push_back(7); // a.push_back(3); // a.push_back(-1); // a.push_back(0); // a.push_back(5); // a.push_back(8); // a.push_back(-1); // a.push_back(6); Solution s1; int c = s1.longestConsecutive(a); cout<<c<<endl; // for(vector<int>::iterator it = c.begin();it != c.end();it++){ // cout<<*it; // } return 0; }
考察知识点
1.vector容器的创建,添加容器的方法两种
第一种 vectorarr(n); 这样可以把arr当做固定长度的数组来用
第二种 vecto arr, arr.push_back(100) ;长度不固定 随意在容器的尾部插入
但是记住,使用arr[]访问容器时 下标零对应的是容器的首个元素而不是最新插入的元素
2.考察sort用法
复制代码
1
2
3
4
5
6sort(iterator beg, iterator end, _Pred); // 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置 // beg 开始迭代器 // end 结束迭代器 // _Pred 谓词
第一种 针对容器 可以使用
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15void myprint(int val) { cout<<val<" "; } vecto<int> v; /*添加元素*/ //sort默认从小到大排序 sort(v.begin(),v.end()); for_each(v.begin(),v.end(),myprint); cout << endl; //从大到小排序 sort(v.begin(),v.end(),greater<int>()); for_each(v.begin(),v.end(),myprint); cout << endl;
3.如何打印vector容器中的元素
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
151. for(vector<int>::iterator it = nums.begin();it != nums.end();it++){ cout<<*it<<endl; } 2.//注意 把vector<int>::iterator 定义一个迭代器指针类型的作用 vector<int>::iterator pbegin = v.begin(); vector<int>::iterator pend =v.end(); while(pBegin != pEnd) { cout<<*pBegin<<endl; pBegin++; } 3.//使用算法中的函数 for_each(v.begin(),v.end(),myprint);
最后
以上就是粗心口红最近收集整理的关于力扣2657-剑指offer II 119 : 最长连续子序列题目代码带注释考察知识点的全部内容,更多相关力扣2657-剑指offer内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复