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 ...
随机推荐
- ***CSS总结-原创
CSS规则 第一个是:选择符.和jquery的选择器是一个意思大括号是:申明块里面KV是:属性和属性值分号是结束符 类别选择符(器) p.red的意思,就是对p标签中类为red的进行CSS作用 ID选 ...
- freemarker导出word的一些问题
首先,了解下freemarker导出word的流程: 参考https://www.cnblogs.com/llfy/p/9303208.html 异常一: freemarker.core.ParseE ...
- firemonkey获取当前文件所在路径的方法
在之前,我们知道有三种方法: ExtractFilePath(ParamStr(0)) ExtractFilePath(Application.ExeName) GetCurrentDir + '\' ...
- 使用shell分页读取600万+的MySQL数据脚本
shell-mysql 脚本背景 因为要在Linux上.远程读取mysql的表的数据,然后做一定清洗后.把数据上传至Hadoop集群中,使用Java写吧,感觉太麻烦了.得在Win上开发好,还得打成ja ...
- git基础之创建ssh公钥和密钥
用git管理过程中,不想每次输入username和password.解决的方法例如以下; 1 . 链接换成ssh协议. 2 . 生成公钥. 3. 加入ssh公钥到gitserver. 打开gitbas ...
- POJ 1159 Palindrome(字符串变回文:LCS)
POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...
- Android自己定义组件系列【5】——进阶实践(2)
上一篇<Android自己定义组件系列[5]--进阶实践(1)>中对任老师的<可下拉的PinnedHeaderExpandableListView的实现>前一部分进行了实现,这 ...
- 一张图告诉你是须要 SQL 还是 Hadoop
译序 非常多朋友问时下如火如荼的 Hadoop 是否适合引进我们自己的项目,什么时候用 SQL.什么时候用 Hadoop,它们之间怎样取舍?Aaron Cordova 用一张图来回答你这个问题,对于不 ...
- 2016/1/27 1, File 创建 删除 改名 换路径 2,输出流 不覆盖 换行输入 3,输入流
1, File 创建 删除 改名 换路径 package Stream; import java.io.File; import java.io.IOException; public cla ...
- iOS开发——高级篇——Runtime实际应用
前言 本篇主要介绍Runtime在开发中的一些使用场景,顺便讲解了下MJExtension的底层实现 一.runtime简介 RunTime简称运行时.OC就是运行时机制,也就是在运行时候的一些机制, ...