一道有意思的题目 -- Duplicate Zeros
本帖最后由 赚小钱 于 2020-5-12 02:20 编辑Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right.
Note that elements beyond the length of the original array are not written.
Do the above modifications to the input array in place, do not return anything from your function.
Example 1:
Input:
Output: null
Explanation: After calling your function, the input array is modified to:
Example 2:
Input:
Output: null
Explanation: After calling your function, the input array is modified to:
Note:
[*]1 <= arr.length <= 10000
[*]0 <= arr <= 9
测试
输入
输出
进阶1
空间复杂度 O(1)。
进阶2
时间复杂度 O(n)。
https://leetcode.com/problems/duplicate-zeros 你是英国人吗 本帖最后由 sunrise085 于 2020-5-12 10:38 编辑
有点意思。不过很简单啊
我用python
def funnyfun(list1): #时间复杂度 O(n)
list2=list()
for each in list1:
list2.append(each)
if len(list1)==len(list2):
return list2
if each == 0:
list2.append(each)
if len(list1)==len(list2):
return list2
def funnyfun2(list1): #时间复杂度 O(1)
return eval(str(list1).replace('0','0,0'))[:len(list1)]
list1=eval(input("Input: "))
print(list1)
print("Output:",funnyfun(list1))
print("Output:",funnyfun2(list1)) 本帖最后由 赚小钱 于 2020-5-12 11:39 编辑
sunrise085 发表于 2020-5-12 10:35
有点意思。不过很简单啊
我用python
亲,这道题一定没有 O(1) 的时间复杂度算法。
觉得简单,可以挑战一下空间复杂度 O(1) 的实现。
页:
[1]