{1,0,3,0,4,0,2,0,12,0,9}
转换成
{0,0,0,0,0,1,3,4,2,12,9}
import java.util.Arrays;
public class lzwCode {
public static void main(String [] args) {
int [] arr = {1,0,3,0,4,0,2,0,12,0,9};
arr = codeTest(arr);
System.out.println(Arrays.toString(arr));
}
public static int [] codeTest (int [] arr) {
int [] temp = new int[arr.length];
int low,hight;
for (low = arr.length - 1, hight = low; low >= 0; low--) {
if (arr[low] != 0){
temp[hight] = arr[low];
hight --;
}
}
return temp;
}
}
控制台结果如下:
PHP实现该功能:
<?php
$arr = array(1,0,3,0,4,0,2,0,12,0,9);
$arrTemp = array();
for ($i=0; $i<count($arr); $i++){
$arrTemp[] = 0;
}
for ($low = count($arr)-1, $right = $low; $low>=0; $low--) {
if ($arr[$low] != 0) {
$arrTemp[$right] = $arr[$low];
$right --;
}
}
?>