283.移动零

283.移动零

题目分析

  • 不能考虑遇到0直接和数组末尾交换,这样会破环相对顺序
  • 反过来想,把0移动到数组末 == 把非0移动到前面
  • 一趟扫描,遇到0就跳过,遇到非0就把它挪到前面去
  • cur记录着可以挪动到的位置

注意当nums[i] != 0,且curi指向同位置,即非0数看作已经放好位置了

在这里插入图片描述

Solution

    public void moveZeroes(int[] nums) {
        if (nums == null) return;
        for (int i = 0, cur = 0; i < nums.length; i++) {
            if (nums[i] == 0) continue;
            if (cur != i) {
                nums[cur] = nums[i];
                nums[i] = 0;
            }
            cur++;
        }
    }

题目变式-PolishFlag

Given an array A[1…N] with elements Red and White, rearrange the order by swapping alone so that all Whites come before all Reds

def polishflag(flag):
    # Precondition: flag is a list containing only 0’s and 1’s
    n = len(flag)
    i, j = 0, 1
    while (j < n):
        # Loop invariant: no indices below i are 0’s,
        # no indices between i+1 and j-1 are 1’s
        while (i < n) and flag[i] == 1:
            i += 1
            j = i+1
        while (j < n) and flag[j] == 0:
            j += 1
        if (j < n):
            flag[i], flag[j] = flag[j], flag[i]
    return flag
# Postcondition: i = number of 1’s
# no indices below i are 0’s,
# no indices between i+1 and n-1 are 1’s

Reference:小码哥MJ

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值