第一周刷题记录来了~
2022-1-1 Saturday Esay
一维数组变二维数组
System.arraycopy(original, i, ans[i / n], 0, n);
图形化一下就很好理解了~
2022-1-2 Sunday Medium
消除游戏,等差数列模拟
class Solution {
public int lastRemaining(int n) {
int head = 1;
int step = 1;
boolean left = true;
//int n = n;
while (n > 1) {
//从左边开始移除 or(从右边开始移除,数列总数为奇数)
if (left || n % 2 != 0) {
head += step;
}
step *= 2; //步长 * 2
left = !left; //取反移除方向
n /= 2; //总数 / 2
}
return head;
}
}
作者:xing-you-ji
链接:https://leetcode-cn.com/problems/elimination-game/solution/wo-hua-yi-bian-jiu-kan-dong-de-ti-jie-ni-k2uj/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
2022-1-3 Monday Easy
一周中的第几天:日期在 1971 到 2100 年之间,本质上还是计算天数;
class Solution {
public String dayOfTheWeek(int day, int month, int year) {
String[] week = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
int[] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};
/* 输入年份之前的年份的天数贡献 */
int days = 365 * (year - 1971) + (year - 1969) / 4;
/* 输入年份中,输入月份之前的月份的天数贡献 */
for (int i = 0; i < month - 1; ++i) {
days += monthDays[i];
}
if ((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) && month >= 3) {
days += 1;
}
/* 输入月份中的天数贡献 */
days += day;
return week[(days + 3) % 7];
}
}
// 作者:LeetCode-Solution
// 链接:https://leetcode-cn.com/problems/day-of-the-week/solution/yi-zhou-zhong-de-di-ji-tian-by-leetcode-w43iw/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
2022-1-4 Tuesday Hard
https://leetcode-cn.com/problems/cat-and-mouse/
猫和老鼠,一看 困难题,直接放弃抵抗,躺好;
博弈问题,最优策略
2022-1-5 Wednesday Easy
替换所有问好,这题目没啥意思;
2022-1-6 Thursday Medium
简化路径:栈 字符串
这道题理论上并不复杂,但是 Java SDK 中 对栈的使用很少,所以 API 不熟悉。
class Solution {
public String simplifyPath(String path) {
String[] names = path.split("/");
Deque<String> stack = new ArrayDeque<String>();
for (String name : names) {
if ("..".equals(name)) {
if (!stack.isEmpty()) {
stack.pollLast();
}
} else if (name.length() > 0 && !".".equals(name)) {
stack.offerLast(name);
}
}
StringBuffer ans = new StringBuffer();
if (stack.isEmpty()) {
ans.append('/');
} else {
while (!stack.isEmpty()) {
ans.append('/');
ans.append(stack.pollFirst());
}
}
return ans.toString();
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/simplify-path/solution/jian-hua-lu-jing-by-leetcode-solution-aucq/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
2022-1-7 Friday Easy
括号的最大嵌套深度,题目意思描述的太复杂,其实没太大意思~
2022-1-8 Saturday Medium
格雷码,完全没听说过
找到一个挺不错的网站:https://oi-wiki.org/misc/gray-code/
public _List
List
ret.add(0);
_for
_int _m = ret.size();
_for (_int _j = m - 1; j >= 0; j—) {
ret.add(ret.get(j) | (1 << (i - 1)));
}
}
_return _ret;
}
2022-1-9 Sunday Easy
按键持续时间最长的键
今天的题也没啥意思,主要是题目有坑~