(该模块仅为记录本人的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();
}
}