题目如下:

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. 1 <= arr.length <= 10000
  2. 0 <= arr[i] <= 9

解题思路:从头开始遍历arr,如果arr[i]等于0,在i+1位置插入0,索引移动到i+2;如果不为0,索引移动到i+1。

代码如下:

class Solution(object):
def duplicateZeros(self, arr):
"""
:type arr: List[int]
:rtype: None Do not return anything, modify arr in-place instead.
"""
length = len(arr)
inx = 0
while inx < length:
if arr[inx] != 0:
inx += 1
continue
else:
arr.insert(inx,0)
arr.pop(-1)
inx += 2

【leetcode】1089. Duplicate Zeros的更多相关文章

  1. 【Leetcode_easy】1089. Duplicate Zeros

    problem 1089. Duplicate Zeros 题意: solution: 其中关于虚拟新数组的下标的计算还是有点迷糊... class Solution { public: void d ...

  2. 【leetcode】Contains Duplicate & Rectangle Area(easy)

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

  3. 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)

    [LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  4. 【LEETCODE】49、数组分类,简单级别,题目:566,1089

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  5. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  6. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

  7. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

  8. 【LeetCode】哈希表 hash_table(共88题)

    [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...

  9. 【LeetCode】树(共94题)

    [94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 ...

随机推荐

  1. Oracle 10046 event

    http://czmmiao.iteye.com/blog/1497509 10046事件概述Oracle的10046事件,可以跟踪应用程序所执行的SQL语句,并且得到其解析次数.执行次数,CPU使用 ...

  2. vue +ts 在router的路由中import报错的解决方案

    在router.ts中引入.vue文件,会提示打不到module,但是编译可能成功,运行也不报错 找了好久,发现了这个答案 https://segmentfault.com/a/11900000167 ...

  3. linux 互斥量

    互斥量(mutex)从本质上来说是一把锁,在访问共享资源前对互斥量进行加锁,在访问完后释放互斥量上的锁. 对互斥量进行加锁以后,任何其他试图再次对互斥量加锁的线程都将会被阻塞直到当前线程释放该互斥锁. ...

  4. unique()函数

    unique()函数可输出列表,元组和series中的不同元素. 且只能输出dataframe的某一列series中的不同元素,不能同时输出. 返回的都是一个ndarry,但不能输出ndarry中的不 ...

  5. dcef3 为按钮添加单击事件

    http://ju.outofmemory.cn/entry/78083 dcef3 为按钮添加单击事件 BccSafe's Blog 2014-06-01 3706 阅读 DOM 演示dcef3操作 ...

  6. package__init__用途

    baidu包,假设在baidu包下有N个模块,分别是baidu1.py.baidu2.py,baidu3.py, baiduHq.py(baidu1.py,baidu2.py,baidu3.py模块代 ...

  7. centos 7下安装pycharm专业版

    1.下载pycharm的linux版本的软件包,下载地址: http://www.jetbrains.com/pycharm/download/#section=linux 2.解压 $ tar -x ...

  8. pandas 入门(2)

    from pandas import Series, DataFrame, Index import numpy as np from numpy import nan as NA obj = Ser ...

  9. oracle--表分区、分区索引

    --|/ range分区 create table sale( product_id varchar2(5), sale_count number(10,2) ) partition by range ...

  10. [Web 前端] 007 css 常见的七种选择器

    1. 标签选择器 影响范围大 建议尽量应用在层级选择器中 举例 <!-- body 体中的 div --> <div>box...</div> /* style 中 ...