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 ...
随机推荐
- Core java for impatient 笔记
类比c++来学习! 1.在java 中变量不持有对象,变量持有的是对象的引用,可以把变量看做c++中的只能指针,自动管理内存 需要手动初始化(否则就是空指针!) 2.final 相当于c++中的con ...
- 携程Apollo(阿波罗)配置中心在Spring Boot项目快速集成
前提:先搭建好本地的单机运行项目:http://www.cnblogs.com/EasonJim/p/7643630.html 说明:下面的示例是基于Spring Boot搭建的,对于Spring项目 ...
- Memcached的几种Java客户端(待实践)
其实现在来尝试Memcached的客户端估计会有点过气,因为现在大势基本都在Redis那边. Memcached Client目前有3种: Memcached Client for Java(已经停止 ...
- test markdown 写博客
欢迎使用 Cmd Markdown 编辑阅读器 我们理解您需要更便捷更高效的工具记录思想,整理笔记.知识,并将其中承载的价值传播给他人,Cmd Markdown 是我们给出的答案 -- 我们为记录思想 ...
- ppc_85xx-gcc -shared -fPIC liberr.c -o liberr.so
fPIC作用于编译阶段,告诉编译器产生与位置无关代码(Position-Independent Code), 则产生的代码中,没有绝对地址,所有使用相对地址.故而代码能够被载入器载入到内存的随意 ...
- gcc 5.2.0 编译安装笔记-20151110
**转载请注明出处** by.haunying3 系统版本号 CentOS-6.6-x86_64-minimal 编译器 gcc-4.4.7通过yum安装 rpm -qa | grep gcc gcc ...
- 小白学开发(iOS)OC_ 字符串的获取 (2015-08-11)
// // main.m // 字符串的获取 // // Created by admin on 15/8/13. // Copyright (c) 2015年 admin. All righ ...
- 自己动手写Android数据库框架
前言 相信不少开发人员跟我一样,每次都非常烦恼自己写数据库,并且那些数据库语句也经常记不住.当然网上也有非常多非常好的数据库框架,你能够直接拿来用,可是 非常多时候我们的项目.特别是一个小型的Andr ...
- 1064 - You have an error in your SQL syntax问题解决
新建表sql语句例如以下,在navicat for mysql 中执行,报错. CREATE TABLE `t_article`( `bh` bigint(20) NOT NULL PRIMARY K ...
- __init__、__str__、__del__方法
1.def__init__()方法: class Cat: """定义了一个Cat类""" #初始化对象 def __init__(self ...