herrDeng網內搜尋

自訂搜尋

Ads

2025年5月15日 星期四

C++ Py3 Greedy 2 pointer速解(含1行解)Leetcode 2900 Longest Unequal Adjacent ...


C++ Py3 Greedy 2 pointer速解2900  Longest Unequal Adjacent Groups Subsequence I
C++ 會採用 2 -pointers主要的原因,就是不要額外的陣列,達到真正的O(1)空間解。
[Py3一行解請進]
---------
The main reason why C++ uses 2-pointers is to avoid extra arrays and achieve a true O(1) space solution.
  1. class Solution:
  2. def getLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]:
  3. return [words[0]]+[ words[i+1] for i, (x, y) in enumerate(zip(groups, groups[1:])) if x!=y]

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章