当前位置:网站首页>283.移动零
283.移动零
2022-06-24 09:47:00 【兀坐晴窗独饮茶】
主体思路 :
- 本质上和删除有序数组重复项类似, 都是满足 xxx 条件, 然后 执行 xxx 操作
- 删除有序数组重复项, 满足条件的直接覆盖即可, 因为是删除
- 而移动零, 并不是覆盖, 而是交换
- 所以 : 思路很简单 定义一个 count 用于记录零元素位置, 然后遍历元素, 当遇到非零元素就和零元素交换即可
细节问题 :
class Solution {
public void moveZeroes(int[] nums) {
int count = 0 ;
for (int i = 0 ; i < nums.length; i++) {
if (nums[i] != 0) {
int tmp = nums[count];
nums[count] = nums[i];
nums[i] = tmp;
count++;
}
}
}
}
- 本题中并无边界问题, 无论是
count
还是i
都是满足数组边界条件的
边栏推荐
- 利用pandas读取SQL Sever数据表
- Juul, the American e-cigarette giant, suffered a disaster, and all products were forced off the shelves
- Flink集群搭建以及企业级yarn集群搭建
- Leetcode-1089: replication zero
- 为什么 JSX 语法这么香?
- A method to solve the self-adaptive width and height of the internal picture of rich text label in wechat applet
- Engine localization adaptation & Reconstruction notes
- 2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。
- tf. errors
- Six states of threads
猜你喜欢
解决Deprecated: Methods with the same name as their class will not be constructors in报错方案
2022年智能机器人与系统国际研讨会(ISoIRS 2022)
线程池的执行流程
【数据分析数据源】全国各省市行政区坐标(包含边界坐标点和中心坐标点)
分布式事务原理以及解决分布式事务方案
Customize the toolbars of the kindeditor editor. Items removes unnecessary toolbars or retains some toolbars
抓包工具charles实践分享
上升的气泡canvas破碎动画js特效
How can I solve the problem that the swiper animation animation fails when switching between left and right rotations of the swiper?
Machine learning - principal component analysis (PCA)
随机推荐
学习使用phpstripslashe函数去除反斜杠
Leetcode interview question 01.05: primary editing
用扫描的方法分发书稿校样
线程的 sleep() 方法与 wait() 方法的区别
【资源分享】2022年第五届土木,建筑与环境工程国际会议(ICCAEE 2022)
被困英西中学的师生安全和食物有保障
tf. errors
6. package management business development
包装类型与基本类型的区别
百度网盘下载一直请求中问题解决
The great charm of cookies
分布式系统你必须了解的点-CAP
SQL Server AVG function rounding
Normal equation
Flink集群搭建以及企业级yarn集群搭建
How to use multiple kindeditor editors on a page and pass values to the server
静态链接库和动态链接库的区别
numpy. linspace()
web网站开发,图片懒加载
leetCode-498: 對角線遍曆