LeetCode刷题记---1518/1646/1736

(该模块仅为记录本人的leetcode的练习记录)

1518.换酒问题

在这里插入图片描述
解题思路:用变量emptyBottols记录空瓶数量,ans记录总共喝的酒数量,模拟喝酒的过程即可,当空瓶数emptyBottols小于可以换取的酒数numExchange时,结束模拟,得出答案。

代码:

package LeetCode.Code2022.January;

public class Solution1518 {
    public int numWaterBottles(int numBottles, int numExchange) {

        int ans = numBottles;
        int emptyBottles = numBottles;
        numBottles = 0;
        while (emptyBottles >= numExchange) {
            numBottles = emptyBottles / numExchange;    //空瓶换酒
            emptyBottles = emptyBottles - numBottles * numExchange + numBottles; //喝完的空瓶数
            ans += numBottles;  //总共和的酒
            numBottles = 0;


        }

        return ans;

    }
}

1646.获取生成数组中的最大值

在这里插入图片描述
解题思路:直接更具题目意思模拟

代码:

package LeetCode.Code2022.January;

public class Solution1646 {
    public int getMaximumGenerated(int n) {

        int[] nums = new int[n + 1];
        int max = 0;

        for (int i = 0; i < nums.length; i++) {
            if (i < 2) {
                nums[i] = i;
                if (max < nums[i]) {
                    max = nums[i];
                }
            }
            if (2 <= i * 2 && i * 2 <= n) {
                nums[2 * i] = nums[i];
                if (max < nums[i * 2]) {
                    max = nums[i * 2];
                }
            }
            if (2 <= 2 * i + 1 && 2 * i + 1 <= n) {
                nums[2 * i + 1] = nums[i] + nums[i + 1];
                if (max < nums[2 * i + 1]) {
                    max = nums[2 * i + 1];
                }
            }
        }
        return max;

    }
}

1720.编码异或后的数组

在这里插入图片描述

解题思路:直接根据题意进行逆运算即可。

代码:

package LeetCode.Code2022.January;

public class Solution1720 {
    public int[] decode(int[] encoded, int first) {

        int n = encoded.length + 1;
        int[] arr = new int[n];
        arr[0] = first;
        for (int i = 0; i < n - 1; i++) {
            arr[i + 1] = arr[i] ^ encoded[i];
        }
        return arr;

    }
}

1736.替换隐藏数字得到的最晚时间

在这里插入图片描述
解题思路:考虑几个特殊情况就没了

代码:

package LeetCode.Code2022.January;

public class Solution1736 {
    public String maximumTime(String time) {

        StringBuilder ans = new StringBuilder(time);
        if (ans.charAt(0) == '?') {
            if (ans.charAt(1) < '4' || !Character.isDigit(ans.charAt(1)))
                ans.setCharAt(0, '2');
            else
                ans.setCharAt(0, '1');
        }
        if (ans.charAt(1) == '?') {
            if (ans.charAt(0) != '2') {
                ans.setCharAt(1, '9');
            } else {
                ans.setCharAt(1, '3');
            }
        }
        if (ans.charAt(3) == '?') {
            ans.setCharAt(3, '5');
        }
        if (ans.charAt(4) == '?') {
            ans.setCharAt(4, '9');
        }
        return ans.toString();


    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值