【LeeCode】283题如何高效实现零的移动?
- 内容介绍
- 文章标签
- 相关推荐
本文共计504个文字,预计阅读时间需要3分钟。
【题目描述】给定一个数组 `nums`,编写一个函数,将所有0移动到数组的末尾,同时保持非零元素的相对顺序。注意,必须在原地修改数组,且不使用额外的数组。
【函数实现】pythondef move_zeros(nums): # 初始化指针 last_non_zero_found_at=0
# 遍历数组 for i in range(len(nums)): # 如果当前元素不是0 if nums[i] !=0: # 如果last_non_zero_found_at不是i,说明之前有0 if last_non_zero_found_at !=i: # 交换元素 nums[last_non_zero_found_at], nums[i]=nums[i], nums[last_non_zero_found_at] # 更新last_non_zero_found_at指针 last_non_zero_found_at +=1
示例nums=[0, 1, 0, 3, 12]move_zeros(nums)print(nums) # 输出应为 [1, 3, 12, 0, 0]
给定一个数组nums,编写一个函数将所有0移动到数组的末尾,同时保持非零元素的相对顺序。
请注意,必须在不复制数组的情况下原地对数组进行操作
leetcode.cn/problems/move-zeroes/description/?favorite=2cktkvj&orderBy=most_votes
admin
代码实现了基本要求,但LeeCode输出有问题
import java.util.*;// 2022-12-17class Solution { public void moveZeroes(int[] nums) { System.out.println("before: " + Arrays.toString(nums)); if (nums.length == 0) return; int[] res = new int[nums.length]; int index = 0; for (int i = 0; i < nums.length; i++){ if (nums[i] != 0){ res[index] = nums[i]; index++; } } nums = res; System.out.println("after: " + Arrays.toString(nums)); }}public class Main{ public static void main(String[] args) { int[] arr = {0,1,0,3,12}; // 输出: 1,3,12,0,0 int[] arr1 = {0}; // 输出: 0 new Solution().moveZeroes(arr); new Solution().moveZeroes(arr1); }}public void moveZeroes(int[] nums) { System.out.println("before: " + Arrays.toString(nums)); if (nums.length == 0) return; // 确保不为0的数字在nums中下标自增 int index = 0; for (int i = 0; i < nums.length; i++){ if (nums[i] != 0){ int tmp = nums[i]; nums[i] = nums[index]; nums[index] = tmp; index++; } } System.out.println("after: " + Arrays.toString(nums));}Jason365
解题思路
- 遍历数组,无为0的元素移动数组前方,用index下标记录。
- 遍历结束,对index值后的元素统一设为0
本文共计504个文字,预计阅读时间需要3分钟。
【题目描述】给定一个数组 `nums`,编写一个函数,将所有0移动到数组的末尾,同时保持非零元素的相对顺序。注意,必须在原地修改数组,且不使用额外的数组。
【函数实现】pythondef move_zeros(nums): # 初始化指针 last_non_zero_found_at=0
# 遍历数组 for i in range(len(nums)): # 如果当前元素不是0 if nums[i] !=0: # 如果last_non_zero_found_at不是i,说明之前有0 if last_non_zero_found_at !=i: # 交换元素 nums[last_non_zero_found_at], nums[i]=nums[i], nums[last_non_zero_found_at] # 更新last_non_zero_found_at指针 last_non_zero_found_at +=1
示例nums=[0, 1, 0, 3, 12]move_zeros(nums)print(nums) # 输出应为 [1, 3, 12, 0, 0]
给定一个数组nums,编写一个函数将所有0移动到数组的末尾,同时保持非零元素的相对顺序。
请注意,必须在不复制数组的情况下原地对数组进行操作
leetcode.cn/problems/move-zeroes/description/?favorite=2cktkvj&orderBy=most_votes
admin
代码实现了基本要求,但LeeCode输出有问题
import java.util.*;// 2022-12-17class Solution { public void moveZeroes(int[] nums) { System.out.println("before: " + Arrays.toString(nums)); if (nums.length == 0) return; int[] res = new int[nums.length]; int index = 0; for (int i = 0; i < nums.length; i++){ if (nums[i] != 0){ res[index] = nums[i]; index++; } } nums = res; System.out.println("after: " + Arrays.toString(nums)); }}public class Main{ public static void main(String[] args) { int[] arr = {0,1,0,3,12}; // 输出: 1,3,12,0,0 int[] arr1 = {0}; // 输出: 0 new Solution().moveZeroes(arr); new Solution().moveZeroes(arr1); }}public void moveZeroes(int[] nums) { System.out.println("before: " + Arrays.toString(nums)); if (nums.length == 0) return; // 确保不为0的数字在nums中下标自增 int index = 0; for (int i = 0; i < nums.length; i++){ if (nums[i] != 0){ int tmp = nums[i]; nums[i] = nums[index]; nums[index] = tmp; index++; } } System.out.println("after: " + Arrays.toString(nums));}Jason365
解题思路
- 遍历数组,无为0的元素移动数组前方,用index下标记录。
- 遍历结束,对index值后的元素统一设为0

