终于碰到一道水题,睡觉去~

Move Zeroes

Total Accepted: 37369 Total Submissions: 88383 Difficulty: Easy

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:

  1. You must do this in-place without making a copy of the array.
  2. 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的更多相关文章

  1. 【leetcode】Move Zeroes

    Move Zeroes 题目: Given an array nums, write a function to move all 0‘s to the end of it while maintai ...

  2. 【CF653G】Move by Prime 组合数

    [CF653G]Move by Prime 题意:给你一个长度为n的数列$a_i$,你可以进行任意次操作:将其中一个数乘上或者除以一个质数.使得最终所有数相同,并使得操作数尽可能小.现在我们想要知道$ ...

  3. 【leetcode❤python】Move Zeroes

    #-*- coding: UTF-8 -*- #filter()函数可以对序列做过滤处理,就是说可以使用一个自定的函数过滤一个序列,#把序列的每一项传到自定义的过滤函数里处理,并返回结果做过滤.最终一 ...

  4. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

  5. 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 ...

  6. 【leetcode】283. Move Zeroes

    problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...

  7. 【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) ...

  8. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  9. 【LeetCode】Set Matrix Zeroes 解题报告

    今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...

随机推荐

  1. 创建struct类型的数组

    在autoit中,如何创建类似这样的数组呢?如下方式,数组的element只是存储的地址相邻,所以我们可以这样做 $tagMYSTRUCT = "int code; char msg[10] ...

  2. 使用DotNetOpenAuth搭建OAuth2.0授权框架——Demo代码简单说明

    前段时间随意抽离了一部分代码作为OAuth2的示例代码,若干处会造成困扰,现说明如下: public class OAuthController : Controller { private stat ...

  3. jQuery ajax()使用serialize()提交form数据

    jQuery的serialize()方法通过序列化表单值,创建URL编码文本字符串,我们就可以选择一个或多个表单元素,也可以直接选择form将其序列化 <form action="&q ...

  4. ajax 提交數據

    $.ajax({ type: "get", async: false, url: "/book/UpdateSession", data: { }, dataT ...

  5. DescriptionResourcePathLocationType Dynamic Web Module 3.0 requires Java

    先保证ide的所有jdk都在1.6及以上,如果还是错就试试下面的 在<build></build>中添加 <plugins> <plugin> < ...

  6. Java项目导出war包 security alert:integrity check error”

    操作方法: 首先下载这个jar包 http://pan.baidu.com/s/1sk5uDzf 1.需要先把common/pluns 下的com.genuitec.eclipse.export.wi ...

  7. sed命令手册

    sed 是一种在线编辑器,它一次处理一行内容. 处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space). 接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内 ...

  8. 循序渐进之Spring AOP(6) - 使用@Aspect注解

    前面几节的示例看起来让人沮丧,要记忆如此多的接口.类和继承关系,做各种复杂的配置.好在这些只是一种相对过时的实现方式,现在只需要使用@Aspect注解及表达式就可以轻松的使用POJO来定义切面,设计精 ...

  9. flex中通过sprite在地图上画柱状图主要代码

    1.主要代码: var sprite:Sprite = new Sprite();     var columnSys:ColumnSymbol = new ColumnSymbol();     v ...

  10. JSF 抽象和实现例子 (函数和属性)

    ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/ ...