[LeetCode]2433. Find The Original Array of Prefix Xor (Medium) Python-2023.10.31
Posted on 2023-10-31 17:17 Uriel 閱讀(40) 評論(0) 編輯 收藏 引用 所屬分類: 閑來無事重切Leet Code 、大水題給出一列數pref,其中pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i],求原數組arr,不斷求pref相鄰值的異或即可
1 #2433
2 #Runtime: 621 ms (Beats 43.23%)
3 #Memory: 33.2 MB (Beats 7.74%)
4
5 class Solution(object):
6 def findArray(self, pref):
7 """
8 :type pref: List[int]
9 :rtype: List[int]
10 """
11 ans = [pref[0]]
12 for i in xrange(1, len(pref)):
13 ans.append(pref[i] ^ pref[i - 1])
14 return ans
2 #Runtime: 621 ms (Beats 43.23%)
3 #Memory: 33.2 MB (Beats 7.74%)
4
5 class Solution(object):
6 def findArray(self, pref):
7 """
8 :type pref: List[int]
9 :rtype: List[int]
10 """
11 ans = [pref[0]]
12 for i in xrange(1, len(pref)):
13 ans.append(pref[i] ^ pref[i - 1])
14 return ans