From 3650ccd44b758eaf21727af732af123734f865e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Sun, 31 Aug 2025 22:29:14 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E5=88=86=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/SolutionBestTimeToBuyAndSellStock.java | 56 ++++++++++++++ .../SolutionBestTimeToBuyAndSellStockIi.java | 51 +++++++++++++ .../editor/cn/SolutionMajorityElement.java | 49 +++++++++++++ .../editor/cn/SolutionRotateArray.java | 73 +++++++++++++++++++ .../SolutionBestTimeToBuyAndSellStock.md | 35 +++++++++ .../SolutionBestTimeToBuyAndSellStockIi.md | 42 +++++++++++ .../cn/doc/content/SolutionMajorityElement.md | 33 +++++++++ .../cn/doc/content/SolutionRomanToInteger.md | 73 +++++++++++++++++++ .../cn/doc/content/SolutionRotateArray.md | 44 +++++++++++ 9 files changed, 456 insertions(+) create mode 100644 src/leetcode/editor/cn/SolutionBestTimeToBuyAndSellStock.java create mode 100644 src/leetcode/editor/cn/SolutionBestTimeToBuyAndSellStockIi.java create mode 100644 src/leetcode/editor/cn/SolutionMajorityElement.java create mode 100644 src/leetcode/editor/cn/SolutionRotateArray.java create mode 100644 src/leetcode/editor/cn/doc/content/SolutionBestTimeToBuyAndSellStock.md create mode 100644 src/leetcode/editor/cn/doc/content/SolutionBestTimeToBuyAndSellStockIi.md create mode 100644 src/leetcode/editor/cn/doc/content/SolutionMajorityElement.md create mode 100644 src/leetcode/editor/cn/doc/content/SolutionRomanToInteger.md create mode 100644 src/leetcode/editor/cn/doc/content/SolutionRotateArray.md diff --git a/src/leetcode/editor/cn/SolutionBestTimeToBuyAndSellStock.java b/src/leetcode/editor/cn/SolutionBestTimeToBuyAndSellStock.java new file mode 100644 index 0000000..062027f --- /dev/null +++ b/src/leetcode/editor/cn/SolutionBestTimeToBuyAndSellStock.java @@ -0,0 +1,56 @@ +//给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。 +// +// 你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。 +// +// 返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。 +// +// +// +// 示例 1: +// +// +//输入:[7,1,5,3,6,4] +//输出:5 +//解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。 +// 注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。 +// +// +// 示例 2: +// +// +//输入:prices = [7,6,4,3,1] +//输出:0 +//解释:在这种情况下, 没有交易完成, 所以最大利润为 0。 +// +// +// +// +// 提示: +// +// +// 1 <= prices.length <= 10⁵ +// 0 <= prices[i] <= 10⁴ +// +// +// Related Topics 数组 动态规划 👍 3876 👎 0 + + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int maxProfit(int[] prices) { + int minPrice = Integer.MAX_VALUE; + int maxProfit = 0; + for (int price : prices) { + minPrice = Math.min(minPrice, price); + maxProfit = Math.max(maxProfit, price - minPrice); + } + return maxProfit; + } +} + +//leetcode submit region end(Prohibit modification and deletion) +void main() { + System.out.println(new Solution().maxProfit(new int[]{7, 1, 5, 3, 6, 4})); + System.out.println(new Solution().maxProfit(new int[]{7,6,4,3,1})); + System.out.println(new Solution().maxProfit(new int[]{3,2,6,5,0,3})); +} \ No newline at end of file diff --git a/src/leetcode/editor/cn/SolutionBestTimeToBuyAndSellStockIi.java b/src/leetcode/editor/cn/SolutionBestTimeToBuyAndSellStockIi.java new file mode 100644 index 0000000..68ca1d6 --- /dev/null +++ b/src/leetcode/editor/cn/SolutionBestTimeToBuyAndSellStockIi.java @@ -0,0 +1,51 @@ +//给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。 +// +// 在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。 +// +// 返回 你能获得的 最大 利润 。 +// +// +// +// 示例 1: +// +// +//输入:prices = [7,1,5,3,6,4] +//输出:7 +//解释:在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4。 +//随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6 - 3 = 3。 +//最大总利润为 4 + 3 = 7 。 +// +// 示例 2: +// +// +//输入:prices = [1,2,3,4,5] +//输出:4 +//解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4。 +//最大总利润为 4 。 +// +// 示例 3: +// +// +//输入:prices = [7,6,4,3,1] +//输出:0 +//解释:在这种情况下, 交易无法获得正利润,所以不参与交易可以获得最大利润,最大利润为 0。 +// +// +// +// 提示: +// +// +// 1 <= prices.length <= 3 * 10⁴ +// 0 <= prices[i] <= 10⁴ +// +// +// Related Topics 贪心 数组 动态规划 👍 2774 👎 0 + + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int maxProfit(int[] prices) { + + } +} +//leetcode submit region end(Prohibit modification and deletion) diff --git a/src/leetcode/editor/cn/SolutionMajorityElement.java b/src/leetcode/editor/cn/SolutionMajorityElement.java new file mode 100644 index 0000000..2846d3f --- /dev/null +++ b/src/leetcode/editor/cn/SolutionMajorityElement.java @@ -0,0 +1,49 @@ +//给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。 +// +// 你可以假设数组是非空的,并且给定的数组总是存在多数元素。 +// +// +// +// 示例 1: +// +// +//输入:nums = [3,2,3] +//输出:3 +// +// 示例 2: +// +// +//输入:nums = [2,2,1,1,1,2,2] +//输出:2 +// +// +// +//提示: +// +// +// n == nums.length +// 1 <= n <= 5 * 10⁴ +// -10⁹ <= nums[i] <= 10⁹ +// +// +// +// +// 进阶:尝试设计时间复杂度为 O(n)、空间复杂度为 O(1) 的算法解决此问题。 +// +// Related Topics 数组 哈希表 分治 计数 排序 👍 2542 👎 0 + + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int majorityElement(int[] nums) { + Arrays.sort(nums); + return nums[nums.length/2]; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + +public void main(String[] args) { + System.out.println(new Solution().majorityElement(new int[]{3,2,3})); + System.out.println(new Solution().majorityElement(new int[]{2,2,1,1,1,2,2})); +} \ No newline at end of file diff --git a/src/leetcode/editor/cn/SolutionRotateArray.java b/src/leetcode/editor/cn/SolutionRotateArray.java new file mode 100644 index 0000000..5b7a155 --- /dev/null +++ b/src/leetcode/editor/cn/SolutionRotateArray.java @@ -0,0 +1,73 @@ +//给定一个整数数组 nums,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。 +// +// +// +// 示例 1: +// +// +//输入: nums = [1,2,3,4,5,6,7], k = 3 +//输出: [5,6,7,1,2,3,4] +//解释: +//向右轮转 1 步: [7,1,2,3,4,5,6] +//向右轮转 2 步: [6,7,1,2,3,4,5] +//向右轮转 3 步: [5,6,7,1,2,3,4] +// +// +// 示例 2: +// +// +//输入:nums = [-1,-100,3,99], k = 2 +//输出:[3,99,-1,-100] +//解释: +//向右轮转 1 步: [99,-1,-100,3] +//向右轮转 2 步: [3,99,-1,-100] +// +// +// +// 提示: +// +// +// 1 <= nums.length <= 10⁵ +// -2³¹ <= nums[i] <= 2³¹ - 1 +// 0 <= k <= 10⁵ +// +// +// +// +// 进阶: +// +// +// 尽可能想出更多的解决方案,至少有 三种 不同的方法可以解决这个问题。 +// 你可以使用空间复杂度为 O(1) 的 原地 算法解决这个问题吗? +// +// +// Related Topics 数组 数学 双指针 👍 2425 👎 0 + + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public void rotate(int[] nums, int k) { + k = k % nums.length; + int[] arr = new int[k]; + System.arraycopy(nums, nums.length - k, arr, 0, k); + +// int right = nums.length - 1; +// while (right >= k) { +// nums[right] = nums[right - k]; +// right--; +// } + System.arraycopy(nums, 0, nums, k, nums.length - k); + System.arraycopy(arr, 0, nums, 0, k); + } +} + +//leetcode submit region end(Prohibit modification and deletion) +void main() { +// System.out.println(-3 % 1); + + // [1,2] -> [2,1] -> [1,2] + + int[] arr = new int[]{1,2,3,4,5,6,7}; + new Solution().rotate(arr, 3); + System.out.println(Arrays.toString(arr)); +} \ No newline at end of file diff --git a/src/leetcode/editor/cn/doc/content/SolutionBestTimeToBuyAndSellStock.md b/src/leetcode/editor/cn/doc/content/SolutionBestTimeToBuyAndSellStock.md new file mode 100644 index 0000000..464032f --- /dev/null +++ b/src/leetcode/editor/cn/doc/content/SolutionBestTimeToBuyAndSellStock.md @@ -0,0 +1,35 @@ +

