当前位置:网站首页>LeetCode-283-移动零
LeetCode-283-移动零
2022-07-30 15:24:00 【z754916067】
题目

思路
- 双指针
- 一个指向为0的 一个指向不为0的 交换即可
代码
public void moveZeroes(int[] nums){
if(nums.length==1) return;
//双指针 start指向为0的数 end指向不为0的数 两者交换即可
int start = 0;
int end = 0;
while (start<nums.length){
//start往后移 找到nums数组里为0的数 找到最后一个就停下来
while(start<nums.length && nums[start]!=0) start++;
while(end<nums.length && nums[end]==0) end++;
if(end>=nums.length || start>=nums.length) break;
if(start<end) swap(nums,start,end);
end++;
}
}
public void swap(int[]nums,int index1,int index2){
int temp = nums[index1];
nums[index1] = nums[index2];
nums[index2] = temp;
}
边栏推荐
猜你喜欢
随机推荐
Introduction to golang image processing library image
TiUP terms and core concepts
When the vite multi-page application refreshes the page, it will not be in the current route and will return to the root route
Use of InputStream and OutputStream
类和对象(下篇)
Placement Rules 使用文档
JHM:芳环羟化双加氧酶数据库DARHD建立及相关引物评价
Promise笔记(一)
php字符串如何去除第一个字符
武汉星起航:海外仓基础建设成为跨境电商企业的一大出海利器
使用 TiUP 命令管理组件
xxl-job源码解析(技术分享)
TiUP 简介
php如何查询字符串出现位置
nodejs environment variable settings
Example of video switching playback (video switching example) code
TiDB tool download
ECCV2022 | FPN错位对齐,实现高效半监督目标检测 (PseCo)
Data Analysis Tools - DDL operations & DML operations in HQL
Store Limit usage documentation









