返回介绍

solution / 0200-0299 / 0283.Move Zeroes / README_EN

发布于 2024-06-17 01:04:02 字数 4235 浏览 0 评论 0 收藏 0

283. Move Zeroes

中文文档

Description

Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Note that you must do this in-place without making a copy of the array.

 

Example 1:

Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]

Example 2:

Input: nums = [0]
Output: [0]

 

Constraints:

  • 1 <= nums.length <= 104
  • -231 <= nums[i] <= 231 - 1

 

Follow up: Could you minimize the total number of operations done?

Solutions

Solution 1: Two Pointers

We use two pointers $i$ and $j$, where pointer $i$ points to the end of the sequence that has been processed, and pointer $j$ points to the head of the sequence to be processed. Initially, $i=-1$.

Next, we traverse $j \in [0,n)$, if $nums[j] \neq 0$, then we swap the next number pointed by pointer $i$ with $nums[j]$, and move $i$ forward. Continue to traverse until $j$ reaches the end of the array, all non-zero elements of the array are moved to the front of the array in the original order, and all zero elements are moved to the end of the array.

The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.

class Solution:
  def moveZeroes(self, nums: List[int]) -> None:
    i = -1
    for j, x in enumerate(nums):
      if x:
        i += 1
        nums[i], nums[j] = nums[j], nums[i]
class Solution {
  public void moveZeroes(int[] nums) {
    int i = -1, n = nums.length;
    for (int j = 0; j < n; ++j) {
      if (nums[j] != 0) {
        int t = nums[++i];
        nums[i] = nums[j];
        nums[j] = t;
      }
    }
  }
}
class Solution {
public:
  void moveZeroes(vector<int>& nums) {
    int i = -1, n = nums.size();
    for (int j = 0; j < n; ++j) {
      if (nums[j]) {
        swap(nums[++i], nums[j]);
      }
    }
  }
};
func moveZeroes(nums []int) {
  i := -1
  for j, x := range nums {
    if x != 0 {
      i++
      nums[i], nums[j] = nums[j], nums[i]
    }
  }
}
/**
 Do not return anything, modify nums in-place instead.
 */
function moveZeroes(nums: number[]): void {
  const n = nums.length;
  let i = 0;
  for (let j = 0; j < n; j++) {
    if (nums[j]) {
      if (j > i) {
        [nums[i], nums[j]] = [nums[j], 0];
      }
      i++;
    }
  }
}
impl Solution {
  pub fn move_zeroes(nums: &mut Vec<i32>) {
    let mut i = 0;
    for j in 0..nums.len() {
      if nums[j] != 0 {
        if j > i {
          nums[i] = nums[j];
          nums[j] = 0;
        }
        i += 1;
      }
    }
  }
}
/**
 * @param {number[]} nums
 * @return {void} Do not return anything, modify nums in-place instead.
 */
var moveZeroes = function (nums) {
  let i = -1;
  for (let j = 0; j < nums.length; ++j) {
    if (nums[j]) {
      const t = nums[++i];
      nums[i] = nums[j];
      nums[j] = t;
    }
  }
};
void moveZeroes(int* nums, int numsSize) {
  int i = 0;
  for (int j = 0; j < numsSize; j++) {
    if (nums[j] != 0) {
      if (j > i) {
        nums[i] = nums[j];
        nums[j] = 0;
      }
      i++;
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文