当前位置:网站首页>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;
}
边栏推荐
- Packages - Notes
- 使用 TiUP 命令管理组件
- 【HMS core】【FAQ】push kit、AR Engine、广告服务、扫描服务典型问题合集2
- Mysql database query is very slow. Besides the index, what else can be caused?
- HTTP缓存小结
- How do luxury giants such as GUCCI and LV deploy the metaverse, should other brands keep up?
- AI遮天传 DL-CNN
- How to split microservices?
- timed task corn
- 二、判断 & 循环
猜你喜欢

Huawei ADS reports an error when obtaining conversion tracking parameters: getInstallReferrer IOException: getInstallReferrer not found installreferrer

解析字符串拼接的两种情况

arcpy使用教程

Local Transactions vs Distributed Transactions
![[flutter]什么是MaterialApp和Material design](/img/72/d0845467b33b2291f47e7f54171088.jpg)
[flutter]什么是MaterialApp和Material design

FME实现CAD带属性转SHP数据方法

php如何查询字符串出现位置

Promise Notes (1)

FME realizes the method of converting CAD with attribute to SHP data

Golang分布式应用定时任务如何实现
随机推荐
华为「天才少年」计划招募的博士们,迎来首秀!
【HMS core】【FAQ】Account、IAP、Location Kit and HarmonyOS典型问题合集1
TensorFlow custom training function
Applicable scenarios of TiDB tools
转换OTU表和序列文件为PICRUST2需要的格式
tiup install
数组元素逆置
AI遮天传 DL-CNN
ECCV2022 | FPN错位对齐,实现高效半监督目标检测 (PseCo)
Debug - Notes
L2-007 家庭房产(vector、set、map的使用)
为什么数据需要序列化
FME realizes the method of converting CAD with attribute to SHP data
yarn的安装及使用教程
php如何查询字符串出现位置
tiup install
websocket flv 客户端解封包
难道Redis真的变慢了吗?
tiup completion
Golang分布式应用之Redis怎么使用