当前位置:网站首页>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;
}
边栏推荐
- tiup env
- 涨姿势了!原来这才是多线程正确实现方式
- 数组元素逆置
- FME realizes the method of converting CAD with attribute to SHP data
- 被捧上天的Scrum敏捷管理为何不受大厂欢迎了?
- php如何查询字符串出现位置
- When the vite multi-page application refreshes the page, it will not be in the current route and will return to the root route
- tiup completion
- Troubleshooting TiUP
- STM32F407定时器输入捕获
猜你喜欢

Flask之路由(app.route)详解

动态规划 --- 状态压缩DP 详细解释

经典实例分割模型Mask RCNN原理与测试

ECCV2022 | FPN错位对齐,实现高效半监督目标检测 (PseCo)

(Crypto essential dry goods) Detailed analysis of the current NFT trading markets

应用接入华为分析在应用调试模式下为何没有数据上报?

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

nodejs environment variable settings

华为「天才少年」计划招募的博士们,迎来首秀!

CAD几个优化设置
随机推荐
Store Limit usage documentation
类和对象(下篇)
【喂到嘴边了的模块】准备徒手撸GUI?用Arm-2D三分钟就够了
代码随想录笔记_哈希_1l两数之和
TiUP 故障排查
tiup install
How do luxury giants such as GUCCI and LV deploy the metaverse, should other brands keep up?
CAD几个优化设置
Database - SQL
动态规划 --- 状态压缩DP 详细解释
STM32F407定时器输入捕获
tiup help
让人上瘾的新一代开发神器,彻底告别Controller、Service、Dao等方法
RISC-V调用惯例
RobotStudio实现喷漆、打磨等功能(曲面路径生成与仿真)
[Cloud native] Grayscale release, blue-green release, rolling release, grayscale release explanation
Placement Rules 使用文档
Core Topics under Microservice Architecture (2): Design Principles and Core Topics of Microservice Architecture
QIIME2得到PICRUSt2结果后如何分析
TiUP 术语及核心概念