转载请注明出处:z_zhaojun的博客

原文地址:http://blog.csdn.net/u012975705/article/details/50493772

题目地址:https://leetcode.com/problems/move-zeroes/

Move Zeroes

  1. 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.
  2. For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
  3. Note:
  4. You must do this in-place without making a copy of the array.
  5. Minimize the total number of operations.

解法(java):

  1. public class Solution {
  2. public void moveZeroes(int[] nums) {
  3. if (nums != null) {
  4. int length = nums.length;
  5. for (int i = 0, j = 0; i < length; i++) {
  6. if (nums[i] != 0) {
  7. if (i != j) {
  8. nums[j] = nums[i];
  9. nums[i] = 0;
  10. }
  11. j++;
  12. }
  13. }
  14. }
  15. }
  16. }

leetcode:283. Move Zeroes(Java)解答的更多相关文章

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

  2. Java [Leetcode 283]Move Zeroes

    题目描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...

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

  4. [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 ...

  5. leetcode 283. Move Zeroes -easy

    题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...

  6. Leetcode 283 Move Zeroes python

    题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...

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

  8. LeetCode 283 Move Zeroes 解题报告

    题目要求 Given an array nums, write a function to move all 0's to the end of it while maintaining the re ...

  9. [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 ...

  10. Leetcode 283. Move Zeroes 移动数组中的零 (数组,模拟)

    题目描述 已知数组nums,写一个函数将nums中的0移动到数组后面,同时保持非零元素的相对位置不变.比如已知nums=[0,1,0,3,12],调用你写的函数后nums应该是[1,3,12,0,0] ...

随机推荐

  1. windows cmd 模仿电影黑客

    1.win+R 2.输入cmd 3.按F11进入全屏 4.color a 改变颜色为绿色(可能看起来秀一点) 5.dir/s 查看所有文件,就跑起来了,看起来很酷,但是在懂得人眼里,没什么的(所以只能 ...

  2. spring mvc poi excel

    Util类 package com.common.util; public class ExportUtil { private XSSFWorkbook wb = null; private XSS ...

  3. 任务备忘(已经完成):用python写一个格式化xml字符串的程序

    功能: 1.将xml中多余的空格,换行符去掉,让xml字符串变成一行. 2.将xml中添加缩进,使用print能正确打印添加缩进后的字符串. 思路: 采用正则表达式来判断xml中字符串的类型: 1.文 ...

  4. 11. GLOBAL_VARIABLES 与 SESSION_VARIABLES

    11. GLOBAL_VARIABLES 与 SESSION_VARIABLES 注意 从MySQL 5.7.6开始,show_compatibility_56系统变量的值会影响此处描述的表中的可用信 ...

  5. python 2018/8/25

    # 含多空格字符串的分割 hello = "hello python hello"print(a.split(" ")) # ['hello', 'python ...

  6. 爬虫app信息抓取之apk反编译抓取

    之前也抓过一些app,数据都比较好取,也没有研究的太深,毕竟还有android 模拟器+ appium 的方法.直到遇见了一款app ,具体名字就不说了,它安装在模拟器上竟然打不开 !!第一次遇见上网 ...

  7. 杭电 1213 How Many Tables (并查集求团体数)

    Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...

  8. LeetCode 309. Best Time to Buy and Sell Stock with Cooldown (stock problem)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. 初识Web框架

    一.Web框架本质 Python的Web框架分为两类: 通过socket自己写程序,自己处理请求: 基于Wsgi(Web Server Gateway Interface:Web服务网关接口),处理请 ...

  10. 大数据学习——点击流日志每天都10T,在业务应用服务器上,需要准实时上传至(Hadoop HDFS)上

    点击流日志每天都10T,在业务应用服务器上,需要准实时上传至(Hadoop HDFS)上 1需求说明 点击流日志每天都10T,在业务应用服务器上,需要准实时上传至(Hadoop HDFS)上 2需求分 ...