1337code+没有背单词+游戏
来源:互联网 发布:java 中级 高级 几年 编辑:程序博客网 时间:2024/05/05 23:20
今天感觉好累,起床后玩了一下午游戏。
晚上和lc,lsy,wtc一起吃饭,李骋请的,我们一起为他饯行。lc,一路顺风!
晚上回来和爸妈聊了一晚上。
现在来写今天code,然后发现今天单词没背,程序没写,dip也没学习,bst更别说了,唉!明天吧!
今天这个题目想了半天,结果网上一查还真是被自己pass的思路,哈希!!!
题目如下
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.
用哈希就很简单了,只需要查每个数字的邻域在不在集合中就ok代码如下:
classSolution{public: int longestConsecutive(std::vector<int> &num) { for(int i = 0; i < num.size(); ++i) { flags_.insert(num[i]); } int maxLen = 0; for(int i = 0; i < num.size(); ++i) { int ascendingMax = FindConsecutiveNumbers(ASCENDING, num[i]); int decendingMax = FindConsecutiveNumbers(DECENDING, num[i] - 1); if(ascendingMax + decendingMax > maxLen) { maxLen = ascendingMax + decendingMax; } } return maxLen; } private: enum OrderBy { ASCENDING, DECENDING }; int FindConsecutiveNumbers(OrderBy orderBy, int value) { int maxLen = 0; while(flags_.find(value) != flags_.end()) { ++maxLen; flags_.erase(value); if(orderBy == ASCENDING) { ++value; } else { --value; } } return maxLen; } std::unordered_set<int> flags_;};哈希表的实现
- 1337code+没有背单词+游戏
- 背单词
- 背单词
- 背单词
- 背单词
- 背单词
- 背单词
- 玩游戏背单词 v5.0 语音版 是什么
- 每日背单词
- 如何背单词
- 背单词 考四级
- 背单词的方法
- MindMap背单词
- 背单词的方法
- 简易背单词
- 怎样背单词
- 颠覆背单词
- 背单词系统
- android 广播机制
- 创建线程的两种方法,及之间的区别
- 用jquery实现鼠标滑动星级评分特效
- javascript变量
- Maclean Liu对Oracle Database 12c新特性研究汇总
- 1337code+没有背单词+游戏
- nyoj 56-阶乘因式分解(一)
- opengl 中透视投影到屏幕的对应关系
- 数组
- Nearest Common Ancestors(最近公共祖先)
- Error executing aapt: Return code -1073741819
- Hdu 1161-Eddy's mistakes
- Stagefright流媒体和本地视频播放
- android 打开各种文件(setDataAndType)