【leetcode】1089. Duplicate Zeros
题目如下:
Given a fixed length array
arrof 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 <= 100000 <= 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的更多相关文章
- 【Leetcode_easy】1089. Duplicate Zeros
problem 1089. Duplicate Zeros 题意: solution: 其中关于虚拟新数组的下标的计算还是有点迷糊... class Solution { public: void d ...
- 【leetcode】Contains Duplicate & Rectangle Area(easy)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- 【LeetCode】652. Find Duplicate Subtrees 解题报告(Python)
[LeetCode]652. Find Duplicate Subtrees 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LEETCODE】49、数组分类,简单级别,题目:566,1089
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
- 【LeetCode】树(共94题)
[94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 ...
随机推荐
- fedora23帮定键盘系统操作快捷键
在All settings -> keyboard 主要是以super为主, 然后有 super+ shift+...虽然感觉用 ctrl+super+... 来组合更方便, 但是用 shift ...
- python3 -m pip install django, -m参数
python -m xxx.py 作用是:把xxx.py文件当做模块启动但是我一直不明白当做模块启动到底有什么用.python xxx.py和python -m xxx.py有什么区别! 自问自答: ...
- 在mpvue或者Vue中使用VUEX
第一个页面Index,主页哦 import Vue from'vue' import Vuex from'vuex' import getters from'./getters' import sta ...
- nw打包vue项目 安装包
接着上篇nw打包vue项目exe中: copy /b nw.exe+dome.nw dome.exe 出现了dome.exe文件之后,要是打算打包成为安装包,网上推荐的是Inno Setup Comp ...
- 练习4-python+selenium+pandas
最近对于python的第三方库pandas比较有兴趣,在学习的过程中也简单的结合selenium做了一个简单的小工具 最新公司用一个外部系统来记录,追踪BUG,可是这个系统并不是专业的BUG管理系统, ...
- 聊聊NTLM认证协议
近期发现多家安全媒体发布NTLM协议漏洞的文章.他们越说越术语,越说越官方,如此这般下去,他们写出来到底给谁看?大雅就是俗,让我来一篇俗文.啥是NTLM呢?微软windows系统的用户账号存储密码哈希 ...
- Angular5 错误: ngModel cannot be used to register form controls with a parent formGroup directive
在创建一个表单时,出现了这样的错误: 原因是,在最外层的form中使用了 formGroup 指令,但在下面的某个input 元素中,使用了ngModel 指令,但没有加入formControl 指令 ...
- k8s--发展历程、知识图谱、组件说明
kubernetes 1.发展历程 基础设施级服务infrastructure as a service 阿里云 平台设施级服务 platform as a service 新浪云 软件设施级服务 s ...
- loading 加载工具
loading 加载工具:http://loading.awesomes.cn/
- 关于Python学习的一点说明
关于Python学习的一点说明 我是用什么地方的资料来学习python的呢? 答案当然是鼎鼎大名的GitHub了. 5万多星推荐,100天让你从入门到精通,你值得拥有,点我进入查看