From fc82d7eb10eb49689f2558ead63d743cc0e1dfa4 Mon Sep 17 00:00:00 2001 From: asahi Date: Tue, 28 Oct 2025 01:43:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=202025-10-28=E6=89=93=E5=8D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 122.best-time-to-buy-and-sell-stock-ii.java | 23 +++++++++++++ 134.gas-station.java | 36 +++++++++++++++++++++ 14.longest-common-prefix.java | 32 ++++++++++++++++++ 55.jump-game.java | 23 +++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 122.best-time-to-buy-and-sell-stock-ii.java create mode 100644 134.gas-station.java create mode 100644 14.longest-common-prefix.java create mode 100644 55.jump-game.java diff --git a/122.best-time-to-buy-and-sell-stock-ii.java b/122.best-time-to-buy-and-sell-stock-ii.java new file mode 100644 index 0000000..9b70071 --- /dev/null +++ b/122.best-time-to-buy-and-sell-stock-ii.java @@ -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 0) { + total += prices[i] - prices[i-1]; + } + } + return total; + } +} +// @lc code=end + diff --git a/134.gas-station.java b/134.gas-station.java new file mode 100644 index 0000000..c93d33d --- /dev/null +++ b/134.gas-station.java @@ -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 + diff --git a/14.longest-common-prefix.java b/14.longest-common-prefix.java new file mode 100644 index 0000000..cd68857 --- /dev/null +++ b/14.longest-common-prefix.java @@ -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 cur) { + return false; + } + if(nums[i]>0) { + cur = Math.max(cur, i+nums[i]); + } + } + return true; + } +} +// @lc code=end +