【5_283】Move Zeroes
终于碰到一道水题,睡觉去~
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].
Note:
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
还有一点,我写代码的时候,用C和C++竟然很不熟悉,不如用Java方便。这说明急需复习!
Java:
public class Solution {
public void moveZeroes(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
for (int j = i + 1; j < nums.length; j++) {
if(nums[j] != 0) {
nums[i] = nums[j];
nums[j] = 0;
break;
}
}
}
}
}
}
【5_283】Move Zeroes的更多相关文章
- 【leetcode】Move Zeroes
Move Zeroes 题目: Given an array nums, write a function to move all 0‘s to the end of it while maintai ...
- 【CF653G】Move by Prime 组合数
[CF653G]Move by Prime 题意:给你一个长度为n的数列$a_i$,你可以进行任意次操作:将其中一个数乘上或者除以一个质数.使得最终所有数相同,并使得操作数尽可能小.现在我们想要知道$ ...
- 【leetcode❤python】Move Zeroes
#-*- coding: UTF-8 -*- #filter()函数可以对序列做过滤处理,就是说可以使用一个自定的函数过滤一个序列,#把序列的每一项传到自定义的过滤函数里处理,并返回结果做过滤.最终一 ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
- 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 ...
- 【leetcode】283. Move Zeroes
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
- 【POJ1568】【极大极小搜索+alpha-beta剪枝】Find the Winning Move
Description 4x4 tic-tac-toe is played on a board with four rows (numbered 0 to 3 from top to bottom) ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】Set Matrix Zeroes 解题报告
今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...
随机推荐
- 4,SFDC 管理员篇 - 数据模型 - 基本对象
Setup | Customize | Object Name | Filed 1, 标准字段定义 standard field:系统字段,不能删除,但是能在页面中remove non-requi ...
- .Net的Excel 导出 格式设置
添加引用:Microsoft Excel 11.0 Object Library ; 添加:using Microsoft.Office.Interop.Excel; 一.打开Exce ...
- 【转】提高VR渲染速度的最好方法(经典转载)
VR的基本渲染方法掌握起来并不难,但是最迫切需要解决的问题是VR的出图速度问题.动则需要数小时的渲染时间真的是很难以接受,我们从三个影响速度的参数结合网上一些高手的教程来分析一下. 一.Irradia ...
- jQuery ajax()使用serialize()提交form数据
jQuery的serialize()方法通过序列化表单值,创建URL编码文本字符串,我们就可以选择一个或多个表单元素,也可以直接选择form将其序列化 <form action="&q ...
- Ninject的使用
摘要 DI容器的一个责任是管理他创建的对象的生命周期.他应该决定什么时候创建一个给定类型的对象,什么时候使用已经存在的对象.他还需要在对象不需要的时候处理对象.Ninject在不同的情况下管理对象的生 ...
- python生成中文验证码,带旋转,带干扰噪音线段
# -*- coding: utf-8 -*- """ Created on Sun Oct 4 15:57:46 2015 @author: keithguofan & ...
- centos 常用命令
查看centos版本:cat /etc/redhat-release
- flex 导出Excel功能实现
方法一: 1.Excel导出主要代码: try { var bytes: ByteArray = new ByteArray(); bytes.writeMultiByte(DataG ...
- 理解node模块的exports和module.exports
exports是module.exports的引用,即var exports = module.exports.在一个模块的开头,这两个值都指向同一个空对象:exports = module.expo ...
- meta标签总结
1."format-detection" format-detection翻译成中文的意思是“格式检测”,顾名思义,它是用来检测html里的一些格式的, 那关于meta的forma ...