- 限制时间复杂度,一前一后两个指针即可。网易的开胃菜( ̄▽ ̄)"
/**
* 1.数组一遍遍历,把所有的零放在数组头部
* >利用low,high两个指针,主要是high移动,并调整。
*/
public class BeforeZeroArray {
public static void main(String[] args) {
int[] a = {2, 0, 5, 7, 8, 0, 0};
int low = 0;
int high = a.length - 1;
int temp = 0;
while (low < high){
if (a[low] == 0)
++low;
if (a[high] == 0){
temp = a[low];
a[low] = a[high];
a[high] = temp;
++low;
}
--high;
}
for (int i : a)
System.out.print(i + " ");
}
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。