[Leetcode] 128, 16, 18
来源:互联网 发布:域名名词解释 编辑:程序博客网 时间:2024/06/09 22:11
128. Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
For example,
Given [100, 4, 200, 1, 3, 2]
,
The longest consecutive elements sequence is [1, 2, 3, 4]
. Return its length: 4
.
Your algorithm should run in O(n) complexity.
Solution: 哈希表存储后,两头查询。快排的时间复杂度是O(nlogn),但此题要求O(n),因此此题不能进行排序操作。
但是,排序算法中有一个很特别的算法,时间复杂度也仅为O(n):
假设需要排序的数据是整数且范围是有限的0<=x<=max,那么可以建立一个数组x[0]-x[max],初始化为0,遍历无序数组a,然后将x[an]++,这样就能在O(n)的时间复杂度内进行排序。
此题数据为整数,但是没有给出范围,因此不能使用这种排序算法,但是可以将其进行变体,使用unordered_map来存储,然后对于一个数据向两头查找,就能够得到包含它的最长连续序列。
Note:map和unordered_map对比。
map和unordered_map的使用方法类似,但是实现方式不同,导致性能有差异:
map使用红黑树实现,unordered_map使用哈希表实现。
map内部是有序的,每次插入和查询的复杂度均为logn。
但我查到的资料对于unordered_map的性能有不同的说法,unordered_map的查询操作是公认的快,但是插入操作有说比map慢,也有说比map快。但就哈希表的特性和这题解法来说,用unordered_map进行插入操作时间复杂度应该在O(1)才对,但是鉴于我不清楚unordered_map具体的实现细节,可能也有待补充的地方。(待查)
Code:
class Solution {public: int longestConsecutive(vector<int>& nums) { unordered_map<int,bool> m; for(auto i:nums) m[i] = false; //遍历的简化写法,auto:自动判断变量类型 int maxlength = 0; for(auto i:nums){ if(m[i]==true) continue; else{ int min = i; int max = i; int length = 1; int end = false; while(!end){ end = true; if(m.find(min-1)!=m.end()){ m[min-1] = true; min--; length++; end = false; } if(m.find(max+1)!=m.end()){ m[max+1] = true; max++; length++; end = false; } } if(maxlength<length) maxlength = length; } } return maxlength; }};
16. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
Solution: 排序之后,夹逼(不需要遍历)
Code:
class Solution {public: int threeSumClosest(vector<int>& nums, int target) { sort(nums.begin(), nums.end()); int cloSum = nums[0] + nums[1] + nums[2]; for(auto a = nums.begin(); a<prev(nums.end(),2); a = upper_bound(a,prev(nums.end(),2),*a)){//得到下一个不重复的a值 auto b = next(a); auto c = prev(nums.end()); while(b<c){ int sum = *a + *b + *c; if(abs(sum-target)<abs(cloSum-target)){ cloSum = sum; } if(sum<target) b++; else c--; } } return cloSum; }};
18. 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note: The solution set must not contain duplicate quadruplets.
Solution: 排序之后,两层遍历之后夹逼(不需要遍历)
Code:
class Solution {public: vector<vector<int>> fourSum(vector<int>& nums, int target) { vector<vector<int>> result; if(nums.size()<4) return result; sort(nums.begin(), nums.end()); for(auto a = nums.begin(); a<prev(nums.end(), 3); a = upper_bound(a, prev(nums.end(), 3), *a)){ int t = target - *a; for(auto b = next(a); b<prev(nums.end(),2); b =upper_bound(b, prev(nums.end(),2), *b)){ auto c = next(b); auto d = prev(nums.end()); while(c<d){ int sum = *b + *c + *d; if(sum==t){ vector<int> r; r.push_back(*a); r.push_back(*b); r.push_back(*c); r.push_back(*d); result.push_back(r); c = upper_bound(c, prev(nums.end()), *c); }else if(sum>t){ d = prev(d); }else if(sum<t){ c = next(c); } } } } return result; }};
18. 4S
阅读全文
0 0
- [Leetcode] 128, 16, 18
- LeetCode#15* 3Sum && LeetCode#16 3Sum Closest && LeetCode#18 4Sum
- Leetcode 128
- leetcode 18
- Leetcode 18
- Leetcode(18)
- Leetcode 16
- Leetcode(16)
- LeetCode 16
- leetcode-1-15-16-18-Ksum
- leetcode note--leetcode 16 3Sum Closest
- leetcode 128: Surrounded Regions
- *leetcode #128 in cpp
- Leetcode no. 128
- leetCode练习(128)
- [LeetCode] 128: Valid Sudoku
- LeetCode 18: 4Sum
- [LeetCode] Implement strStr() [18]
- Linux中其他环境变量配置文件
- Python的条件表达式和lambda表达式
- retrofit 请求后台接口url时参数中用URLEncoder.encode编码方式
- MP4文件格式详解——结构概述
- 过滤器使用与bean注入
- [Leetcode] 128, 16, 18
- virtualBox 不能选择64位
- 背包问题
- 图片懒加载
- 最近发现系统rabbitmq丢消息比较严重,于是想了些方案来查找原因,给将消息发送方式添加确认机制。 我们在本地模拟了wms发送打标消息的场景. 1. 有事务 2. 先发点对点队列, 再发订
- HDU 6069 Counting Divisors【区间素筛】【经典题】【好题】
- spring-cloud跨域问题
- POJ-1611 The Suspects
- eclipse中各种图标的含义