zoukankan      html  css  js  c++  java
  • #Leetcode# 283. Move Zeroes

    https://leetcode.com/problems/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.

    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.

    代码:

    class Solution {
    public:
        void moveZeroes(vector<int>& nums) {
            int n = nums.size();
            for(int i = 0; i < n; i ++) {
                if(!nums[i]) {
                    int temp = i, temp2 = i;
                    for(int j = temp + 1; j < n; j ++) {
                        if(nums[j]) {
                            temp2 = j;
                            break;
                        }
                    }
                    swap(nums[temp], nums[temp2]);
                }
            }
        }
    };
    

      很纠结晚上要不要粗去走一走今天天气还不错

     

  • 相关阅读:
    Java面向对象知识点总结
    JAVA编程必学必会单词集(1)
    Linux 帮助命令
    学习笔记
    day4
    复习
    day5
    day04
    day3
    day02
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10291954.html
Copyright © 2011-2022 走看看