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
Approach #1 Queue队列的使用,我的解法
class Solution {
public void duplicateZeros(int[] arr) {
int len = arr.length;
Queue<Integer> queue = new LinkedList<Integer>();
for(int i=0; i<len; i++){
queue.offer(arr[i]);
if(arr[i]==0){
queue.offer(0);
}
}
for(int i = 0; i<len; i++){
arr[i] = queue.poll();
}
}
}
分析:
发现这个其实是一个先进先出的数据结构,即非零的元素直接入队,是零的元素复制一个自己然后双双入队,在出队的时候,只有前len个进入的元素可以出去。
时间复杂度 O(N)
空间复杂度 O(N)
关于java 中 Queue 的使用:
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
//add()和remove()方法在失败的时候会抛出异常(不推荐)
Queue<String> queue = new LinkedList<String>();
//添加元素
queue.offer("a");
queue.offer("b");
queue.offer("c");
queue.offer("d");
queue.offer("e");
for(String q : queue){
System.out.println(q);
}
System.out.println("===");
System.out.println("poll="+queue.poll()); //返回第一个元素,并在队列中删除
for(String q : queue){
System.out.println(q);
}
System.out.println("===");
System.out.println("element="+queue.element()); //返回第一个元素
for(String q : queue){
System.out.println(q);
}
System.out.println("===");
System.out.println("peek="+queue.peek()); //返回第一个元素
for(String q : queue){
System.out.println(q);
}
}
}
输出:
a
b
c
d
e
===
poll=a
b
c
d
e
===
element=b
b
c
d
e
===
peek=b
b
c
d
e
offer,add 区别:
一些队列有大小限制,因此如果想在一个满的队列中加入一个新项,多出的项就会被拒绝。
这时新的 offer 方法就可以起作用了。它不是对调用 add() 方法抛出一个 unchecked 异常,而只是得到由 offer() 返回的 false。
poll,remove 区别:
remove() 和 poll() 方法都是从队列中删除第一个元素。remove() 的行为与 Collection 接口的版本相似, 但是新的 poll() 方法在用空集合调用时不是抛出异常,只是返回 null。因此新的方法更适合容易出现异常条件的情况。
peek,element区别:
element() 和 peek() 用于在队列的头部查询元素。与 remove() 方法类似,在队列为空时, element() 抛出一个异常,而 peek() 返回 null。
Approach #2 Two Pointers
public void duplicateZeros(int[] arr) {
int countZero = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) countZero++;
}
int len = arr.length + countZero;
//We just need O(1) space if we scan from back
//i point to the original array, j point to the new location
for (int i = arr.length - 1, j = len - 1; i < j; i--, j--) {
if (arr[i] != 0) {
if (j < arr.length) arr[j] = arr[i];
} else {
if (j < arr.length) arr[j] = arr[i];
j--;
if (j < arr.length) arr[j] = arr[i]; //copy twice when hit '0'
}
}
}
Explanation
We can improve it to O(N) time and O(1) space.
Basically, we apply two pointers.
i is the position in the original array,
j is the position in the new array.
(the original and the new are actually the same array.)
The first we pass forward and count the zeros.
The second we pass backward and assign the value from original input to the new array.
so that the original value won't be overridden too early.