我可以在不创建新的 list/array 的情况下展平数组吗?
Can I flatten an array without making a new list/array?
假设我有 arr = [[1], [0, 0], [2], [3], [0, 0], [4]]
我可以将其扁平化为 [1, 0, 0, 2, 3, 0, 0, 4]
而不必使用 itertools、map/reduce 或列表组合吗?我正在努力想办法做到这一点。谢谢。
这是我目前试过的,是一道leetcode题:https://leetcode.com/problems/duplicate-zeros/
class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
for ix in range(len(arr) - 2):
if arr[ix] == 0:
arr[ix] = [0, 0]
del arr[-1]
else:
l = []
l.append(arr[ix])
arr[ix] = l
# Output when you return arr is [[1],[0, 0],[2],[3],[0, 0],[4]]
尝试一些递归:
def flatten(lst):
ans = []
for el in lst:
if type(el) == list:
for e in flatten(el):
ans.append(e)
else:
ans.append(el)
return ans
它将展平任何维度的列表。
你可以简单地做:
arr = sum(arr, [])
你在这里做的是添加 arr 的可迭代元素,以空数组 []
作为初始值。
- 我们这里也可以使用list copy(
[:]
),解决问题:
class Solution:
def duplicateZeros(self, A):
"""
Do not return anything, modify arr in-place instead.
"""
A[:] = [x for num in A for x in ([num] if num else [0, 0])][:len(A)]
- 此外,最佳解决方案是 N 运行时的顺序和恒定内存。 LeetCode here:
已经解决了这个问题
class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
possible_dups = 0
length_ = len(arr) - 1
# Find the number of zeros to be duplicated
for left in range(length_ + 1):
# Stop when left points beyond the last element in the original list
# which would be part of the modified list
if left > length_ - possible_dups:
break
# Count the zeros
if arr[left] == 0:
# Edge case: This zero can't be duplicated. We have no more space,
# as left is pointing to the last element which could be included
if left == length_ - possible_dups:
arr[length_] = 0 # For this zero we just copy it without duplication.
length_ -= 1
break
possible_dups += 1
# Start backwards from the last element which would be part of new list.
last = length_ - possible_dups
# Copy zero twice, and non zero once.
for i in range(last, -1, -1):
if arr[i] == 0:
arr[i + possible_dups] = 0
possible_dups -= 1
arr[i + possible_dups] = 0
else:
arr[i + possible_dups] = arr[i]
假设我有 arr = [[1], [0, 0], [2], [3], [0, 0], [4]]
我可以将其扁平化为 [1, 0, 0, 2, 3, 0, 0, 4]
而不必使用 itertools、map/reduce 或列表组合吗?我正在努力想办法做到这一点。谢谢。
这是我目前试过的,是一道leetcode题:https://leetcode.com/problems/duplicate-zeros/
class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
for ix in range(len(arr) - 2):
if arr[ix] == 0:
arr[ix] = [0, 0]
del arr[-1]
else:
l = []
l.append(arr[ix])
arr[ix] = l
# Output when you return arr is [[1],[0, 0],[2],[3],[0, 0],[4]]
尝试一些递归:
def flatten(lst):
ans = []
for el in lst:
if type(el) == list:
for e in flatten(el):
ans.append(e)
else:
ans.append(el)
return ans
它将展平任何维度的列表。
你可以简单地做:
arr = sum(arr, [])
你在这里做的是添加 arr 的可迭代元素,以空数组 []
作为初始值。
- 我们这里也可以使用list copy(
[:]
),解决问题:
class Solution:
def duplicateZeros(self, A):
"""
Do not return anything, modify arr in-place instead.
"""
A[:] = [x for num in A for x in ([num] if num else [0, 0])][:len(A)]
- 此外,最佳解决方案是 N 运行时的顺序和恒定内存。 LeetCode here: 已经解决了这个问题
class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
possible_dups = 0
length_ = len(arr) - 1
# Find the number of zeros to be duplicated
for left in range(length_ + 1):
# Stop when left points beyond the last element in the original list
# which would be part of the modified list
if left > length_ - possible_dups:
break
# Count the zeros
if arr[left] == 0:
# Edge case: This zero can't be duplicated. We have no more space,
# as left is pointing to the last element which could be included
if left == length_ - possible_dups:
arr[length_] = 0 # For this zero we just copy it without duplication.
length_ -= 1
break
possible_dups += 1
# Start backwards from the last element which would be part of new list.
last = length_ - possible_dups
# Copy zero twice, and non zero once.
for i in range(last, -1, -1):
if arr[i] == 0:
arr[i + possible_dups] = 0
possible_dups -= 1
arr[i + possible_dups] = 0
else:
arr[i + possible_dups] = arr[i]