【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 ...
随机推荐
- MySQL高可用方案 MHA之二 master_ip_failover
异步主从复制架构master:10.150.20.90 ed3jrdba90slave:10.15.20.97 ed3jrdba9710.150.20.132 ed3jrdba132manager:1 ...
- PLSQL设置数据库选项
1.将数据库安装目录下的"NETWORK"文件夹复制到client(客户端)安装目录下 : 2.修改"NETWORK"-->"ADMIN&quo ...
- SpringMvc @ModelAttribute 的用法
参考:Spring 3.x 企业应用开发实战 第15章:SpringMvc 页码:532 ModelAttribute 从字面上解释就是模型的属性. 对于MVC框架来说是模型数据是最重要的,因为 ...
- idea 2017 快捷键
Ctrl+Shift + Enter,语句完成 “!”,否定完成,输入表达式时按 “!”键 Ctrl+E,最近的文件 Ctrl+Shift+E,最近更改的文件 Shift+Click,可以关闭文件 C ...
- Vue2.0---将页面中表格数据导出excel
这不是教程,是随笔. 项目中将后台返回的数据v-for到表格中,然后需要将这个表格导出为EXCEL 只说怎么做. 一.需要安装三个依赖: npm install -S file-saver xlsx ...
- Backbone中bind和bindAll的作用
本文参考: http://blog.bigbinary.com/2011/08/18/understanding-bind-and-bindall-in-backbone.html bindAll内部 ...
- python 链接交换机并执行相关命令
原文地址 https://blog.csdn.net/u010897775/article/details/80311786?utm_source=blogxgwz0 # encoding=utf-8 ...
- Windows 10更新后无法启动Dolby音频驱动程序
在电脑更新Windows 10 1903版本后,重启出现如下问题: 经查,这与驱动强制签名有关.解决方法如下: 打开"设置"->"更新与安全"->& ...
- synchronized的底层实现原理
转自:http://www.cnblogs.com/paddix/p/5367116.html 如果对上面的执行结果还有疑问,也先不用急,我们先来了解Synchronized的原理,再回头上面的问题就 ...
- PCB电路设计 altiumdesigner(项目软件总结)
1.Altium designer 10在PCB里面复制粘贴,比CAD里面多一个动作,就是点击ctrl+C后,要左键点一下复制基点,比如某根线端点或者焊盘,再粘贴,就是基于刚才点的那个为基点粘贴了.2 ...