华为OD-D卷找座位

在一个大型体育场内举办了一场大型活动,由于疫情防控的需要,要求每位观众的必须间隔至少一个空位才允许落座。现在给出一排观众座位分布图,座位中存在已落座的观众,请计算出,在不移动现有观众座位的情况下,最多还能坐下多少名观众。

输入描述:

一个数组,用来标识某一排座位中,每个座位是否已经坐人。0表示该座位没有坐人,1表示该座位已经坐人。

输出描述:

整数,在不移动现有观众座位的情况下,最多还能坐下多少名观众。

备注:

1<=数组长度<=10000

题目解析:只要保证第i个位置的i-1和i+1都是空的就可以坐,只需要特殊处理首位和末尾就可以!

import java.util.*;

public class Main {
    public static void main(String[] args) {
//        int[] nums = new int[]{1, 0, 0, 0, 1};
        // 处理数据
        Scanner scanner = new Scanner(System.in);
        String string1 = scanner.next();
        int[] nums = new int[string1.length()];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = string1.charAt(i) - '0';
        }
        if (nums.length == 1) {
            if (nums[0] == 0) {
                System.out.println(1);
            } else {
                System.out.println(0);
            }
            return;
        }
        int result = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) {
                // 依次处理首位,中间,末尾,注意顺序
                if (i == 0 && nums[i + 1] == 0) {
                    nums[i] = 1;
                    result++;
                } else if (i > 0 && i < nums.length - 1 && nums[i + 1] == 0 && nums[i - 1] == 0) {
                    nums[i] = 1;
                    result++;
                } else if (i == nums.length - 1 && nums[i - 1] == 0) {
                    nums[i] = 1;
                    result++;
                }
            }
        }
        System.out.println(result);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值