zoukankan      html  css  js  c++  java
  • 0283. Move Zeroes (E)

    Move Zeroes (E)

    题目

    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.

    Example:

    Input: [0,1,0,3,12]
    Output: [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.

    题意

    将数组中所有0放到最后,其余数按照原有顺序放到前面。

    思路

    见代码。


    代码实现

    Java

    class Solution {
        public void moveZeroes(int[] nums) {
            int p = 0, q = 0;
            while (q != nums.length) {
                if (nums[q] != 0) {
                    int tmp = nums[p];
                    nums[p++] = nums[q];
                    nums[q] = tmp;
                }
                q++;
            }
        }
    }
    
  • 相关阅读:
    HTML5表单元素的学习
    微博账号注册
    微博三方登陆流程
    Vue发送短信逻辑
    celery异步发送短信
    celery
    celery
    jwt安装配置与原理
    图片验证
    Vue组件
  • 原文地址:https://www.cnblogs.com/mapoos/p/13175064.html
Copyright © 2011-2022 走看看