给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。

+ +

你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。

+ +

返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0

+ +

 

+ +

示例 1:

+ +
+输入:[7,1,5,3,6,4]
+输出:5
+解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
+     注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
+
+ +

示例 2:

+ +
+输入:prices = [7,6,4,3,1]
+输出:0
+解释:在这种情况下, 没有交易完成, 所以最大利润为 0。
+
+ +

 

+ +

提示:

+ + + +
Related Topics
  • 数组
  • 动态规划

  • 👍 3876
  • 👎 0
  • \ No newline at end of file diff --git a/src/leetcode/editor/cn/doc/content/SolutionBestTimeToBuyAndSellStockIi.md b/src/leetcode/editor/cn/doc/content/SolutionBestTimeToBuyAndSellStockIi.md new file mode 100644 index 0000000..8c90695 --- /dev/null +++ b/src/leetcode/editor/cn/doc/content/SolutionBestTimeToBuyAndSellStockIi.md @@ -0,0 +1,42 @@ +

    给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。

    + +

    在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。

    + +

    返回 你能获得的 最大 利润 。

    + +

     

    + +

    示例 1:

    + +
    +输入:prices = [7,1,5,3,6,4]
    +输出:7
    +解释:在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4。
    +随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6 - 3 = 3。
    +最大总利润为 4 + 3 = 7 。
    + +

    示例 2:

    + +
    +输入:prices = [1,2,3,4,5]
    +输出:4
    +解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4。
    +最大总利润为 4 。
    + +

    示例 3:

    + +
    +输入:prices = [7,6,4,3,1]
    +输出:0
    +解释:在这种情况下, 交易无法获得正利润,所以不参与交易可以获得最大利润,最大利润为 0。
    + +

     

    + +

    提示:

    + + + +
    Related Topics
  • 贪心
  • 数组
  • 动态规划

  • 👍 2774
  • 👎 0
  • \ No newline at end of file diff --git a/src/leetcode/editor/cn/doc/content/SolutionMajorityElement.md b/src/leetcode/editor/cn/doc/content/SolutionMajorityElement.md new file mode 100644 index 0000000..0525282 --- /dev/null +++ b/src/leetcode/editor/cn/doc/content/SolutionMajorityElement.md @@ -0,0 +1,33 @@ +

    给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。

    + +

    你可以假设数组是非空的,并且给定的数组总是存在多数元素。

    + +

     

    + +

    示例 1:

    + +
    +输入:nums = [3,2,3]
    +输出:3
    + +

    示例 2:

    + +
    +输入:nums = [2,2,1,1,1,2,2]
    +输出:2
    +
    + +

     

    +提示: + + + +

     

    + +

    进阶:尝试设计时间复杂度为 O(n)、空间复杂度为 O(1) 的算法解决此问题。

    + +
    Related Topics
  • 数组
  • 哈希表
  • 分治
  • 计数
  • 排序

  • 👍 2542
  • 👎 0
  • \ No newline at end of file diff --git a/src/leetcode/editor/cn/doc/content/SolutionRomanToInteger.md b/src/leetcode/editor/cn/doc/content/SolutionRomanToInteger.md new file mode 100644 index 0000000..a4e96fa --- /dev/null +++ b/src/leetcode/editor/cn/doc/content/SolutionRomanToInteger.md @@ -0,0 +1,73 @@ +

    罗马数字包含以下七种字符: I, V, X, LCD 和 M

    + +
    +字符          数值
    +I             1
    +V             5
    +X             10
    +L             50
    +C             100
    +D             500
    +M             1000
    + +

    例如, 罗马数字 2 写做 II ,即为两个并列的 1 。12 写做 XII ,即为 X + II 。 27 写做  XXVII, 即为 XX + V + II 。

    + +

    通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:

    + + + +

    给定一个罗马数字,将其转换成整数。

    + +

     

    + +

    示例 1:

    + +
    +输入: s = "III"
    +输出: 3
    + +

    示例 2:

    + +
    +输入: s = "IV"
    +输出: 4
    + +

    示例 3:

    + +
    +输入: s = "IX"
    +输出: 9
    + +

    示例 4:

    + +
    +输入: s = "LVIII"
    +输出: 58
    +解释: L = 50, V= 5, III = 3.
    +
    + +

    示例 5:

    + +
    +输入: s = "MCMXCIV"
    +输出: 1994
    +解释: M = 1000, CM = 900, XC = 90, IV = 4.
    + +

     

    + +

    提示:

    + + + +
    Related Topics
  • 哈希表
  • 数学
  • 字符串

  • 👍 2988
  • 👎 0
  • \ No newline at end of file diff --git a/src/leetcode/editor/cn/doc/content/SolutionRotateArray.md b/src/leetcode/editor/cn/doc/content/SolutionRotateArray.md new file mode 100644 index 0000000..3544306 --- /dev/null +++ b/src/leetcode/editor/cn/doc/content/SolutionRotateArray.md @@ -0,0 +1,44 @@ +

    给定一个整数数组 nums,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。

    + +

     

    + +

    示例 1:

    + +
    +输入: nums = [1,2,3,4,5,6,7], k = 3
    +输出: [5,6,7,1,2,3,4]
    +解释:
    +向右轮转 1 步: [7,1,2,3,4,5,6]
    +向右轮转 2 步: [6,7,1,2,3,4,5]
    +向右轮转 3 步: [5,6,7,1,2,3,4]
    +
    + +

    示例 2:

    + +
    +输入:nums = [-1,-100,3,99], k = 2
    +输出:[3,99,-1,-100]
    +解释: 
    +向右轮转 1 步: [99,-1,-100,3]
    +向右轮转 2 步: [3,99,-1,-100]
    + +

     

    + +

    提示:

    + + + +

     

    + +

    进阶:

    + + + +
    Related Topics
  • 数组
  • 数学
  • 双指针

  • 👍 2425
  • 👎 0
  • \ No newline at end of file