鱼C论坛

 找回密码
 立即注册
查看: 2399|回复: 3

[吹水] 一道有意思的题目 -- Duplicate Zeros

[复制链接]
发表于 2020-5-12 02:18:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 赚小钱 于 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: [1,0,2,3,0,4,5,0] 
Output: null 
Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4] 

Example 2:
Input: [1,2,3] 
Output: null 
Explanation: After calling your function, the input array is modified to: [1,2,3] 

Note:
  • 1 <= arr.length <= 10000
  • 0 <= arr[i] <= 9


测试

输入
[1,0,2,3,0,4,5,0]
[1, 0, 0, 0]
[1, 1, 1, 1]
[1, 0, 0, 1]
[1, 1, 1, 0]
[1, 0, 0, 0, 0]
[1, 1, 1, 1, 0]
[1, 0, 0, 1, 0]
[1, 1, 1, 0, 0]
[1, 0, 0, 0, 1]
[1, 1, 1, 1, 1]
[1, 0, 0, 1, 1]
[1, 1, 1, 0, 1]


输出
[1,0,0,2,3,0,0,4]
[1,0,0,0]
[1,1,1,1]
[1,0,0,0]
[1,1,1,0]
[1,0,0,0,0]
[1,1,1,1,0]
[1,0,0,0,0]
[1,1,1,0,0]
[1,0,0,0,0]
[1,1,1,1,1]
[1,0,0,0,0]
[1,1,1,0,0]

进阶1
空间复杂度 O(1)。

进阶2
时间复杂度 O(n)。

https://leetcode.com/problems/duplicate-zeros
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-5-12 07:55:46 From FishC Mobile | 显示全部楼层
你是英国人吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-12 10:35:40 | 显示全部楼层
本帖最后由 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))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-12 11:35:42 | 显示全部楼层
本帖最后由 赚小钱 于 2020-5-12 11:39 编辑
sunrise085 发表于 2020-5-12 10:35
有点意思。不过很简单啊
我用python


亲,这道题一定没有 O(1) 的时间复杂度算法。

觉得简单,可以挑战一下空间复杂度 O(1) 的实现。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 17:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表