首页 > 其他 > 详细

LeetCode开心刷题五十六天——128. Longest Consecutive Sequence

时间:2019-12-16 10:36:16      阅读:82      评论:0      收藏:0      [点我收藏+]

最近刷题进展尚可,但是形式变化了下,因为感觉眼睛会看瞎,所以好多写在纸上。本来想放到文件夹存储起来,但是太容易丢了,明天整理下,赶紧拍上来把

今晚是周末,这一周都在不停的学学学,我想下周怕是不能睡午觉了,中午回去床对我的诱惑太大了,我得想办法,一进门先把被褥收起来,再放个欢快的歌,中午少吃点,加油小可爱

之前欠下的烂帐,把太多简单题做完,导致剩下的都是难题,所以万万记住一点捷径都不要走
126. Word Ladder II
Hard

Given two words (beginWord and endWord), and a dictionary‘s word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. Each transformed word must exist in the word list. Note that beginWord is not a transformed word.

Note:

  • Return an empty list if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
  • You may assume no duplicates in the word list.
  • You may assume beginWord and endWord are non-empty and are not the same.

Example 1:

Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]

Output:
[
  ["hit","hot","dot","dog","cog"],
  ["hit","hot","lot","log","cog"]
]

Example 2:

Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

Output: []

Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.
BONUS:
1.vector begin end起至迭代器 front back起至元素 用法

向量容器(vector)是一种顺序容器,是一块连续分配的内存,支持随机访问,从数据安排的角度看,和数组极其相似,数组跟vector的区别在于:数组是静态分配空间,一旦分配了空间的大小,就不可以再改变了,例如,int a[6];而vector是动态分配内存,随着元素的不断插入,它会按照自身的一套机制不断扩充自身的容量,vector容器的容量增长是按照容器现在容量的一倍进行增长。

begin函数:
函数原型:
iterator begin();
const_iterator begin();
功能:
返回一个当前vector容器中起始元素的迭代器。

end函数:
函数原型:
iterator end();
const_iterator end();
功能:
返回一个当前vector容器中末尾元素的迭代器。

front函数:
函数原型:
reference front();
const_reference front();
功能:
返回当前vector容器中起始元素的引用。

back函数:
函数原型:
reference back();
const_reference back();
功能:
返回当前vector容器中末尾元素的引用。
————————————————
版权声明:本文为CSDN博主「LzyRapX」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/liangzhaoyang1/article/details/51853686

2.

C++中的swap函数:交换函数

好处:不用担心交换变量精度的缺失,无需构造临时变量,不会增加空间复杂度

 
128看花花酱大神的解法,发现对hashtable的了解十分不足,甚至一些常见函数都不知道是干什么的
这道题涉及的知识点竟然还有并查集,逃来逃去没逃出去。
 
128. Longest Consecutive Sequence
Hard

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

Your algorithm should run in O(n) complexity.

Example:

Input: [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.

BONUS:
auto自动类型推断,用于从初始化表达式中推断出变量的数据类型。通过auto的自动类型推断,可以大大简化我们的编程工作。下面是一些使用auto的例子。
class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_map<int,int> h;
        int ans=0;
        
        for(int num:nums){
            if(h.count(num)) continue;
            //check whether can find iterator
            auto it_l=h.find(num-1);
            auto it_r=h.find(num+1);
                
            int l=it_l!=h.end()?it_l->second:0;
            int r=it_r!=h.end()?it_r->second:0;
            int t=l+r+1;
            
            h[num]=h[num-l]=h[num+r]=t;
            
            ans=max(ans,t);
        }
        return ans;
    }
};

 

LeetCode开心刷题五十六天——128. Longest Consecutive Sequence

原文:https://www.cnblogs.com/Marigolci/p/12046855.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!