【LeetCode】283. Move Zeros

题目

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:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.

问题陈述:

把给定数组中的0全部挪到后边,保持其余数字顺序不变

题目思路:

遍历,把非零元素从前向后赋值给原数组,剩余空位补0。

代码:

class Solution {

public:

void moveZeroes(vector<int>& nums) {

int len = nums.size();

int j = 0;//赋值指针

for (int i = 0; i < len; i++) {

if (nums[i] != 0) {

nums[j] = nums[i];

j++;

}

else continue;

}

for (; j < len; j++) {//将后面剩余的空位用0补齐

nums[j] = 0;

}

}

};

结果

原文:大专栏  【LeetCode】283. Move Zeros

原文地址:https://www.cnblogs.com/petewell/p/11601691.html

时间: 2024-10-12 17:32:23

【LeetCode】283. Move Zeros的相关文章

【leetcode】Set Matrix Zeros

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up:Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O(

【leetcode】283.moveZeroes

题目描述 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. https://leetcode-cn.com/problems/move-zeroes/ 解法一 时间复杂度:O(n) 空间复杂度:O(1) 思路:将非零值覆盖数组前方,尾部赋为零值 void moveZeroes(vector<int>& nums) { int j = 0; for (int i = 0; i < nums.size(); i ++) { if (num

【leetcode】1089. Duplicate Zeros

题目如下: Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right. Note that elements beyond the length of the original array are not written. Do the above modifications to the input arr

【Leetcode】Unique Paths

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in t

【Leetcode】Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 思路:简单的动态规划题目,设f(m, n)为从(0, 0)到达(m

【LeetCode】Triangle

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i

【leetcode】657. Robot Return to Origin

Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin/ 1)problem There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0,

【leetcode】893. Groups of Special-Equivalent Strings

Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-of-special-equivalent-strings/ 1)problem You are given an array A of strings. Two strings S and T are special-equivalent if after any number of moves,

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"