[LeetCode]1089. Duplicate Zeros
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
Solution:
这题目的大概意思是让你遍历数组,如果遇到0就往0后面再添加一个0,之后的元素向右平移,超过数据长度的元素会被舍弃。
这里说只能原地修改数组,这句话有两种解释。第一个解释是不能使用额外的空间,第二个解释是不能返回新的数组(方法返回类型是void)。
解法一:队列标记替换
最直观的解法是,我们用一个队列保存需要平移的元素,然后遍历数组。如果队列不为空就用队列里最后加入的元素替换当前元素,如果遇到了0就把0加入到队列。
class Solution {
public void duplicateZeros(int[] arr) {
LinkedList<Integer> queue=new LinkedList<>();
for(int i=0;i<arr.length;i++){
if(0==arr[i]){
queue.add(arr[i]);
}
if(queue.size()>0){
queue.add(arr[i]);
arr[i]=queue.poll();
}
}
}
}
该解法性能如下:
执行用时 : 4 ms, 在所有 Java 提交中击败了57.21%的用户
内存消耗 :37.4 MB, 在所有 Java 提交中击败了100.00%的用户
解法二:双指针
双指针也许是本题的最优解法。具体思路是维护一个快指针和一个慢指针。快指针是遇到0
就多进一步。这样遍历一遍数据后,快指针和慢指针会有一个差值。这个差值就是需要填充0
的个数。
接下来,我们需要从后向前遍历数组。如果慢指针指向的元素不为0,则把快指针指向的元素替换为慢指针指向的元素;如果慢指针指向的元素为0,则把快指针和快指针之前指向的元素替换为0。
你可能会发现对于不同的数组,第一遍遍历之后fast指针的值是不一样的。区别在于数组末尾是否为0,如果末尾为0,则fast指针的值(数组索引)为数组长度+1。如果末尾不是0,则fast指针的值是数组长度。其实数组最后一位是0的话,其实是不用复制这个值的。因此从后向前遍历的时候需要判断fast指针的值是否小于n,这样就可以把数组末尾为0的时候就不会复制了。
class Solution {
public void duplicateZeros(int[] arr) {
int slow = 0;
int fast = 0;
int n = arr.length;
while (fast < n) {
if (arr[slow] == 0) {
fast++;
}
fast++;
slow++;
}
fast--;
slow--;
while (slow >= 0) {
if (fast < n) {
arr[fast] = arr[slow];
}
if (arr[slow] == 0) {
arr[--fast] = arr[slow];
}
fast--;
slow--;
}
}
}
该解法性能如下:
执行用时 : 1 ms, 在所有 Java 提交中击败了99.87%的用户
内存消耗 :37.1 MB, 在所有 Java 提交中击败了100.00%的用户
[LeetCode]1089. Duplicate Zeros的更多相关文章
- 【Leetcode_easy】1089. Duplicate Zeros
problem 1089. Duplicate Zeros 题意: solution: 其中关于虚拟新数组的下标的计算还是有点迷糊... class Solution { public: void d ...
- 【leetcode】1089. Duplicate Zeros
题目如下: Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the re ...
- LeetCode 1089. 复写零(Duplicate Zeros) 72
1089. 复写零 1089. Duplicate Zeros 题目描述 给你一个长度固定的整数数组 arr,请你将该数组中出现的每个零都复写一遍,并将其余的元素向右平移. 注意:请不要在超过该数组长 ...
- LeetCode.1089-重复的0(Duplicate Zeros)
这是小川的第392次更新,第423篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第255题(顺位题号是1089).给定一个固定长度的整数数组arr,复制每次出现的零,将剩 ...
- [LeetCode] Remove Duplicate Letters 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- [LeetCode] Contains Duplicate III 包含重复值之三
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- [LeetCode] Contains Duplicate II 包含重复值之二
Given an array of integers and an integer k, return true if and only if there are two distinct indic ...
- [LeetCode] Contains Duplicate 包含重复值
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- [LeetCode] Delete Duplicate Emails 删除重复邮箱
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...
随机推荐
- Java中非静态成员变量、静态成员变量的初始化时机
转: Java中非静态成员变量.静态成员变量的初始化时机. 2018年05月22日 11:48:11 SilenceCarrot 阅读数 421 版权声明:技术就要分享才有意思,欢迎大家分享(注明 ...
- maskrcnn_benchmark 理解
转载与参考: 1,https://zhuanlan.zhihu.com/p/58101945 2,https://blog.csdn.net/linolzhang/article/details/71 ...
- openstack部署dashboard
1.下载安装包 yum install openstack-dashboard 2.编辑配置文件 cp /etc/openstack-dashboard/local_settings /etc/ope ...
- YII框架中可以使用foreach遍历对象以及可以使用数组形式直接访问对象的原因
YII框架中可以使用foreach遍历对象以及可以使用数组形式直接访问对象的原因在YII框架的使用过程中,我们可以使用foreach直接遍历findAll等方法返回的对象的属性为什么呢?其实这与CMo ...
- Spring Cloud(3):配置服务(Config)
Spring Cloud Config的目标是在在大量的微服务中,将服务配置信息和和服务的实际物理部署分离,且服务配置服务不应与服务实例一起部署.配置信息应该作为环境变量传递给正在启动的服务,或者在服 ...
- Code First EF 多对多时拆分两个一对多
(*)多对多中还可以为中间表建立一个实体方式映射.当然如果中间关系表还想有其他字段,则要必须为中间表建立实体类(中间表和两个表之间就是两个一对多的关系了). demo项目 WebApp22 GitHu ...
- 【Zookeeper】本地模式安装
安装步骤 上传gz包 通过rz命令,将zookeeper-3.4.10.tar.gz安装包上传到/opt/soft/文件夹下. [root@bigdata111 soft]# rz [root@big ...
- 使用logstash迁移elasticsearch数据
支持同集群复制和跨集群复制 优点:通过简单配置即可实现.零编码. 缺点:logstash 单点运行迁移,速度一般. 以es2.2.1 logstash2.2.1 为例 以下logstash 配置功能 ...
- [转帖]图说Docker架构的各种信息
图说Docker架构的各种信息 2018-07-18 15:16:04作者:linux人稿源:运维之美 https://ywnz.com/linuxyffq/2344.html 介绍Docker架 ...
- Codeforces 1148E Earth Wind and Fire
分析 必要条件: ① $\sum_{i=1}^{n} s_i = \sum_{i=1}^{n} t_i$ 预处理: 将 $s, t$ 从小到大排序. 尝试一 首尾匹配.例子 s = 2, 2, 4, ...