leetcode刷題,總結,記錄,備忘 128

2021-07-06 08:10:10 字數 1009 閱讀 4846

leetcode128longest 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.

subscribe to see which companies asked this question

我直接使用了c++的sort和unique兩個函式,先排序,然後找出重複的部分並且去除,然後再自己迴圈遍歷處理就非常簡單了。但是排序的時間複雜度貌似是nlogn,,,,我也看了網上的使用map或者set的方法,,,但是提交不通過,,,而且我自己像出來的這個方法才16ms,也不是特別慢,,,

class solution 

if (nums.size() == 1)

sort(nums.begin(), nums.end());

nums.erase(unique(nums.begin(), nums.end()), nums.end());

int len = 1;

int maxlen = 1;

for (vector::iterator it = nums.begin(); it != nums.end() - 1; ++it)

else

len = 1;}}

if (len > maxlen)

maxlen = len;

return maxlen;

}};

leetcode刷題,總結,記錄,備忘 226

leetcode226題。invert binary tree 倒置二叉樹。想不到這題還有個梗,挺有意思的。我一開始自作聰明,想用乙個棧乙個佇列來做,用中序遍歷,把節點push進棧中,把節點的指push進佇列中,遍歷完之後,分別從棧頂和佇列頂拿出節點和數值,賦值。一開始想著感覺還挺對的,遞交了幾次都...

leetcode刷題,總結,記錄,備忘83

leetcode83.for example,given1 1 2,return1 2.given1 1 2 3 3,return1 2 3.不想說什麼了,比較簡單的題目,大爺的我提了6,7遍才過,因為中間有個流程一直疏忽了,如果當前項值域和下乙個結點的值域相同的話,在將下乙個結點刪掉,即連到下乙個...

leetcode刷題,總結,記錄,備忘202

leetcode202 credits special thanks to mithmatt and ts for adding this problem and creating all test cases.還是比較簡單的題目,雖然我提的次數比較多,又拉低了通過率,真是無情。每次計算如果得到結果...