283 Move Zeroes 移动零
给定一个数组 nums, 编写一个函数将所有 0 移动到它的末尾,同时保持非零元素的相对顺序。
例如, 定义 nums = [0, 1, 0, 3, 12],调用函数之后, nums 应为 [1, 3, 12, 0, 0]。
注意事项:
必须在原数组上操作,不要为一个新数组分配额外空间。
尽量减少操作总数。
详见:https://leetcode.com/problems/move-zeroes/description/
Java实现:
class Solution {
public void moveZeroes(int[] nums) {
for(int i=0,j=0;i<nums.length;++i){
if(nums[i]!=0){
swap(nums,i,j++);
}
}
}
private void swap(int[] nums,int i,int j){
int tmp=nums[i];
nums[i]=nums[j];
nums[j]=tmp;
}
}
C++实现:
class Solution {
public:
void moveZeroes(vector<int>& nums) {
for(int i=0,j=0;i<nums.size();++i)
{
if(nums[i])
{
swap(nums[i],nums[j++]);
}
}
}
};
参考:https://www.cnblogs.com/grandyang/p/4822732.html
283 Move Zeroes 移动零的更多相关文章
- [LeetCode] 283. Move Zeroes 移动零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- [leetcode]283. Move Zeroes移零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- 283. Move Zeroes把零放在最后面
[抄题]: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...
- 【leetcode】283. Move Zeroes
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
- 283. Move Zeroes(C++)
283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mainta ...
- LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List
283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...
- 283. Move Zeroes【easy】
283. Move Zeroes[easy] Given an array nums, write a function to move all 0's to the end of it while ...
- LN : leetcode 283 Move Zeroes
lc 283 Move Zeroes 283 Move Zeroes Given an array nums, write a function to move all 0's to the end ...
- 283. Move Zeroes - LeetCode
Question 283. Move Zeroes Solution 题目大意:将0移到最后 思路: 1. 数组复制 2. 不用数组复制 Java实现: 数组复制 public void moveZe ...
随机推荐
- Balance POJ - 1837 地推
Gigel has a strange "balance" and he wants to poise it. Actually, the device is different ...
- 微信浏览器video
<style> /* 解决上下有黑边,不能全屏 */ video{object-fit: fill;} </style> <video id="videoID& ...
- Kerberos认证浅析
1 引言 在希腊神话中Kerberos是守护地狱之门的一条凶猛的三头神犬,而我们在本文中所要介绍的Kerberos认证协议是由美国麻省理工学院(MIT)首先提出并实现的,是该校雅典娜计划的一部分.这个 ...
- MongoDB小结07 - update【$addToSet & $each】
用$addToSet更新可以避免重复,将它与$each组合起来,可以一次性添加多条(就算后添加的值已存在也没有关系) db.user.update({"name":"co ...
- openWrt 安装管理界面luci中文包
openWrt15安装管理界面luci中文包 如果刚刷的openwrt15没有中文界面,用ssh连接路由后用opkg安装 root@bang-bang-tang:~# opkg insta ...
- Django学习系列之django restframework
曾几何时,Ajax已经统治了Web开发中的客户端,而REST成为web世界中最流行的架构风格(architecture style).所以我们的选择变得很简单:前端ajax访问后端的RESTful A ...
- IntelliJ IDEA在行尾增加分号
IntelliJ IDEA在行尾增加分号 Ctrl+Shift+Enter - 本身的含义是自动完成,如果需要的话,会在行尾添加分号:
- java设计模式 -------- 行为模式 之 策略模式(4)
[本文是自己学习所做笔记.欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020] 上面3节实现了从最初的对整形数组排序到最后能够对全部类型都能够依据须要定义自 ...
- 制作NGUI动态字体
在ngui中有两种制做字体的方式.一种是bmfont等工具制作字体图集的方法,这样的方法呢是动态的.生成的图集有多个字就是多少个字,要多加一 个字要又一次用工具做一次,非常是麻烦. 而汉字有太多,我们 ...
- Brackets常用插件
Emmet插件:https://github.com/emmetio/brackets-emmet AngularJS插件:https://github.com/angular-ui/AngularJ ...