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 maintaining the relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12]
, after calling your function, nums
should be [1, 3, 12, 0, 0]
.
题目大意:
给一个数组,将值为0的元素放置到最后,其他元素相对位置不变。
解题方法:
排序题,修改插入排序的判断条件就可以了
注意事项:
C++代码:
class Solution {
public:
void moveZeroes(vector<int>& nums) //仿插入排序
{
int j,tmp;
for(int p=;p<nums.size();p++)
{
tmp=nums[p];
for(j=p;j>&&tmp!=&&nums[j-]==;j--)
{
swap(nums[j],nums[j-]);
}
nums[j]=tmp;
}
}
};
283. Move Zeroes(C++)的更多相关文章
- leetcode:283. Move Zeroes(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...
- 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(移动全部的零元素)
翻译 给定一个数字数组.写一个方法将全部的"0"移动到数组尾部.同一时候保持其余非零元素的相对位置不变. 比如,给定nums = [0, 1, 0, 3, 12],在调用你的函数之 ...
- 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 ...
- 【leetcode】283. Move Zeroes
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
- 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 ...
- leetcode 283. Move Zeroes -easy
题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...
随机推荐
- 在Ubuntu 中安装eclipse, eclipse 文件已经下载好!
If you've downloaded Eclipse from their official website, follow these steps for the installation. E ...
- Java--创建线程及常用方法
继承java.lang.Thread类--Thread类代表线程类 它的常用方法如下: static Thread currentThread():返回当前正在运行的线程对象的引用. static ...
- 删除用不到的linux内核
方法1 sudo aptitude purge ~ilinux-image-.*\(\!`uname -r`\) 方法2 sudo apt-get autoremove 方法3 uname -a #使 ...
- 缓存算法–LRU
LRU LRU是Least Recently Used 的缩写,翻译过来就是“最近最少使用”,也就是说,LRU缓存把最近最少使用的数据移除,让给最新读取的数据.而往往最常读取的,也是读取次数最多的,所 ...
- Mysql分表和分区的区别
一,什么是mysql分表,分区 什么是分表,从表面意思上看呢,就是把一张表分成N多个小表,具体请看mysql分表的3种方法 什么是分区,分区呢就是把一张表的数据分成N多个区块,这些区块可以在同一个磁盘 ...
- 关于虚拟机VM
装Vmware虚拟机+安装系统不再困难 不要再以为你是女生,你就可以不用学装系统,亲,我们是计算机系的女生,顶起!!呼啦呼啦,这篇我装VM+系统的记录,作为勉励. 想必很多人都会使用到虚拟机,因为我们 ...
- 基于windows的ngnix基础使用
前言 今天组长一大早心血来潮的跟我说,我希望我们小组电脑做web站点的服务器集群,你搞一搞,就用ngnix吧. 君要臣死,臣不得不死.顺便写个文章做个笔记. 简介 Nginx 是一个高性能的HTTP和 ...
- [Redux] Filtering Redux State with React Router Params
We will learn how adding React Router shifts the balance of responsibilities, and how the components ...
- 非常全面的java基础笔试题
下面是java基础笔试题,当时我去笔试,做了1个小时(80道选择题,后面的简答题就没时间做了),结果很吓人,太挫了,最后被面试官忽悠去培训去了,呵呵.我偷偷把面试题弄了下来,用来学习吧,也希望能对你们 ...
- pugixml
http://www.firedragonpzy.com.cn/index.php/archives/3227 有关cocos2d-x的xml文件读取问题