feat: 2025-10-28打卡

This commit is contained in:
asahi
2025-10-28 01:43:48 +08:00
parent 3c203342a5
commit fc82d7eb10
4 changed files with 114 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
/*
* @lc app=leetcode id=122 lang=java
*
* [122] Best Time to Buy and Sell Stock II
*/
// @lc code=start
class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length <= 1) {
return 0;
}
int total = 0;
for(int i = 1; i<prices.length; i++) {
if(prices[i] - prices[i-1] > 0) {
total += prices[i] - prices[i-1];
}
}
return total;
}
}
// @lc code=end

36
134.gas-station.java Normal file
View File

@@ -0,0 +1,36 @@
/*
* @lc app=leetcode id=134 lang=java
*
* [134] Gas Station
*/
// @lc code=start
class Solution {
/**
* 本题核心为若从i开始计算到j时发现不行那么从(i, j]范围内任一站点开始也不行,易进行论证
*
* @param gas
* @param cost
* @return
*/
public int canCompleteCircuit(int[] gas, int[] cost) {
int start = 0, sum = 0, curIdx = 0;
while(start < gas.length) {
if(curIdx - start == gas.length) {
return start;
}
int pos = curIdx % gas.length;
if(sum + gas[pos] - cost[pos] < 0) {
curIdx++;
start = curIdx;
sum = 0;
} else {
sum += gas[pos] - cost[pos];
curIdx++;
}
}
return -1;
}
}
// @lc code=end

View File

@@ -0,0 +1,32 @@
/*
* @lc app=leetcode id=14 lang=java
*
* [14] Longest Common Prefix
*/
// @lc code=start
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0) {
return "";
}
String r = strs[0];
for(int i=1; i<strs.length;i++) {
r = longestCommonPrefix(r, strs[i]);
}
return r;
}
private String longestCommonPrefix(String a, String b) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i<a.length() && i<b.length();i++) {
if(a.charAt(i) != b.charAt(i)) {
break;
}
sb.append(a.charAt(i));
}
return sb.toString();
}
}
// @lc code=end

23
55.jump-game.java Normal file
View File

@@ -0,0 +1,23 @@
/*
* @lc app=leetcode id=55 lang=java
*
* [55] Jump Game
*/
// @lc code=start
class Solution {
public boolean canJump(int[] nums) {
int cur = 0;
for(int i=0;i<nums.length;i++) {
if(i > cur) {
return false;
}
if(nums[i]>0) {
cur = Math.max(cur, i+nums[i]);
}
}
return true;
}
}
// @lc code=end