From 788e1e8e7157730b45f31248750e340a06b764ab Mon Sep 17 00:00:00 2001 From: shangliang Date: Thu, 3 Feb 2022 17:56:19 +0800 Subject: [PATCH 01/85] leetcode question doc --- .gitignore | 1 + .../editor/cn/doc/content/AddTwoNumbers.md | 40 +++++++++++++ .../cn/doc/content/ContainsDuplicateIi.md | 34 +++++++++++ .../cn/doc/content/CountVowelsPermutation.md | 42 ++++++++++++++ .../doc/content/FindKPairsWithSmallestSums.md | 45 +++++++++++++++ .../content/IncreasingTripletSubsequence.md | 42 ++++++++++++++ .../LargestNumberAtLeastTwiceOfOthers.md | 39 +++++++++++++ .../cn/doc/content/LinkedListRandomNode.md | 48 ++++++++++++++++ .../cn/doc/content/StockPriceFluctuation.md | 56 +++++++++++++++++++ .../editor/cn/doc/content/StoneGameIx.md | 56 +++++++++++++++++++ .../TheNumberOfWeakCharactersInTheGame.md | 42 ++++++++++++++ 11 files changed, 445 insertions(+) create mode 100644 src/com/leetcode/editor/cn/doc/content/AddTwoNumbers.md create mode 100644 src/com/leetcode/editor/cn/doc/content/ContainsDuplicateIi.md create mode 100644 src/com/leetcode/editor/cn/doc/content/CountVowelsPermutation.md create mode 100644 src/com/leetcode/editor/cn/doc/content/FindKPairsWithSmallestSums.md create mode 100644 src/com/leetcode/editor/cn/doc/content/IncreasingTripletSubsequence.md create mode 100644 src/com/leetcode/editor/cn/doc/content/LargestNumberAtLeastTwiceOfOthers.md create mode 100644 src/com/leetcode/editor/cn/doc/content/LinkedListRandomNode.md create mode 100644 src/com/leetcode/editor/cn/doc/content/StockPriceFluctuation.md create mode 100644 src/com/leetcode/editor/cn/doc/content/StoneGameIx.md create mode 100644 src/com/leetcode/editor/cn/doc/content/TheNumberOfWeakCharactersInTheGame.md diff --git a/.gitignore b/.gitignore index 068778f..736d1ee 100644 --- a/.gitignore +++ b/.gitignore @@ -89,3 +89,4 @@ nbdist/ /out/production/Algorithm/com/liang/Test/ConsistentHashingWithVirtualNode.class /out/production/Algorithm/com/liang/Test/RSAUtils.class /out/production/Algorithm/com/liang/Test/Solution.class +/out diff --git a/src/com/leetcode/editor/cn/doc/content/AddTwoNumbers.md b/src/com/leetcode/editor/cn/doc/content/AddTwoNumbers.md new file mode 100644 index 0000000..4842831 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/AddTwoNumbers.md @@ -0,0 +1,40 @@ +

给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。

+ +

请你将两个数相加,并以相同形式返回一个表示和的链表。

+ +

你可以假设除了数字 0 之外,这两个数都不会以 0 开头。

+ +

 

+ +

示例 1:

+ +
+输入:l1 = [2,4,3], l2 = [5,6,4]
+输出:[7,0,8]
+解释:342 + 465 = 807.
+
+ +

示例 2:

+ +
+输入:l1 = [0], l2 = [0]
+输出:[0]
+
+ +

示例 3:

+ +
+输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
+输出:[8,9,9,9,0,0,0,1]
+
+ +

 

+ +

提示:

+ + +
Related Topics
  • 递归
  • 链表
  • 数学

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

    给你一个整数数组 nums 和一个整数 k ,判断数组中是否存在两个 不同的索引 i 和 j ,满足 nums[i] == nums[j]abs(i - j) <= k 。如果存在,返回 true ;否则,返回 false

    + +

     

    + +

    示例 1:

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

    示例 2:

    + +
    +输入:nums = [1,0,1,1], k = 1
    +输出:true
    + +

    示例 3:

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

     

    + +

     

    + +

    提示:

    + + +
    Related Topics
  • 数组
  • 哈希表
  • 滑动窗口

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

    给你一个整数 n,请你帮忙统计一下我们可以按下述规则形成多少个长度为 n 的字符串:

    + + + +

    由于答案可能会很大,所以请你返回 模 10^9 + 7 之后的结果。

    + +

     

    + +

    示例 1:

    + +
    输入:n = 1
    +输出:5
    +解释:所有可能的字符串分别是:"a", "e", "i" , "o" 和 "u"。
    +
    + +

    示例 2:

    + +
    输入:n = 2
    +输出:10
    +解释:所有可能的字符串分别是:"ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou" 和 "ua"。
    +
    + +

    示例 3:

    + +
    输入:n = 5
    +输出:68
    + +

     

    + +

    提示:

    + + +
    Related Topics
  • 动态规划

  • 👍 101
  • 👎 0
  • \ No newline at end of file diff --git a/src/com/leetcode/editor/cn/doc/content/FindKPairsWithSmallestSums.md b/src/com/leetcode/editor/cn/doc/content/FindKPairsWithSmallestSums.md new file mode 100644 index 0000000..20ac5f5 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/FindKPairsWithSmallestSums.md @@ -0,0 +1,45 @@ +

    给定两个以升序排列的整数数组 nums1 nums2 , 以及一个整数 k 

    + +

    定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2 

    + +

    请找到和最小的 k 个数对 (u1,v1),  (u2,v2)  ...  (uk,vk) 。

    + +

     

    + +

    示例 1:

    + +
    +输入: nums1 = [1,7,11], nums2 = [2,4,6], k = 3
    +输出: [1,2],[1,4],[1,6]
    +解释: 返回序列中的前 3 对数:
    +     [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
    +
    + +

    示例 2:

    + +
    +输入: nums1 = [1,1,2], nums2 = [1,2,3], k = 2
    +输出: [1,1],[1,1]
    +解释: 返回序列中的前 2 对数:
    +     [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
    +
    + +

    示例 3:

    + +
    +输入: nums1 = [1,2], nums2 = [3], k = 3 
    +输出: [1,3],[2,3]
    +解释: 也可能序列中所有的数对都被返回:[1,3],[2,3]
    +
    + +

     

    + +

    提示:

    + + +
    Related Topics
  • 数组
  • 堆(优先队列)

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

    给你一个整数数组 nums ,判断这个数组中是否存在长度为 3 的递增子序列。

    + +

    如果存在这样的三元组下标 (i, j, k) 且满足 i < j < k ,使得 nums[i] < nums[j] < nums[k] ,返回 true ;否则,返回 false

    + +

     

    + +

    示例 1:

    + +
    +输入:nums = [1,2,3,4,5]
    +输出:true
    +解释:任何 i < j < k 的三元组都满足题意
    +
    + +

    示例 2:

    + +
    +输入:nums = [5,4,3,2,1]
    +输出:false
    +解释:不存在满足题意的三元组
    + +

    示例 3:

    + +
    +输入:nums = [2,1,5,0,4,6]
    +输出:true
    +解释:三元组 (3, 4, 5) 满足题意,因为 nums[3] == 0 < nums[4] == 4 < nums[5] == 6
    +
    + +

     

    + +

    提示:

    + + + +

     

    + +

    进阶:你能实现时间复杂度为 O(n) ,空间复杂度为 O(1) 的解决方案吗?

    +
    Related Topics
  • 贪心
  • 数组

  • 👍 489
  • 👎 0
  • \ No newline at end of file diff --git a/src/com/leetcode/editor/cn/doc/content/LargestNumberAtLeastTwiceOfOthers.md b/src/com/leetcode/editor/cn/doc/content/LargestNumberAtLeastTwiceOfOthers.md new file mode 100644 index 0000000..7a75454 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/LargestNumberAtLeastTwiceOfOthers.md @@ -0,0 +1,39 @@ +

    给你一个整数数组 nums ,其中总是存在 唯一的 一个最大整数 。

    + +

    请你找出数组中的最大元素并检查它是否 至少是数组中每个其他数字的两倍 。如果是,则返回 最大元素的下标 ,否则返回 -1

    + +

     

    + +

    示例 1:

    + +
    +输入:nums = [3,6,1,0]
    +输出:1
    +解释:6 是最大的整数,对于数组中的其他整数,6 大于数组中其他元素的两倍。6 的下标是 1 ,所以返回 1 。
    +
    + +

    示例 2:

    + +
    +输入:nums = [1,2,3,4]
    +输出:-1
    +解释:4 没有超过 3 的两倍大,所以返回 -1 。
    + +

    示例 3:

    + +
    +输入:nums = [1]
    +输出:0
    +解释:因为不存在其他数字,所以认为现有数字 1 至少是其他数字的两倍。
    +
    + +

     

    + +

    提示:

    + + +
    Related Topics
  • 数组
  • 排序

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

    给你一个单链表,随机选择链表的一个节点,并返回相应的节点值。每个节点 被选中的概率一样

    + +

    实现 Solution 类:

    + + + +

     

    + +

    示例:

    + +
    +输入
    +["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"]
    +[[[1, 2, 3]], [], [], [], [], []]
    +输出
    +[null, 1, 3, 2, 2, 3]
    +
    +解释
    +Solution solution = new Solution([1, 2, 3]);
    +solution.getRandom(); // 返回 1
    +solution.getRandom(); // 返回 3
    +solution.getRandom(); // 返回 2
    +solution.getRandom(); // 返回 2
    +solution.getRandom(); // 返回 3
    +// getRandom() 方法应随机返回 1、2、3中的一个,每个元素被返回的概率相等。
    + +

     

    + +

    提示:

    + + + +

     

    + +

    进阶:

    + + +
    Related Topics
  • 水塘抽样
  • 链表
  • 数学
  • 随机化

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

    给你一支股票价格的数据流。数据流中每一条记录包含一个 时间戳 和该时间点股票对应的 价格 。

    + +

    不巧的是,由于股票市场内在的波动性,股票价格记录可能不是按时间顺序到来的。某些情况下,有的记录可能是错的。如果两个有相同时间戳的记录出现在数据流中,前一条记录视为错误记录,后出现的记录 更正 前一条错误的记录。

    + +

    请你设计一个算法,实现:

    + + + +

    请你实现 StockPrice 类:

    + + + +

     

    + +

    示例 1:

    + +
    输入:
    +["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"]
    +[[], [1, 10], [2, 5], [], [], [1, 3], [], [4, 2], []]
    +输出:
    +[null, null, null, 5, 10, null, 5, null, 2]
    +
    +解释:
    +StockPrice stockPrice = new StockPrice();
    +stockPrice.update(1, 10); // 时间戳为 [1] ,对应的股票价格为 [10] 。
    +stockPrice.update(2, 5);  // 时间戳为 [1,2] ,对应的股票价格为 [10,5] 。
    +stockPrice.current();     // 返回 5 ,最新时间戳为 2 ,对应价格为 5 。
    +stockPrice.maximum();     // 返回 10 ,最高价格的时间戳为 1 ,价格为 10 。
    +stockPrice.update(1, 3);  // 之前时间戳为 1 的价格错误,价格更新为 3 。
    +                          // 时间戳为 [1,2] ,对应股票价格为 [3,5] 。
    +stockPrice.maximum();     // 返回 5 ,更正后最高价格为 5 。
    +stockPrice.update(4, 2);  // 时间戳为 [1,2,4] ,对应价格为 [3,5,2] 。
    +stockPrice.minimum();     // 返回 2 ,最低价格时间戳为 4 ,价格为 2 。
    +
    + +

     

    + +

    提示:

    + + +
    Related Topics
  • 设计
  • 哈希表
  • 数据流
  • 有序集合
  • 堆(优先队列)

  • 👍 64
  • 👎 0
  • \ No newline at end of file diff --git a/src/com/leetcode/editor/cn/doc/content/StoneGameIx.md b/src/com/leetcode/editor/cn/doc/content/StoneGameIx.md new file mode 100644 index 0000000..96e576c --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/StoneGameIx.md @@ -0,0 +1,56 @@ +

    Alice 和 Bob 再次设计了一款新的石子游戏。现有一行 n 个石子,每个石子都有一个关联的数字表示它的价值。给你一个整数数组 stones ,其中 stones[i] 是第 i 个石子的价值。

    + +

    Alice 和 Bob 轮流进行自己的回合,Alice 先手。每一回合,玩家需要从 stones 中移除任一石子。

    + + + +

    假设两位玩家均采用 最佳 决策。如果 Alice 获胜,返回 true ;如果 Bob 获胜,返回 false

    + +

     

    + +

    示例 1:

    + +
    +输入:stones = [2,1]
    +输出:true
    +解释:游戏进行如下:
    +- 回合 1:Alice 可以移除任意一个石子。
    +- 回合 2:Bob 移除剩下的石子。 
    +已移除的石子的值总和为 1 + 2 = 3 且可以被 3 整除。因此,Bob 输,Alice 获胜。
    +
    + +

    示例 2:

    + +
    +输入:stones = [2]
    +输出:false
    +解释:Alice 会移除唯一一个石子,已移除石子的值总和为 2 。 
    +由于所有石子都已移除,且值总和无法被 3 整除,Bob 获胜。
    +
    + +

    示例 3:

    + +
    +输入:stones = [5,1,2,4,3]
    +输出:false
    +解释:Bob 总会获胜。其中一种可能的游戏进行方式如下:
    +- 回合 1:Alice 可以移除值为 1 的第 2 个石子。已移除石子值总和为 1 。
    +- 回合 2:Bob 可以移除值为 3 的第 5 个石子。已移除石子值总和为 = 1 + 3 = 4 。
    +- 回合 3:Alices 可以移除值为 4 的第 4 个石子。已移除石子值总和为 = 1 + 3 + 4 = 8 。
    +- 回合 4:Bob 可以移除值为 2 的第 3 个石子。已移除石子值总和为 = 1 + 3 + 4 + 2 = 10.
    +- 回合 5:Alice 可以移除值为 5 的第 1 个石子。已移除石子值总和为 = 1 + 3 + 4 + 2 + 5 = 15.
    +Alice 输掉游戏,因为已移除石子值总和(15)可以被 3 整除,Bob 获胜。
    +
    + +

     

    + +

    提示:

    + + +
    Related Topics
  • 贪心
  • 数组
  • 数学
  • 计数
  • 博弈

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

    你正在参加一个多角色游戏,每个角色都有两个主要属性:攻击防御 。给你一个二维整数数组 properties ,其中 properties[i] = [attacki, defensei] 表示游戏中第 i 个角色的属性。

    + +

    如果存在一个其他角色的攻击和防御等级 都严格高于 该角色的攻击和防御等级,则认为该角色为 弱角色 。更正式地,如果认为角色 i 弱于 存在的另一个角色 j ,那么 attackj > attackidefensej > defensei

    + +

    返回 弱角色 的数量。

    + +

     

    + +

    示例 1:

    + +
    +输入:properties = [[5,5],[6,3],[3,6]]
    +输出:0
    +解释:不存在攻击和防御都严格高于其他角色的角色。
    +
    + +

    示例 2:

    + +
    +输入:properties = [[2,2],[3,3]]
    +输出:1
    +解释:第一个角色是弱角色,因为第二个角色的攻击和防御严格大于该角色。
    +
    + +

    示例 3:

    + +
    +输入:properties = [[1,5],[10,4],[4,3]]
    +输出:1
    +解释:第三个角色是弱角色,因为第二个角色的攻击和防御严格大于该角色。
    +
    + +

     

    + +

    提示:

    + + +
    Related Topics
  • 贪心
  • 数组
  • 排序
  • 单调栈

  • 👍 108
  • 👎 0
  • \ No newline at end of file From f6674773489bd17c908ce9572407d8699f71fa88 Mon Sep 17 00:00:00 2001 From: shangliang Date: Fri, 4 Feb 2022 22:11:24 +0800 Subject: [PATCH 02/85] =?UTF-8?q?1725=20=E5=8F=AF=E4=BB=A5=E5=BD=A2?= =?UTF-8?q?=E6=88=90=E6=9C=80=E5=A4=A7=E6=AD=A3=E6=96=B9=E5=BD=A2=E7=9A=84?= =?UTF-8?q?=E7=9F=A9=E5=BD=A2=E6=95=B0=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...RectanglesThatCanFormTheLargestSquare.java | 77 +++++++++++++++++++ ...OfRectanglesThatCanFormTheLargestSquare.md | 37 +++++++++ 2 files changed, 114 insertions(+) create mode 100644 src/com/leetcode/editor/cn/NumberOfRectanglesThatCanFormTheLargestSquare.java create mode 100644 src/com/leetcode/editor/cn/doc/content/NumberOfRectanglesThatCanFormTheLargestSquare.md diff --git a/src/com/leetcode/editor/cn/NumberOfRectanglesThatCanFormTheLargestSquare.java b/src/com/leetcode/editor/cn/NumberOfRectanglesThatCanFormTheLargestSquare.java new file mode 100644 index 0000000..9f2993c --- /dev/null +++ b/src/com/leetcode/editor/cn/NumberOfRectanglesThatCanFormTheLargestSquare.java @@ -0,0 +1,77 @@ +package com.leetcode.editor.cn; + +//给你一个数组 rectangles ,其中 rectangles[i] = [li, wi] 表示第 i 个矩形的长度为 li 、宽度为 wi 。 +// +// 如果存在 k 同时满足 k <= li 和 k <= wi ,就可以将第 i 个矩形切成边长为 k 的正方形。例如,矩形 [4,6] 可以切成边长最大为 +//4 的正方形。 +// +// 设 maxLen 为可以从矩形数组 rectangles 切分得到的 最大正方形 的边长。 +// +// 请你统计有多少个矩形能够切出边长为 maxLen 的正方形,并返回矩形 数目 。 +// +// +// +// 示例 1: +// +// +//输入:rectangles = [[5,8],[3,9],[5,12],[16,5]] +//输出:3 +//解释:能从每个矩形中切出的最大正方形边长分别是 [5,3,5,5] 。 +//最大正方形的边长为 5 ,可以由 3 个矩形切分得到。 +// +// +// 示例 2: +// +// +//输入:rectangles = [[2,3],[3,7],[4,3],[3,7]] +//输出:3 +// +// +// +// +// 提示: +// +// +// 1 <= rectangles.length <= 1000 +// rectangles[i].length == 2 +// 1 <= li, wi <= 10⁹ +// li != wi +// +// Related Topics 数组 👍 47 👎 0 + + +/** + * 1725 可以形成最大正方形的矩形数目 + * @date 2022-02-04 22:08:26 + * @author shang.liang + */ + public class NumberOfRectanglesThatCanFormTheLargestSquare{ + public static void main(String[] args){ + Solution soution = new NumberOfRectanglesThatCanFormTheLargestSquare().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int countGoodRectangles(int[][] rectangles) { + + int res = 0; + int maxLen = 0; + + for (int[] rectangle : rectangles) { + int currLen = Math.min(rectangle[0], rectangle[1]); + + if (currLen == maxLen) { + res++; + } else if (currLen > maxLen) { + res = 1; + maxLen = currLen; + } + } + + return res; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode/editor/cn/doc/content/NumberOfRectanglesThatCanFormTheLargestSquare.md b/src/com/leetcode/editor/cn/doc/content/NumberOfRectanglesThatCanFormTheLargestSquare.md new file mode 100644 index 0000000..0bf230c --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/NumberOfRectanglesThatCanFormTheLargestSquare.md @@ -0,0 +1,37 @@ +

    给你一个数组 rectangles ,其中 rectangles[i] = [li, wi] 表示第 i 个矩形的长度为 li 、宽度为 wi

    + +

    如果存在 k 同时满足 k <= lik <= wi ,就可以将第 i 个矩形切成边长为 k 的正方形。例如,矩形 [4,6] 可以切成边长最大为 4 的正方形。

    + +

    maxLen 为可以从矩形数组 rectangles 切分得到的 最大正方形 的边长。

    + +

    请你统计有多少个矩形能够切出边长为 maxLen 的正方形,并返回矩形 数目

    + +

     

    + +

    示例 1:

    + +
    +输入:rectangles = [[5,8],[3,9],[5,12],[16,5]]
    +输出:3
    +解释:能从每个矩形中切出的最大正方形边长分别是 [5,3,5,5] 。
    +最大正方形的边长为 5 ,可以由 3 个矩形切分得到。
    +
    + +

    示例 2:

    + +
    +输入:rectangles = [[2,3],[3,7],[4,3],[3,7]]
    +输出:3
    +
    + +

     

    + +

    提示:

    + + +
    Related Topics
  • 数组

  • 👍 47
  • 👎 0
  • \ No newline at end of file From 38b729db993ff51a610a27b8d8fa88f530ff9ec6 Mon Sep 17 00:00:00 2001 From: shangliang Date: Mon, 7 Feb 2022 23:19:24 +0800 Subject: [PATCH 03/85] =?UTF-8?q?1405=20=E6=9C=80=E9=95=BF=E5=BF=AB?= =?UTF-8?q?=E4=B9=90=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/LongestHappyString.java | 89 +++++++++++++++++++ .../cn/doc/content/LongestHappyString.md | 42 +++++++++ 2 files changed, 131 insertions(+) create mode 100644 src/com/leetcode/editor/cn/LongestHappyString.java create mode 100644 src/com/leetcode/editor/cn/doc/content/LongestHappyString.md diff --git a/src/com/leetcode/editor/cn/LongestHappyString.java b/src/com/leetcode/editor/cn/LongestHappyString.java new file mode 100644 index 0000000..1d0e6ce --- /dev/null +++ b/src/com/leetcode/editor/cn/LongestHappyString.java @@ -0,0 +1,89 @@ +package com.leetcode.editor.cn; + +//如果字符串中不含有任何 'aaa','bbb' 或 'ccc' 这样的字符串作为子串,那么该字符串就是一个「快乐字符串」。 +// +// 给你三个整数 a,b ,c,请你返回 任意一个 满足下列全部条件的字符串 s: +// +// +// s 是一个尽可能长的快乐字符串。 +// s 中 最多 有a 个字母 'a'、b 个字母 'b'、c 个字母 'c' 。 +// s 中只含有 'a'、'b' 、'c' 三种字母。 +// +// +// 如果不存在这样的字符串 s ,请返回一个空字符串 ""。 +// +// +// +// 示例 1: +// +// 输入:a = 1, b = 1, c = 7 +//输出:"ccaccbcc" +//解释:"ccbccacc" 也是一种正确答案。 +// +// +// 示例 2: +// +// 输入:a = 2, b = 2, c = 1 +//输出:"aabbc" +// +// +// 示例 3: +// +// 输入:a = 7, b = 1, c = 0 +//输出:"aabaa" +//解释:这是该测试用例的唯一正确答案。 +// +// +// +// 提示: +// +// +// 0 <= a, b, c <= 100 +// a + b + c > 0 +// +// Related Topics 贪心 字符串 堆(优先队列) 👍 168 👎 0 + + +import java.util.PriorityQueue; + +/** + * 1405 最长快乐字符串 + * + * @author shang.liang + * @date 2022-02-07 22:59:44 + */ +public class LongestHappyString { + public static void main(String[] args) { + Solution soution = new LongestHappyString().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public String longestDiverseString(int a, int b, int c) { + + PriorityQueue q = new PriorityQueue<>((x, y) -> y[1] - x[1]); + if (a > 0) q.add(new int[]{0, a}); + if (b > 0) q.add(new int[]{1, b}); + if (c > 0) q.add(new int[]{2, c}); + StringBuilder sb = new StringBuilder(); + while (!q.isEmpty()) { + int[] cur = q.poll(); + int n = sb.length(); + if (n >= 2 && sb.charAt(n - 1) - 'a' == cur[0] && sb.charAt(n - 2) - 'a' == cur[0]) { + if (q.isEmpty()) break; + int[] next = q.poll(); + sb.append((char) (next[0] + 'a')); + if (--next[1] != 0) q.add(next); + q.add(cur); + } else { + sb.append((char) (cur[0] + 'a')); + if (--cur[1] != 0) q.add(cur); + } + } + return sb.toString(); + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/com/leetcode/editor/cn/doc/content/LongestHappyString.md b/src/com/leetcode/editor/cn/doc/content/LongestHappyString.md new file mode 100644 index 0000000..2728ad5 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/LongestHappyString.md @@ -0,0 +1,42 @@ +

    如果字符串中不含有任何 'aaa''bbb''ccc' 这样的字符串作为子串,那么该字符串就是一个「快乐字符串」。

    + +

    给你三个整数 abc,请你返回 任意一个 满足下列全部条件的字符串 s

    + +
      +
    • s 是一个尽可能长的快乐字符串。
    • +
    • s最多a 个字母 'a'b 个字母 'b'c 个字母 'c'
    • +
    • s 中只含有 'a''b''c' 三种字母。
    • +
    + +

    如果不存在这样的字符串 s ,请返回一个空字符串 ""

    + +

     

    + +

    示例 1:

    + +
    输入:a = 1, b = 1, c = 7
    +输出:"ccaccbcc"
    +解释:"ccbccacc" 也是一种正确答案。
    +
    + +

    示例 2:

    + +
    输入:a = 2, b = 2, c = 1
    +输出:"aabbc"
    +
    + +

    示例 3:

    + +
    输入:a = 7, b = 1, c = 0
    +输出:"aabaa"
    +解释:这是该测试用例的唯一正确答案。
    + +

     

    + +

    提示:

    + +
      +
    • 0 <= a, b, c <= 100
    • +
    • a + b + c > 0
    • +
    +
    Related Topics
  • 贪心
  • 字符串
  • 堆(优先队列)

  • 👍 168
  • 👎 0
  • \ No newline at end of file From 0dbb42a64f9ac366f9d5d041dac112b0083088ba Mon Sep 17 00:00:00 2001 From: shangliang Date: Wed, 9 Feb 2022 11:34:09 +0800 Subject: [PATCH 04/85] =?UTF-8?q?2006=20=E5=B7=AE=E7=9A=84=E7=BB=9D?= =?UTF-8?q?=E5=AF=B9=E5=80=BC=E4=B8=BA=20K=20=E7=9A=84=E6=95=B0=E5=AF=B9?= =?UTF-8?q?=E6=95=B0=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...tNumberOfPairsWithAbsoluteDifferenceK.java | 89 +++++++++++++++++++ ...untNumberOfPairsWithAbsoluteDifferenceK.md | 49 ++++++++++ 2 files changed, 138 insertions(+) create mode 100644 src/com/leetcode/editor/cn/CountNumberOfPairsWithAbsoluteDifferenceK.java create mode 100644 src/com/leetcode/editor/cn/doc/content/CountNumberOfPairsWithAbsoluteDifferenceK.md diff --git a/src/com/leetcode/editor/cn/CountNumberOfPairsWithAbsoluteDifferenceK.java b/src/com/leetcode/editor/cn/CountNumberOfPairsWithAbsoluteDifferenceK.java new file mode 100644 index 0000000..55aa4ca --- /dev/null +++ b/src/com/leetcode/editor/cn/CountNumberOfPairsWithAbsoluteDifferenceK.java @@ -0,0 +1,89 @@ +package com.leetcode.editor.cn; + +//给你一个整数数组 nums 和一个整数 k ,请你返回数对 (i, j) 的数目,满足 i < j 且 |nums[i] - nums[j]| == k 。 +// +// +// |x| 的值定义为: +// +// +// 如果 x >= 0 ,那么值为 x 。 +// 如果 x < 0 ,那么值为 -x 。 +// +// +// +// +// 示例 1: +// +// 输入:nums = [1,2,2,1], k = 1 +//输出:4 +//解释:差的绝对值为 1 的数对为: +//- [1,2,2,1] +//- [1,2,2,1] +//- [1,2,2,1] +//- [1,2,2,1] +// +// +// 示例 2: +// +// 输入:nums = [1,3], k = 3 +//输出:0 +//解释:没有任何数对差的绝对值为 3 。 +// +// +// 示例 3: +// +// 输入:nums = [3,2,1,5,4], k = 2 +//输出:3 +//解释:差的绝对值为 2 的数对为: +//- [3,2,1,5,4] +//- [3,2,1,5,4] +//- [3,2,1,5,4] +// +// +// +// +// 提示: +// +// +// 1 <= nums.length <= 200 +// 1 <= nums[i] <= 100 +// 1 <= k <= 99 +// +// Related Topics 数组 哈希表 计数 👍 26 👎 0 + + +/** + * 2006 差的绝对值为 K 的数对数目 + * + * @author shang.liang + * @date 2022-02-09 10:00:57 + */ +public class CountNumberOfPairsWithAbsoluteDifferenceK { + public static void main(String[] args) { + Solution soution = new CountNumberOfPairsWithAbsoluteDifferenceK().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int countKDifference(int[] nums, int k) { + + int tmpNum, res = 0; + + for (int i = 0; i < nums.length; i++) { + for (int j = i + 1; j < nums.length; j++) { + + tmpNum = nums[i] - nums[j]; + + if (tmpNum == k || tmpNum + k == 0) { + res++; + } + } + } + + return res; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/com/leetcode/editor/cn/doc/content/CountNumberOfPairsWithAbsoluteDifferenceK.md b/src/com/leetcode/editor/cn/doc/content/CountNumberOfPairsWithAbsoluteDifferenceK.md new file mode 100644 index 0000000..5cbcb02 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/CountNumberOfPairsWithAbsoluteDifferenceK.md @@ -0,0 +1,49 @@ +

    给你一个整数数组 nums 和一个整数 k ,请你返回数对 (i, j) 的数目,满足 i < j 且 |nums[i] - nums[j]| == k 。

    + +

    |x| 的值定义为:

    + +
      +
    • 如果 x >= 0 ,那么值为 x 。
    • +
    • 如果 x < 0 ,那么值为 -x 。
    • +
    + +

     

    + +

    示例 1:

    + +
    输入:nums = [1,2,2,1], k = 1
    +输出:4
    +解释:差的绝对值为 1 的数对为:
    +- [1,2,2,1]
    +- [1,2,2,1]
    +- [1,2,2,1]
    +- [1,2,2,1]
    +
    + +

    示例 2:

    + +
    输入:nums = [1,3], k = 3
    +输出:0
    +解释:没有任何数对差的绝对值为 3 。
    +
    + +

    示例 3:

    + +
    输入:nums = [3,2,1,5,4], k = 2
    +输出:3
    +解释:差的绝对值为 2 的数对为:
    +- [3,2,1,5,4]
    +- [3,2,1,5,4]
    +- [3,2,1,5,4]
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= nums.length <= 200
    • +
    • 1 <= nums[i] <= 100
    • +
    • 1 <= k <= 99
    • +
    +
    Related Topics
  • 数组
  • 哈希表
  • 计数

  • 👍 26
  • 👎 0
  • \ No newline at end of file From 4c2c1c7387a35de16c08dad90168bdae70f5f6cb Mon Sep 17 00:00:00 2001 From: shangliang Date: Thu, 10 Feb 2022 22:17:56 +0800 Subject: [PATCH 05/85] =?UTF-8?q?1447=20=E6=9C=80=E7=AE=80=E5=88=86?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/SimplifiedFractions.java | 80 +++++++++++++++++++ .../cn/doc/content/SimplifiedFractions.md | 36 +++++++++ 2 files changed, 116 insertions(+) create mode 100644 src/com/leetcode/editor/cn/SimplifiedFractions.java create mode 100644 src/com/leetcode/editor/cn/doc/content/SimplifiedFractions.md diff --git a/src/com/leetcode/editor/cn/SimplifiedFractions.java b/src/com/leetcode/editor/cn/SimplifiedFractions.java new file mode 100644 index 0000000..a2b16c7 --- /dev/null +++ b/src/com/leetcode/editor/cn/SimplifiedFractions.java @@ -0,0 +1,80 @@ +package com.leetcode.editor.cn; + +//给你一个整数 n ,请你返回所有 0 到 1 之间(不包括 0 和 1)满足分母小于等于 n 的 最简 分数 。分数可以以 任意 顺序返回。 +// +// +// +// 示例 1: +// +// 输入:n = 2 +//输出:["1/2"] +//解释:"1/2" 是唯一一个分母小于等于 2 的最简分数。 +// +// 示例 2: +// +// 输入:n = 3 +//输出:["1/2","1/3","2/3"] +// +// +// 示例 3: +// +// 输入:n = 4 +//输出:["1/2","1/3","1/4","2/3","3/4"] +//解释:"2/4" 不是最简分数,因为它可以化简为 "1/2" 。 +// +// 示例 4: +// +// 输入:n = 1 +//输出:[] +// +// +// +// +// 提示: +// +// +// 1 <= n <= 100 +// +// Related Topics 数学 字符串 数论 👍 74 👎 0 + + +import java.util.ArrayList; +import java.util.List; + +/** + * 1447 最简分数 + * + * @author shang.liang + * @date 2022-02-10 21:47:59 + */ +public class SimplifiedFractions { + public static void main(String[] args) { + Solution soution = new SimplifiedFractions().new Solution(); + int n = 3; + System.out.println("soution.simplifiedFractions(n) = " + soution.simplifiedFractions(n)); + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public List simplifiedFractions(int n) { + + List resList = new ArrayList(); + + for (int i = 2; i <= n; i++) { + for (int j = 1; j < i; j++) { + if (gcd(i, j) == 1) { + resList.add(j + "/" +i); + } + } + } + + return resList; + } + + int gcd(int i, int j) { + return j == 0 ? i : gcd(j, i % j); + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/com/leetcode/editor/cn/doc/content/SimplifiedFractions.md b/src/com/leetcode/editor/cn/doc/content/SimplifiedFractions.md new file mode 100644 index 0000000..1c0a9b3 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/SimplifiedFractions.md @@ -0,0 +1,36 @@ +

    给你一个整数 n ,请你返回所有 0 到 1 之间(不包括 0 和 1)满足分母小于等于  n 的 最简 分数 。分数可以以 任意 顺序返回。

    + +

     

    + +

    示例 1:

    + +
    输入:n = 2
    +输出:["1/2"]
    +解释:"1/2" 是唯一一个分母小于等于 2 的最简分数。
    + +

    示例 2:

    + +
    输入:n = 3
    +输出:["1/2","1/3","2/3"]
    +
    + +

    示例 3:

    + +
    输入:n = 4
    +输出:["1/2","1/3","1/4","2/3","3/4"]
    +解释:"2/4" 不是最简分数,因为它可以化简为 "1/2" 。
    + +

    示例 4:

    + +
    输入:n = 1
    +输出:[]
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= n <= 100
    • +
    +
    Related Topics
  • 数学
  • 字符串
  • 数论

  • 👍 74
  • 👎 0
  • \ No newline at end of file From 3c924e903ae31b73595e6ed3510f4560e8e63afb Mon Sep 17 00:00:00 2001 From: shangliang Date: Thu, 10 Feb 2022 22:20:16 +0800 Subject: [PATCH 06/85] =?UTF-8?q?1447=20=E6=9C=80=E7=AE=80=E5=88=86?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/leetcode/editor/cn/SimplifiedFractions.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/leetcode/editor/cn/SimplifiedFractions.java b/src/com/leetcode/editor/cn/SimplifiedFractions.java index a2b16c7..bc2f1b6 100644 --- a/src/com/leetcode/editor/cn/SimplifiedFractions.java +++ b/src/com/leetcode/editor/cn/SimplifiedFractions.java @@ -58,7 +58,7 @@ public static void main(String[] args) { class Solution { public List simplifiedFractions(int n) { - List resList = new ArrayList(); + List resList = new ArrayList<>(); for (int i = 2; i <= n; i++) { for (int j = 1; j < i; j++) { From 53fbaf0af53d6a89cb34d0e2e7e8582dc2827e3f Mon Sep 17 00:00:00 2001 From: shangliang Date: Fri, 11 Feb 2022 10:18:28 +0800 Subject: [PATCH 07/85] =?UTF-8?q?1984=20=E5=AD=A6=E7=94=9F=E5=88=86?= =?UTF-8?q?=E6=95=B0=E7=9A=84=E6=9C=80=E5=B0=8F=E5=B7=AE=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...renceBetweenHighestAndLowestOfKScores.java | 80 +++++++++++++++++++ ...ferenceBetweenHighestAndLowestOfKScores.md | 39 +++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/com/leetcode/editor/cn/MinimumDifferenceBetweenHighestAndLowestOfKScores.java create mode 100644 src/com/leetcode/editor/cn/doc/content/MinimumDifferenceBetweenHighestAndLowestOfKScores.md diff --git a/src/com/leetcode/editor/cn/MinimumDifferenceBetweenHighestAndLowestOfKScores.java b/src/com/leetcode/editor/cn/MinimumDifferenceBetweenHighestAndLowestOfKScores.java new file mode 100644 index 0000000..2be9e3d --- /dev/null +++ b/src/com/leetcode/editor/cn/MinimumDifferenceBetweenHighestAndLowestOfKScores.java @@ -0,0 +1,80 @@ +package com.leetcode.editor.cn; + +//给你一个 下标从 0 开始 的整数数组 nums ,其中 nums[i] 表示第 i 名学生的分数。另给你一个整数 k 。 +// +// 从数组中选出任意 k 名学生的分数,使这 k 个分数间 最高分 和 最低分 的 差值 达到 最小化 。 +// +// 返回可能的 最小差值 。 +// +// +// +// 示例 1: +// +// 输入:nums = [90], k = 1 +//输出:0 +//解释:选出 1 名学生的分数,仅有 1 种方法: +//- [90] 最高分和最低分之间的差值是 90 - 90 = 0 +//可能的最小差值是 0 +// +// +// 示例 2: +// +// 输入:nums = [9,4,1,7], k = 2 +//输出:2 +//解释:选出 2 名学生的分数,有 6 种方法: +//- [9,4,1,7] 最高分和最低分之间的差值是 9 - 4 = 5 +//- [9,4,1,7] 最高分和最低分之间的差值是 9 - 1 = 8 +//- [9,4,1,7] 最高分和最低分之间的差值是 9 - 7 = 2 +//- [9,4,1,7] 最高分和最低分之间的差值是 4 - 1 = 3 +//- [9,4,1,7] 最高分和最低分之间的差值是 7 - 4 = 3 +//- [9,4,1,7] 最高分和最低分之间的差值是 7 - 1 = 6 +//可能的最小差值是 2 +// +// +// +// 提示: +// +// +// 1 <= k <= nums.length <= 1000 +// 0 <= nums[i] <= 10⁵ +// +// Related Topics 数组 排序 滑动窗口 👍 15 👎 0 + + +import java.util.Arrays; + +/** + * 1984 学生分数的最小差值 + * @date 2022-02-11 08:46:52 + * @author shang.liang + */ + public class MinimumDifferenceBetweenHighestAndLowestOfKScores{ + public static void main(String[] args){ + Solution soution = new MinimumDifferenceBetweenHighestAndLowestOfKScores().new Solution(); + int[] nums = new int[]{9, 4, 1, 7}; + int k = 2; + System.out.println("soution.minimumDifference(nums,k) = " + soution.minimumDifference(nums, k)); + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int minimumDifference(int[] nums, int k) { + + int res = Integer.MAX_VALUE; + int index = 0; + + Arrays.sort(nums); + + for (int i = k-1; i < nums.length; i++) { + + int tmp = nums[i] - nums[index++]; + res = Math.min(res, tmp); + + } + + return res; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode/editor/cn/doc/content/MinimumDifferenceBetweenHighestAndLowestOfKScores.md b/src/com/leetcode/editor/cn/doc/content/MinimumDifferenceBetweenHighestAndLowestOfKScores.md new file mode 100644 index 0000000..ee9f944 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/MinimumDifferenceBetweenHighestAndLowestOfKScores.md @@ -0,0 +1,39 @@ +

    给你一个 下标从 0 开始 的整数数组 nums ,其中 nums[i] 表示第 i 名学生的分数。另给你一个整数 k

    + +

    从数组中选出任意 k 名学生的分数,使这 k 个分数间 最高分最低分差值 达到 最小化

    + +

    返回可能的 最小差值

    + +

     

    + +

    示例 1:

    + +
    输入:nums = [90], k = 1
    +输出:0
    +解释:选出 1 名学生的分数,仅有 1 种方法:
    +- [90] 最高分和最低分之间的差值是 90 - 90 = 0
    +可能的最小差值是 0
    +
    + +

    示例 2:

    + +
    输入:nums = [9,4,1,7], k = 2
    +输出:2
    +解释:选出 2 名学生的分数,有 6 种方法:
    +- [9,4,1,7] 最高分和最低分之间的差值是 9 - 4 = 5
    +- [9,4,1,7] 最高分和最低分之间的差值是 9 - 1 = 8
    +- [9,4,1,7] 最高分和最低分之间的差值是 9 - 7 = 2
    +- [9,4,1,7] 最高分和最低分之间的差值是 4 - 1 = 3
    +- [9,4,1,7] 最高分和最低分之间的差值是 7 - 4 = 3
    +- [9,4,1,7] 最高分和最低分之间的差值是 7 - 1 = 6
    +可能的最小差值是 2
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= k <= nums.length <= 1000
    • +
    • 0 <= nums[i] <= 105
    • +
    +
    Related Topics
  • 数组
  • 排序
  • 滑动窗口

  • 👍 15
  • 👎 0
  • \ No newline at end of file From 636ab9de3202a717c5a9448b561fabd206c938d1 Mon Sep 17 00:00:00 2001 From: shangliang Date: Mon, 14 Feb 2022 12:44:59 +0800 Subject: [PATCH 08/85] =?UTF-8?q?540=20=E6=9C=89=E5=BA=8F=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E4=B8=AD=E7=9A=84=E5=8D=95=E4=B8=80=E5=85=83=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/SingleElementInASortedArray.java | 65 +++++++++++++++++++ .../content/SingleElementInASortedArray.md | 33 ++++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/com/leetcode/editor/cn/SingleElementInASortedArray.java create mode 100644 src/com/leetcode/editor/cn/doc/content/SingleElementInASortedArray.md diff --git a/src/com/leetcode/editor/cn/SingleElementInASortedArray.java b/src/com/leetcode/editor/cn/SingleElementInASortedArray.java new file mode 100644 index 0000000..1f96fd4 --- /dev/null +++ b/src/com/leetcode/editor/cn/SingleElementInASortedArray.java @@ -0,0 +1,65 @@ +package com.leetcode.editor.cn; + +//给你一个仅由整数组成的有序数组,其中每个元素都会出现两次,唯有一个数只会出现一次。 +// +// 请你找出并返回只出现一次的那个数。 +// +// 你设计的解决方案必须满足 O(log n) 时间复杂度和 O(1) 空间复杂度。 +// +// +// +// 示例 1: +// +// +//输入: nums = [1,1,2,3,3,4,4,8,8] +//输出: 2 +// +// +// 示例 2: +// +// +//输入: nums = [3,3,7,7,10,11,11] +//输出: 10 +// +// +// +// +// +// +// 提示: +// +// +// 1 <= nums.length <= 10⁵ +// 0 <= nums[i] <= 10⁵ +// +// Related Topics 数组 二分查找 👍 391 👎 0 + + +/** + * 540 有序数组中的单一元素 + * @date 2022-02-14 12:42:15 + * @author shang.liang + */ + public class SingleElementInASortedArray{ + public static void main(String[] args){ + Solution soution = new SingleElementInASortedArray().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int singleNonDuplicate(int[] nums) { + + int res = 0; + + for (int num : nums) { + res ^= num; + } + + return res; + + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode/editor/cn/doc/content/SingleElementInASortedArray.md b/src/com/leetcode/editor/cn/doc/content/SingleElementInASortedArray.md new file mode 100644 index 0000000..bbadc43 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/SingleElementInASortedArray.md @@ -0,0 +1,33 @@ +

    给你一个仅由整数组成的有序数组,其中每个元素都会出现两次,唯有一个数只会出现一次。

    + +

    请你找出并返回只出现一次的那个数。

    + +

    你设计的解决方案必须满足 O(log n) 时间复杂度和 O(1) 空间复杂度。

    + +

     

    + +

    示例 1:

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

    示例 2:

    + +
    +输入: nums =  [3,3,7,7,10,11,11]
    +输出: 10
    +
    + +

     

    + +

    + +

    提示:

    + +
      +
    • 1 <= nums.length <= 105
    • +
    • 0 <= nums[i] <= 105
    • +
    +
    Related Topics
  • 数组
  • 二分查找

  • 👍 391
  • 👎 0
  • \ No newline at end of file From 9f97a1c7a12d60e7501440af7c0e59b9573a6d78 Mon Sep 17 00:00:00 2001 From: shangliang Date: Tue, 15 Feb 2022 18:30:06 +0800 Subject: [PATCH 09/85] =?UTF-8?q?1380=20=E7=9F=A9=E9=98=B5=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E5=B9=B8=E8=BF=90=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/LuckyNumbersInAMatrix.java | 94 +++++++++++++++++++ .../cn/doc/content/LuckyNumbersInAMatrix.md | 43 +++++++++ 2 files changed, 137 insertions(+) create mode 100644 src/com/leetcode/editor/cn/LuckyNumbersInAMatrix.java create mode 100644 src/com/leetcode/editor/cn/doc/content/LuckyNumbersInAMatrix.md diff --git a/src/com/leetcode/editor/cn/LuckyNumbersInAMatrix.java b/src/com/leetcode/editor/cn/LuckyNumbersInAMatrix.java new file mode 100644 index 0000000..1a2f8a1 --- /dev/null +++ b/src/com/leetcode/editor/cn/LuckyNumbersInAMatrix.java @@ -0,0 +1,94 @@ +package com.leetcode.editor.cn; + +//给你一个 m * n 的矩阵,矩阵中的数字 各不相同 。请你按 任意 顺序返回矩阵中的所有幸运数。 +// +// 幸运数是指矩阵中满足同时下列两个条件的元素: +// +// +// 在同一行的所有元素中最小 +// 在同一列的所有元素中最大 +// +// +// +// +// 示例 1: +// +// 输入:matrix = [[3,7,8],[9,11,13],[15,16,17]] +//输出:[15] +//解释:15 是唯一的幸运数,因为它是其所在行中的最小值,也是所在列中的最大值。 +// +// +// 示例 2: +// +// 输入:matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]] +//输出:[12] +//解释:12 是唯一的幸运数,因为它是其所在行中的最小值,也是所在列中的最大值。 +// +// +// 示例 3: +// +// 输入:matrix = [[7,8],[1,2]] +//输出:[7] +// +// +// +// +// 提示: +// +// +// m == mat.length +// n == mat[i].length +// 1 <= n, m <= 50 +// 1 <= matrix[i][j] <= 10^5 +// 矩阵中的所有元素都是不同的 +// +// Related Topics 数组 矩阵 👍 95 👎 0 + + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * 1380 矩阵中的幸运数 + * @date 2022-02-15 17:36:20 + * @author shang.liang + */ + public class LuckyNumbersInAMatrix{ + public static void main(String[] args){ + Solution soution = new LuckyNumbersInAMatrix().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public List luckyNumbers (int[][] matrix) { + + List res = new ArrayList<>(); + + int[] dpRow = new int[matrix.length]; + Arrays.fill(dpRow, Integer.MAX_VALUE); + int[] dpCol = new int[matrix[0].length]; + + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[i].length; j++) { + dpRow[i] = Math.min(dpRow[i], matrix[i][j]); + dpCol[j] = Math.max(dpCol[j], matrix[i][j]); + } + } + + + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[0].length; j++) { + if (dpRow[i] == matrix[i][j] && dpCol[j] == matrix[i][j]) { + res.add(matrix[i][j]); + } + } + } + + return res; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode/editor/cn/doc/content/LuckyNumbersInAMatrix.md b/src/com/leetcode/editor/cn/doc/content/LuckyNumbersInAMatrix.md new file mode 100644 index 0000000..e11aac5 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/LuckyNumbersInAMatrix.md @@ -0,0 +1,43 @@ +

    给你一个 m * n 的矩阵,矩阵中的数字 各不相同 。请你按 任意 顺序返回矩阵中的所有幸运数。

    + +

    幸运数是指矩阵中满足同时下列两个条件的元素:

    + +
      +
    • 在同一行的所有元素中最小
    • +
    • 在同一列的所有元素中最大
    • +
    + +

     

    + +

    示例 1:

    + +
    输入:matrix = [[3,7,8],[9,11,13],[15,16,17]]
    +输出:[15]
    +解释:15 是唯一的幸运数,因为它是其所在行中的最小值,也是所在列中的最大值。
    +
    + +

    示例 2:

    + +
    输入:matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
    +输出:[12]
    +解释:12 是唯一的幸运数,因为它是其所在行中的最小值,也是所在列中的最大值。
    +
    + +

    示例 3:

    + +
    输入:matrix = [[7,8],[1,2]]
    +输出:[7]
    +
    + +

     

    + +

    提示:

    + +
      +
    • m == mat.length
    • +
    • n == mat[i].length
    • +
    • 1 <= n, m <= 50
    • +
    • 1 <= matrix[i][j] <= 10^5
    • +
    • 矩阵中的所有元素都是不同的
    • +
    +
    Related Topics
  • 数组
  • 矩阵

  • 👍 95
  • 👎 0
  • \ No newline at end of file From c19f30cfdcf6540cebf0b4146bda8e8ccbded429 Mon Sep 17 00:00:00 2001 From: shangliang Date: Wed, 16 Feb 2022 23:26:52 +0800 Subject: [PATCH 10/85] =?UTF-8?q?1719=20=E9=87=8D=E6=9E=84=E4=B8=80?= =?UTF-8?q?=E6=A3=B5=E6=A0=91=E7=9A=84=E6=96=B9=E6=A1=88=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/NumberOfWaysToReconstructATree.java | 94 +++++++++++++++++++ .../content/NumberOfWaysToReconstructATree.md | 64 +++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 src/com/leetcode/editor/cn/NumberOfWaysToReconstructATree.java create mode 100644 src/com/leetcode/editor/cn/doc/content/NumberOfWaysToReconstructATree.md diff --git a/src/com/leetcode/editor/cn/NumberOfWaysToReconstructATree.java b/src/com/leetcode/editor/cn/NumberOfWaysToReconstructATree.java new file mode 100644 index 0000000..4df02e7 --- /dev/null +++ b/src/com/leetcode/editor/cn/NumberOfWaysToReconstructATree.java @@ -0,0 +1,94 @@ +package com.leetcode.editor.cn; + +//给你一个数组 pairs ,其中 pairs[i] = [xi, yi] ,并且满足: +// +// +// pairs 中没有重复元素 +// xi < yi +// +// +// 令 ways 为满足下面条件的有根树的方案数: +// +// +// 树所包含的所有节点值都在 pairs 中。 +// 一个数对 [xi, yi] 出现在 pairs 中 当且仅当 xi 是 yi 的祖先或者 yi 是 xi 的祖先。 +// 注意:构造出来的树不一定是二叉树。 +// +// +// 两棵树被视为不同的方案当存在至少一个节点在两棵树中有不同的父节点。 +// +// 请你返回: +// +// +// 如果 ways == 0 ,返回 0 。 +// 如果 ways == 1 ,返回 1 。 +// 如果 ways > 1 ,返回 2 。 +// +// +// 一棵 有根树 指的是只有一个根节点的树,所有边都是从根往外的方向。 +// +// 我们称从根到一个节点路径上的任意一个节点(除去节点本身)都是该节点的 祖先 。根节点没有祖先。 +// +// +// +// 示例 1: +// +// +//输入:pairs = [[1,2],[2,3]] +//输出:1 +//解释:如上图所示,有且只有一个符合规定的有根树。 +// +// +// 示例 2: +// +// +//输入:pairs = [[1,2],[2,3],[1,3]] +//输出:2 +//解释:有多个符合规定的有根树,其中三个如上图所示。 +// +// +// 示例 3: +// +// +//输入:pairs = [[1,2],[2,3],[2,4],[1,5]] +//输出:0 +//解释:没有符合规定的有根树。 +// +// +// +// 提示: +// +// +// 1 <= pairs.length <= 10⁵ +// 1 <= xi < yi <= 500 +// pairs 中的元素互不相同。 +// +// Related Topics 树 图 拓扑排序 👍 114 👎 0 + + +/** + * 1719 重构一棵树的方案数 + * @date 2022-02-16 22:07:08 + * @author shang.liang + */ + public class NumberOfWaysToReconstructATree{ + public static void main(String[] args){ + Solution soution = new NumberOfWaysToReconstructATree().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int checkWays(int[][] pairs) { + + int res = 0; + + // 不会做,再见!!! + // 待我来日再来干掉你!!! + + return res; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode/editor/cn/doc/content/NumberOfWaysToReconstructATree.md b/src/com/leetcode/editor/cn/doc/content/NumberOfWaysToReconstructATree.md new file mode 100644 index 0000000..5b40c40 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/NumberOfWaysToReconstructATree.md @@ -0,0 +1,64 @@ +

    给你一个数组 pairs ,其中 pairs[i] = [xi, yi] ,并且满足:

    + +
      +
    • pairs 中没有重复元素
    • +
    • xi < yi
    • +
    + +

    令 ways 为满足下面条件的有根树的方案数:

    + +
      +
    • 树所包含的所有节点值都在 pairs 中。
    • +
    • 一个数对 [xi, yi] 出现在 pairs 中 当且仅当 xi 是 yi 的祖先或者 yi 是 xi 的祖先。
    • +
    • 注意:构造出来的树不一定是二叉树。
    • +
    + +

    两棵树被视为不同的方案当存在至少一个节点在两棵树中有不同的父节点。

    + +

    请你返回:

    + +
      +
    • 如果 ways == 0 ,返回 0 。
    • +
    • 如果 ways == 1 ,返回 1 。
    • +
    • 如果 ways > 1 ,返回 2 。
    • +
    + +

    一棵 有根树 指的是只有一个根节点的树,所有边都是从根往外的方向。

    + +

    我们称从根到一个节点路径上的任意一个节点(除去节点本身)都是该节点的 祖先 。根节点没有祖先。

    + +

     

    + +

    示例 1:

    + +
    +输入:pairs = [[1,2],[2,3]]
    +输出:1
    +解释:如上图所示,有且只有一个符合规定的有根树。
    +
    + +

    示例 2:

    + +
    +输入:pairs = [[1,2],[2,3],[1,3]]
    +输出:2
    +解释:有多个符合规定的有根树,其中三个如上图所示。
    +
    + +

    示例 3:

    + +
    +输入:pairs = [[1,2],[2,3],[2,4],[1,5]]
    +输出:0
    +解释:没有符合规定的有根树。
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= pairs.length <= 105
    • +
    • 1 <= xi < yi <= 500
    • +
    • pairs 中的元素互不相同。
    • +
    +
    Related Topics
  • 拓扑排序

  • 👍 114
  • 👎 0
  • \ No newline at end of file From f8998be65ddce85afea1c96e4417a712f3a10f69 Mon Sep 17 00:00:00 2001 From: shangliang Date: Sat, 19 Feb 2022 10:26:10 +0800 Subject: [PATCH 11/85] =?UTF-8?q?1791=20=E6=89=BE=E5=87=BA=E6=98=9F?= =?UTF-8?q?=E5=9E=8B=E5=9B=BE=E7=9A=84=E4=B8=AD=E5=BF=83=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/FindCenterOfStarGraph.java | 73 +++++++++++++++++++ .../cn/doc/content/FindCenterOfStarGraph.md | 34 +++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/com/leetcode/editor/cn/FindCenterOfStarGraph.java create mode 100644 src/com/leetcode/editor/cn/doc/content/FindCenterOfStarGraph.md diff --git a/src/com/leetcode/editor/cn/FindCenterOfStarGraph.java b/src/com/leetcode/editor/cn/FindCenterOfStarGraph.java new file mode 100644 index 0000000..b42cd97 --- /dev/null +++ b/src/com/leetcode/editor/cn/FindCenterOfStarGraph.java @@ -0,0 +1,73 @@ +package com.leetcode.editor.cn; + +//有一个无向的 星型 图,由 n 个编号从 1 到 n 的节点组成。星型图有一个 中心 节点,并且恰有 n - 1 条边将中心节点与其他每个节点连接起来。 +// +// 给你一个二维整数数组 edges ,其中 edges[i] = [ui, vi] 表示在节点 ui 和 vi 之间存在一条边。请你找出并返回 edges +//所表示星型图的中心节点。 +// +// +// +// 示例 1: +// +// +//输入:edges = [[1,2],[2,3],[4,2]] +//输出:2 +//解释:如上图所示,节点 2 与其他每个节点都相连,所以节点 2 是中心节点。 +// +// +// 示例 2: +// +// +//输入:edges = [[1,2],[5,1],[1,3],[1,4]] +//输出:1 +// +// +// +// +// 提示: +// +// +// 3 <= n <= 10⁵ +// edges.length == n - 1 +// edges[i].length == 2 +// 1 <= ui, vi <= n +// ui != vi +// 题目数据给出的 edges 表示一个有效的星型图 +// +// Related Topics 图 👍 23 👎 0 + + +/** + * 1791 找出星型图的中心节点 + * @date 2022-02-18 09:22:47 + * @author shang.liang + */ + public class FindCenterOfStarGraph{ + public static void main(String[] args){ + Solution soution = new FindCenterOfStarGraph().new Solution(); + int[][] edges = new int[][]{{1, 2}, {2, 3}, {4, 2}}; + System.out.println("soution.findCenter(edges) = " + soution.findCenter(edges)); + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int findCenter(int[][] edges) { + + int[] numsLine = new int[edges.length + 2]; + + for (int[] edge : edges) { + numsLine[edge[0]]++; + numsLine[edge[1]]++; + } + + for (int i = 0; i < numsLine.length; i++) { + if (numsLine[i] == edges.length) { + return i; + } + } + return 1; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode/editor/cn/doc/content/FindCenterOfStarGraph.md b/src/com/leetcode/editor/cn/doc/content/FindCenterOfStarGraph.md new file mode 100644 index 0000000..8df8558 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/FindCenterOfStarGraph.md @@ -0,0 +1,34 @@ +

    有一个无向的 星型 图,由 n 个编号从 1n 的节点组成。星型图有一个 中心 节点,并且恰有 n - 1 条边将中心节点与其他每个节点连接起来。

    + +

    给你一个二维整数数组 edges ,其中 edges[i] = [ui, vi] 表示在节点 uivi 之间存在一条边。请你找出并返回 edges 所表示星型图的中心节点。

    + +

     

    + +

    示例 1:

    + +
    +输入:edges = [[1,2],[2,3],[4,2]]
    +输出:2
    +解释:如上图所示,节点 2 与其他每个节点都相连,所以节点 2 是中心节点。
    +
    + +

    示例 2:

    + +
    +输入:edges = [[1,2],[5,1],[1,3],[1,4]]
    +输出:1
    +
    + +

     

    + +

    提示:

    + +
      +
    • 3 <= n <= 105
    • +
    • edges.length == n - 1
    • +
    • edges[i].length == 2
    • +
    • 1 <= ui, vi <= n
    • +
    • ui != vi
    • +
    • 题目数据给出的 edges 表示一个有效的星型图
    • +
    +
    Related Topics

  • 👍 23
  • 👎 0
  • \ No newline at end of file From e130172f3f99651600faebd3ee0809cf19bdf420 Mon Sep 17 00:00:00 2001 From: shangliang Date: Tue, 22 Feb 2022 22:52:49 +0800 Subject: [PATCH 12/85] =?UTF-8?q?838=20=E6=8E=A8=E5=A4=9A=E7=B1=B3?= =?UTF-8?q?=E8=AF=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/leetcode/editor/cn/PushDominoes.java | 111 ++++++++++++++++++ .../editor/cn/doc/content/PushDominoes.md | 44 +++++++ 2 files changed, 155 insertions(+) create mode 100644 src/com/leetcode/editor/cn/PushDominoes.java create mode 100644 src/com/leetcode/editor/cn/doc/content/PushDominoes.md diff --git a/src/com/leetcode/editor/cn/PushDominoes.java b/src/com/leetcode/editor/cn/PushDominoes.java new file mode 100644 index 0000000..5af8cab --- /dev/null +++ b/src/com/leetcode/editor/cn/PushDominoes.java @@ -0,0 +1,111 @@ +package com.leetcode.editor.cn; + +//n 张多米诺骨牌排成一行,将每张多米诺骨牌垂直竖立。在开始时,同时把一些多米诺骨牌向左或向右推。 +// +// 每过一秒,倒向左边的多米诺骨牌会推动其左侧相邻的多米诺骨牌。同样地,倒向右边的多米诺骨牌也会推动竖立在其右侧的相邻多米诺骨牌。 +// +// 如果一张垂直竖立的多米诺骨牌的两侧同时有多米诺骨牌倒下时,由于受力平衡, 该骨牌仍然保持不变。 +// +// 就这个问题而言,我们会认为一张正在倒下的多米诺骨牌不会对其它正在倒下或已经倒下的多米诺骨牌施加额外的力。 +// +// 给你一个字符串 dominoes 表示这一行多米诺骨牌的初始状态,其中: +// +// +// dominoes[i] = 'L',表示第 i 张多米诺骨牌被推向左侧, +// dominoes[i] = 'R',表示第 i 张多米诺骨牌被推向右侧, +// dominoes[i] = '.',表示没有推动第 i 张多米诺骨牌。 +// +// +// 返回表示最终状态的字符串。 +// +// +// 示例 1: +// +// +//输入:dominoes = "RR.L" +//输出:"RR.L" +//解释:第一张多米诺骨牌没有给第二张施加额外的力。 +// +// +// 示例 2: +// +// +//输入:dominoes = ".L.R...LR..L.." +//输出:"LL.RR.LLRRLL.." +// +// +// +// +// 提示: +// +// +// n == dominoes.length +// 1 <= n <= 10⁵ +// dominoes[i] 为 'L'、'R' 或 '.' +// +// Related Topics 双指针 字符串 动态规划 👍 242 👎 0 + + +/** + * 838 推多米诺 + * @date 2022-02-21 23:19:28 + * @author shang.liang + */ + public class PushDominoes{ + public static void main(String[] args){ + Solution soution = new PushDominoes().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public String pushDominoes(String dominoes) { + + dominoes = "L" + dominoes + "R"; + + StringBuilder res = new StringBuilder(); + + + int l = 0; + + + for (int r = 1; r < dominoes.length(); r++) { + + if (dominoes.charAt(r) == '.') { + continue; + } + + int mid = r - l - 1; + + if (l != 0) { + res.append(dominoes.charAt(l)); + } + + if (dominoes.charAt(r) == dominoes.charAt(l)) { + for (int i = 0; i < mid; i++) { + res.append(dominoes.charAt(r)); + } + } else if (dominoes.charAt(l) == 'L' && dominoes.charAt(r) == 'R') { + for (int i = 0; i < mid; i++) { + res.append("."); + } + } else { + for (int i = 0; i < mid / 2; i++) { + res.append('R'); + } + if (mid % 2 == 1) { + res.append("."); + } + for (int i = 0; i < mid / 2; i++) { + res.append("L"); + } + } + l = r; + } + + return res.toString(); + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode/editor/cn/doc/content/PushDominoes.md b/src/com/leetcode/editor/cn/doc/content/PushDominoes.md new file mode 100644 index 0000000..0ca4c77 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/PushDominoes.md @@ -0,0 +1,44 @@ +

    n 张多米诺骨牌排成一行,将每张多米诺骨牌垂直竖立。在开始时,同时把一些多米诺骨牌向左或向右推。

    + +

    每过一秒,倒向左边的多米诺骨牌会推动其左侧相邻的多米诺骨牌。同样地,倒向右边的多米诺骨牌也会推动竖立在其右侧的相邻多米诺骨牌。

    + +

    如果一张垂直竖立的多米诺骨牌的两侧同时有多米诺骨牌倒下时,由于受力平衡, 该骨牌仍然保持不变。

    + +

    就这个问题而言,我们会认为一张正在倒下的多米诺骨牌不会对其它正在倒下或已经倒下的多米诺骨牌施加额外的力。

    + +

    给你一个字符串 dominoes 表示这一行多米诺骨牌的初始状态,其中:

    + +
      +
    • dominoes[i] = 'L',表示第 i 张多米诺骨牌被推向左侧,
    • +
    • dominoes[i] = 'R',表示第 i 张多米诺骨牌被推向右侧,
    • +
    • dominoes[i] = '.',表示没有推动第 i 张多米诺骨牌。
    • +
    + +

    返回表示最终状态的字符串。

    +  + +

    示例 1:

    + +
    +输入:dominoes = "RR.L"
    +输出:"RR.L"
    +解释:第一张多米诺骨牌没有给第二张施加额外的力。
    +
    + +

    示例 2:

    + +
    +输入:dominoes = ".L.R...LR..L.."
    +输出:"LL.RR.LLRRLL.."
    +
    + +

     

    + +

    提示:

    + +
      +
    • n == dominoes.length
    • +
    • 1 <= n <= 105
    • +
    • dominoes[i]'L''R''.'
    • +
    +
    Related Topics
  • 双指针
  • 字符串
  • 动态规划

  • 👍 242
  • 👎 0
  • \ No newline at end of file From 34ba1f923e761335ed90cd57b90657be55a6ffa8 Mon Sep 17 00:00:00 2001 From: shangliang Date: Wed, 23 Feb 2022 23:20:17 +0800 Subject: [PATCH 13/85] =?UTF-8?q?917=20=E4=BB=85=E4=BB=85=E5=8F=8D?= =?UTF-8?q?=E8=BD=AC=E5=AD=97=E6=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/ReverseOnlyLetters.java | 103 ++++++++++++++++++ .../cn/doc/content/ReverseOnlyLetters.md | 51 +++++++++ 2 files changed, 154 insertions(+) create mode 100644 src/com/leetcode/editor/cn/ReverseOnlyLetters.java create mode 100644 src/com/leetcode/editor/cn/doc/content/ReverseOnlyLetters.md diff --git a/src/com/leetcode/editor/cn/ReverseOnlyLetters.java b/src/com/leetcode/editor/cn/ReverseOnlyLetters.java new file mode 100644 index 0000000..b58037e --- /dev/null +++ b/src/com/leetcode/editor/cn/ReverseOnlyLetters.java @@ -0,0 +1,103 @@ +package com.leetcode.editor.cn; + +//给你一个字符串 s ,根据下述规则反转字符串: +// +// +// 所有非英文字母保留在原有位置。 +// 所有英文字母(小写或大写)位置反转。 +// +// +// 返回反转后的 s 。 +// +// +// +// +// +// +// 示例 1: +// +// +//输入:s = "ab-cd" +//输出:"dc-ba" +// +// +// +// +// +// 示例 2: +// +// +//输入:s = "a-bC-dEf-ghIj" +//输出:"j-Ih-gfE-dCba" +// +// +// +// +// +// 示例 3: +// +// +//输入:s = "Test1ng-Leet=code-Q!" +//输出:"Qedo1ct-eeLg=ntse-T!" +// +// +// +// +// 提示 +// +// +// 1 <= s.length <= 100 +// s 仅由 ASCII 值在范围 [33, 122] 的字符组成 +// s 不含 '\"' 或 '\\' +// +// Related Topics 双指针 字符串 👍 155 👎 0 + + +/** + * 917 仅仅反转字母 + * @date 2022-02-23 22:54:20 + * @author shang.liang + */ + public class ReverseOnlyLetters{ + public static void main(String[] args){ + Solution soution = new ReverseOnlyLetters().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public String reverseOnlyLetters(String s) { + + int left = 0; + int right = s.length() - 1; + + + char[] charArray = s.toCharArray(); + + + while (left < right) { + + if (!Character.isLetter(charArray[left])) { + left++; + continue; + } + if (!Character.isLetter(charArray[right])) { + right--; + continue; + } + + char temp = charArray[left]; + charArray[left] = charArray[right]; + charArray[right] = temp; + + left++; + right--; + } + + return new String(charArray); + + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode/editor/cn/doc/content/ReverseOnlyLetters.md b/src/com/leetcode/editor/cn/doc/content/ReverseOnlyLetters.md new file mode 100644 index 0000000..746480b --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/ReverseOnlyLetters.md @@ -0,0 +1,51 @@ +

    给你一个字符串 s ,根据下述规则反转字符串:

    + +
      +
    • 所有非英文字母保留在原有位置。
    • +
    • 所有英文字母(小写或大写)位置反转。
    • +
    + +

    返回反转后的 s

    + +

     

    + +
      +
    + +

    示例 1:

    + +
    +输入:s = "ab-cd"
    +输出:"dc-ba"
    +
    + +
      +
    + +

    示例 2:

    + +
    +输入:s = "a-bC-dEf-ghIj"
    +输出:"j-Ih-gfE-dCba"
    +
    + +
      +
    + +

    示例 3:

    + +
    +输入:s = "Test1ng-Leet=code-Q!"
    +输出:"Qedo1ct-eeLg=ntse-T!"
    +
    + +

     

    + +

    提示

    + +
      +
    • 1 <= s.length <= 100
    • +
    • s 仅由 ASCII 值在范围 [33, 122] 的字符组成
    • +
    • s 不含 '\"''\\'
    • +
    +
    Related Topics
  • 双指针
  • 字符串

  • 👍 155
  • 👎 0
  • \ No newline at end of file From b891fef305ad871316d3f79cd6d17be8245c58f3 Mon Sep 17 00:00:00 2001 From: shangliang Date: Fri, 25 Feb 2022 23:06:55 +0800 Subject: [PATCH 14/85] =?UTF-8?q?537=20=E5=A4=8D=E6=95=B0=E4=B9=98?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/ComplexNumberMultiplication.java | 72 +++++++++++++++++++ .../content/ComplexNumberMultiplication.md | 36 ++++++++++ 2 files changed, 108 insertions(+) create mode 100644 src/com/leetcode/editor/cn/ComplexNumberMultiplication.java create mode 100644 src/com/leetcode/editor/cn/doc/content/ComplexNumberMultiplication.md diff --git a/src/com/leetcode/editor/cn/ComplexNumberMultiplication.java b/src/com/leetcode/editor/cn/ComplexNumberMultiplication.java new file mode 100644 index 0000000..07d7b93 --- /dev/null +++ b/src/com/leetcode/editor/cn/ComplexNumberMultiplication.java @@ -0,0 +1,72 @@ +package com.leetcode.editor.cn; + +//复数 可以用字符串表示,遵循 "实部+虚部i" 的形式,并满足下述条件: +// +// +// 实部 是一个整数,取值范围是 [-100, 100] +// 虚部 也是一个整数,取值范围是 [-100, 100] +// i² == -1 +// +// +// 给你两个字符串表示的复数 num1 和 num2 ,请你遵循复数表示形式,返回表示它们乘积的字符串。 +// +// +// +// 示例 1: +// +// +//输入:num1 = "1+1i", num2 = "1+1i" +//输出:"0+2i" +//解释:(1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i ,你需要将它转换为 0+2i 的形式。 +// +// +// 示例 2: +// +// +//输入:num1 = "1+-1i", num2 = "1+-1i" +//输出:"0+-2i" +//解释:(1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i ,你需要将它转换为 0+-2i 的形式。 +// +// +// +// +// 提示: +// +// +// num1 和 num2 都是有效的复数表示。 +// +// Related Topics 数学 字符串 模拟 👍 123 👎 0 + + +/** + * 537 复数乘法 + * + * @author shang.liang + * @date 2022-02-25 22:53:28 + */ +public class ComplexNumberMultiplication { + public static void main(String[] args) { + Solution soution = new ComplexNumberMultiplication().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public String complexNumberMultiply(String num1, String num2) { + + String[] str1 = num1.split("\\+|i"); + String[] str2 = num2.split("\\+|i"); + + // a + bi , c + di + int a = Integer.parseInt(str1[0]); + int b = Integer.parseInt(str1[1]); + int c = Integer.parseInt(str2[0]); + int d = Integer.parseInt(str2[1]); + + return (a * c - b * d) + "+" + (a * d + b * c) + "i"; + } + + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/com/leetcode/editor/cn/doc/content/ComplexNumberMultiplication.md b/src/com/leetcode/editor/cn/doc/content/ComplexNumberMultiplication.md new file mode 100644 index 0000000..7eb58d3 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/ComplexNumberMultiplication.md @@ -0,0 +1,36 @@ +

    复数 可以用字符串表示,遵循 "实部+虚部i" 的形式,并满足下述条件:

    + +
      +
    • 实部 是一个整数,取值范围是 [-100, 100]
    • +
    • 虚部 也是一个整数,取值范围是 [-100, 100]
    • +
    • i2 == -1
    • +
    + +

    给你两个字符串表示的复数 num1num2 ,请你遵循复数表示形式,返回表示它们乘积的字符串。

    + +

     

    + +

    示例 1:

    + +
    +输入:num1 = "1+1i", num2 = "1+1i"
    +输出:"0+2i"
    +解释:(1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i ,你需要将它转换为 0+2i 的形式。
    +
    + +

    示例 2:

    + +
    +输入:num1 = "1+-1i", num2 = "1+-1i"
    +输出:"0+-2i"
    +解释:(1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i ,你需要将它转换为 0+-2i 的形式。 
    +
    + +

     

    + +

    提示:

    + +
      +
    • num1num2 都是有效的复数表示。
    • +
    +
    Related Topics
  • 数学
  • 字符串
  • 模拟

  • 👍 123
  • 👎 0
  • \ No newline at end of file From 2fe2dbf00e213a98b92784c6553d8a932b2a7e2f Mon Sep 17 00:00:00 2001 From: shangliang Date: Fri, 25 Feb 2022 23:10:46 +0800 Subject: [PATCH 15/85] =?UTF-8?q?537=20=E5=A4=8D=E6=95=B0=E4=B9=98?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/leetcode/editor/cn/ComplexNumberMultiplication.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/leetcode/editor/cn/ComplexNumberMultiplication.java b/src/com/leetcode/editor/cn/ComplexNumberMultiplication.java index 07d7b93..e321ec9 100644 --- a/src/com/leetcode/editor/cn/ComplexNumberMultiplication.java +++ b/src/com/leetcode/editor/cn/ComplexNumberMultiplication.java @@ -54,8 +54,8 @@ public static void main(String[] args) { class Solution { public String complexNumberMultiply(String num1, String num2) { - String[] str1 = num1.split("\\+|i"); - String[] str2 = num2.split("\\+|i"); + String[] str1 = num1.split("[+i]"); + String[] str2 = num2.split("[+i]"); // a + bi , c + di int a = Integer.parseInt(str1[0]); From bb9e3f71597046c1141d77b802a503afd32b69fe Mon Sep 17 00:00:00 2001 From: shangliang Date: Fri, 25 Feb 2022 23:17:17 +0800 Subject: [PATCH 16/85] =?UTF-8?q?537=20=E5=A4=8D=E6=95=B0=E4=B9=98?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/leetcode/editor/cn/ComplexNumberMultiplication.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/com/leetcode/editor/cn/ComplexNumberMultiplication.java b/src/com/leetcode/editor/cn/ComplexNumberMultiplication.java index e321ec9..e6c905c 100644 --- a/src/com/leetcode/editor/cn/ComplexNumberMultiplication.java +++ b/src/com/leetcode/editor/cn/ComplexNumberMultiplication.java @@ -47,14 +47,16 @@ public class ComplexNumberMultiplication { public static void main(String[] args) { Solution soution = new ComplexNumberMultiplication().new Solution(); - + String nums1 = "1+1i"; + String nums2 = "1+1i"; + System.out.println("soution.complexNumberMultiply(nums1,nums2) = " + soution.complexNumberMultiply(nums1, nums2)); } //leetcode submit region begin(Prohibit modification and deletion) class Solution { public String complexNumberMultiply(String num1, String num2) { - String[] str1 = num1.split("[+i]"); + String[] str1 = num1.split("\\+|i"); String[] str2 = num2.split("[+i]"); // a + bi , c + di From c6a84c2bf8c3460c50c92d5808f9b4aa5df5191b Mon Sep 17 00:00:00 2001 From: shangliang Date: Sat, 26 Feb 2022 18:11:05 +0800 Subject: [PATCH 17/85] =?UTF-8?q?2016=20=E5=A2=9E=E9=87=8F=E5=85=83?= =?UTF-8?q?=E7=B4=A0=E4=B9=8B=E9=97=B4=E7=9A=84=E6=9C=80=E5=A4=A7=E5=B7=AE?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...umDifferenceBetweenIncreasingElements.java | 83 +++++++++++++++++++ ...imumDifferenceBetweenIncreasingElements.md | 41 +++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/com/leetcode/editor/cn/MaximumDifferenceBetweenIncreasingElements.java create mode 100644 src/com/leetcode/editor/cn/doc/content/MaximumDifferenceBetweenIncreasingElements.md diff --git a/src/com/leetcode/editor/cn/MaximumDifferenceBetweenIncreasingElements.java b/src/com/leetcode/editor/cn/MaximumDifferenceBetweenIncreasingElements.java new file mode 100644 index 0000000..2a131ae --- /dev/null +++ b/src/com/leetcode/editor/cn/MaximumDifferenceBetweenIncreasingElements.java @@ -0,0 +1,83 @@ +package com.leetcode.editor.cn; + +//给你一个下标从 0 开始的整数数组 nums ,该数组的大小为 n ,请你计算 nums[j] - nums[i] 能求得的 最大差值 ,其中 0 <= +//i < j < n 且 nums[i] < nums[j] 。 +// +// 返回 最大差值 。如果不存在满足要求的 i 和 j ,返回 -1 。 +// +// +// +// 示例 1: +// +// 输入:nums = [7,1,5,4] +//输出:4 +//解释: +//最大差值出现在 i = 1 且 j = 2 时,nums[j] - nums[i] = 5 - 1 = 4 。 +//注意,尽管 i = 1 且 j = 0 时 ,nums[j] - nums[i] = 7 - 1 = 6 > 4 ,但 i > j 不满足题面要求,所以 6 +// 不是有效的答案。 +// +// +// 示例 2: +// +// 输入:nums = [9,4,3,2] +//输出:-1 +//解释: +//不存在同时满足 i < j 和 nums[i] < nums[j] 这两个条件的 i, j 组合。 +// +// +// 示例 3: +// +// 输入:nums = [1,5,2,10] +//输出:9 +//解释: +//最大差值出现在 i = 0 且 j = 3 时,nums[j] - nums[i] = 10 - 1 = 9 。 +// +// +// +// +// 提示: +// +// +// n == nums.length +// 2 <= n <= 1000 +// 1 <= nums[i] <= 10⁹ +// +// Related Topics 数组 👍 63 👎 0 + + +/** + * 2016 增量元素之间的最大差值 + * + * @author shang.liang + * @date 2022-02-26 18:06:16 + */ +public class MaximumDifferenceBetweenIncreasingElements { + public static void main(String[] args) { + Solution soution = new MaximumDifferenceBetweenIncreasingElements().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int maximumDifference(int[] nums) { + + int min = nums[0]; + int ans = -1; + + for (int i = 1; i < nums.length; i++) { + + if (nums[i] > min) { + ans = Math.max(ans, nums[i] - min); + } else { + min = nums[i]; + } + + } + + return ans; + + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/com/leetcode/editor/cn/doc/content/MaximumDifferenceBetweenIncreasingElements.md b/src/com/leetcode/editor/cn/doc/content/MaximumDifferenceBetweenIncreasingElements.md new file mode 100644 index 0000000..df1dfcf --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/MaximumDifferenceBetweenIncreasingElements.md @@ -0,0 +1,41 @@ +

    给你一个下标从 0 开始的整数数组 nums ,该数组的大小为 n ,请你计算 nums[j] - nums[i] 能求得的 最大差值 ,其中 0 <= i < j < nnums[i] < nums[j]

    + +

    返回 最大差值 。如果不存在满足要求的 ij ,返回 -1

    + +

     

    + +

    示例 1:

    + +
    输入:nums = [7,1,5,4]
    +输出:4
    +解释:
    +最大差值出现在 i = 1 且 j = 2 时,nums[j] - nums[i] = 5 - 1 = 4 。
    +注意,尽管 i = 1 且 j = 0 时 ,nums[j] - nums[i] = 7 - 1 = 6 > 4 ,但 i > j 不满足题面要求,所以 6 不是有效的答案。
    +
    + +

    示例 2:

    + +
    输入:nums = [9,4,3,2]
    +输出:-1
    +解释:
    +不存在同时满足 i < j 和 nums[i] < nums[j] 这两个条件的 i, j 组合。
    +
    + +

    示例 3:

    + +
    输入:nums = [1,5,2,10]
    +输出:9
    +解释:
    +最大差值出现在 i = 0 且 j = 3 时,nums[j] - nums[i] = 10 - 1 = 9 。
    +
    + +

     

    + +

    提示:

    + +
      +
    • n == nums.length
    • +
    • 2 <= n <= 1000
    • +
    • 1 <= nums[i] <= 109
    • +
    +
    Related Topics
  • 数组

  • 👍 63
  • 👎 0
  • \ No newline at end of file From ee2b464ac761cdf35901fd4cd2391f0e227f0699 Mon Sep 17 00:00:00 2001 From: shangliang Date: Sun, 27 Feb 2022 11:00:32 +0800 Subject: [PATCH 18/85] =?UTF-8?q?553=20=E6=9C=80=E4=BC=98=E9=99=A4?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/OptimalDivision.java | 76 +++++++++++++++++++ .../editor/cn/doc/content/OptimalDivision.md | 29 +++++++ 2 files changed, 105 insertions(+) create mode 100644 src/com/leetcode/editor/cn/OptimalDivision.java create mode 100644 src/com/leetcode/editor/cn/doc/content/OptimalDivision.md diff --git a/src/com/leetcode/editor/cn/OptimalDivision.java b/src/com/leetcode/editor/cn/OptimalDivision.java new file mode 100644 index 0000000..ba77c29 --- /dev/null +++ b/src/com/leetcode/editor/cn/OptimalDivision.java @@ -0,0 +1,76 @@ +package com.leetcode.editor.cn; + +//给定一组正整数,相邻的整数之间将会进行浮点除法操作。例如, [2,3,4] -> 2 / 3 / 4 。 +// +// 但是,你可以在任意位置添加任意数目的括号,来改变算数的优先级。你需要找出怎么添加括号,才能得到最大的结果,并且返回相应的字符串格式的表达式。你的表达式不应 +//该含有冗余的括号。 +// +// 示例: +// +// +//输入: [1000,100,10,2] +//输出: "1000/(100/10/2)" +//解释: +//1000/(100/10/2) = 1000/((100/10)/2) = 200 +//但是,以下加粗的括号 "1000/((100/10)/2)" 是冗余的, +//因为他们并不影响操作的优先级,所以你需要返回 "1000/(100/10/2)"。 +// +//其他用例: +//1000/(100/10)/2 = 50 +//1000/(100/(10/2)) = 50 +//1000/100/10/2 = 0.5 +//1000/100/(10/2) = 2 +// +// +// 说明: +// +// +// 输入数组的长度在 [1, 10] 之间。 +// 数组中每个元素的大小都在 [2, 1000] 之间。 +// 每个测试用例只有一个最优除法解。 +// +// Related Topics 数组 数学 动态规划 👍 117 👎 0 + + +/** + * 553 最优除法 + * @date 2022-02-27 10:31:05 + * @author shang.liang + */ + public class OptimalDivision{ + public static void main(String[] args){ + Solution soution = new OptimalDivision().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public String optimalDivision(int[] nums) { + + int n = nums.length; + + if (n == 1) { + return String.valueOf(nums[0]); + } + + if (n == 2) { + return nums[0] + "/" + nums[1]; + } + + StringBuilder sb = new StringBuilder(); + + sb.append(nums[0]).append("/("); + sb.append(nums[1]); + + for (int i = 2; i < nums.length; i++) { + sb.append("/").append(nums[i]); + } + + sb.append(")"); + + return sb.toString(); + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode/editor/cn/doc/content/OptimalDivision.md b/src/com/leetcode/editor/cn/doc/content/OptimalDivision.md new file mode 100644 index 0000000..2532c0c --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/OptimalDivision.md @@ -0,0 +1,29 @@ +

    给定一组正整数,相邻的整数之间将会进行浮点除法操作。例如, [2,3,4] -> 2 / 3 / 4 。

    + +

    但是,你可以在任意位置添加任意数目的括号,来改变算数的优先级。你需要找出怎么添加括号,才能得到最大的结果,并且返回相应的字符串格式的表达式。你的表达式不应该含有冗余的括号。

    + +

    示例:

    + +
    +输入: [1000,100,10,2]
    +输出: "1000/(100/10/2)"
    +解释:
    +1000/(100/10/2) = 1000/((100/10)/2) = 200
    +但是,以下加粗的括号 "1000/((100/10)/2)" 是冗余的,
    +因为他们并不影响操作的优先级,所以你需要返回 "1000/(100/10/2)"。
    +
    +其他用例:
    +1000/(100/10)/2 = 50
    +1000/(100/(10/2)) = 50
    +1000/100/10/2 = 0.5
    +1000/100/(10/2) = 2
    +
    + +

    说明:

    + +
      +
    1. 输入数组的长度在 [1, 10] 之间。
    2. +
    3. 数组中每个元素的大小都在 [2, 1000] 之间。
    4. +
    5. 每个测试用例只有一个最优除法解。
    6. +
    +
    Related Topics
  • 数组
  • 数学
  • 动态规划

  • 👍 117
  • 👎 0
  • \ No newline at end of file From 40899bf6c22c2ff0ee488a7593de031ceb610efb Mon Sep 17 00:00:00 2001 From: shangliang Date: Wed, 2 Mar 2022 09:42:16 +0800 Subject: [PATCH 19/85] =?UTF-8?q?6=20Z=20=E5=AD=97=E5=BD=A2=E5=8F=98?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/ZigzagConversion.java | 119 ++++++++++++++++++ .../editor/cn/doc/content/ZigzagConversion.md | 53 ++++++++ 2 files changed, 172 insertions(+) create mode 100644 src/com/leetcode/editor/cn/ZigzagConversion.java create mode 100644 src/com/leetcode/editor/cn/doc/content/ZigzagConversion.md diff --git a/src/com/leetcode/editor/cn/ZigzagConversion.java b/src/com/leetcode/editor/cn/ZigzagConversion.java new file mode 100644 index 0000000..6ecc38a --- /dev/null +++ b/src/com/leetcode/editor/cn/ZigzagConversion.java @@ -0,0 +1,119 @@ +package com.leetcode.editor.cn; + +//将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。 +// +// 比如输入字符串为 "PAYPALISHIRING" 行数为 3 时,排列如下: +// +// +//P A H N +//A P L S I I G +//Y I R +// +// 之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"PAHNAPLSIIGYIR"。 +// +// 请你实现这个将字符串进行指定行数变换的函数: +// +// +//string convert(string s, int numRows); +// +// +// +// 示例 1: +// +// +//输入:s = "PAYPALISHIRING", numRows = 3 +//输出:"PAHNAPLSIIGYIR" +// +//示例 2: +// +// +//输入:s = "PAYPALISHIRING", numRows = 4 +//输出:"PINALSIGYAHRPI" +//解释: +//P I N +//A L S I G +//Y A H R +//P I +// +// +// 示例 3: +// +// +//输入:s = "A", numRows = 1 +//输出:"A" +// +// +// +// +// 提示: +// +// +// 1 <= s.length <= 1000 +// s 由英文字母(小写和大写)、',' 和 '.' 组成 +// 1 <= numRows <= 1000 +// +// Related Topics 字符串 👍 1569 👎 0 + + +/** + * 6 Z 字形变换 + * @date 2022-03-01 22:26:00 + * @author shang.liang + */ + public class ZigzagConversion{ + public static void main(String[] args){ + Solution soution = new ZigzagConversion().new Solution(); + String s = "AB"; + int numRows = 14; + System.out.println("soution.convert(s, numRows) = " + soution.convert(s, numRows)); + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public String convert(String s, int numRows) { + + if (numRows > s.length() || numRows == 1) { + return s; + } + + StringBuilder[] sb = new StringBuilder[numRows]; + + for (int i = 0; i < numRows; i++) { + sb[i] = new StringBuilder(); + } + + int index = 0; + boolean down = true; + for (int i = 0; i < s.length(); i++) { + + sb[index].append(s.charAt(i)); + + if (down) { + if (index < numRows - 1) { + index++; + } else { + index--; + down = false; + } + } else { + + if (index > 0) { + index--; + } else { + index++; + down = true; + } + + } + } + + for (int i = 1; i < sb.length; i++) { + sb[0].append(sb[i]); + } + + return sb[0].toString(); + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode/editor/cn/doc/content/ZigzagConversion.md b/src/com/leetcode/editor/cn/doc/content/ZigzagConversion.md new file mode 100644 index 0000000..8955775 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/ZigzagConversion.md @@ -0,0 +1,53 @@ +

    将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。

    + +

    比如输入字符串为 "PAYPALISHIRING" 行数为 3 时,排列如下:

    + +
    +P   A   H   N
    +A P L S I I G
    +Y   I   R
    + +

    之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"PAHNAPLSIIGYIR"

    + +

    请你实现这个将字符串进行指定行数变换的函数:

    + +
    +string convert(string s, int numRows);
    + +

     

    + +

    示例 1:

    + +
    +输入:s = "PAYPALISHIRING", numRows = 3
    +输出:"PAHNAPLSIIGYIR"
    +
    +示例 2: + +
    +输入:s = "PAYPALISHIRING", numRows = 4
    +输出:"PINALSIGYAHRPI"
    +解释:
    +P     I    N
    +A   L S  I G
    +Y A   H R
    +P     I
    +
    + +

    示例 3:

    + +
    +输入:s = "A", numRows = 1
    +输出:"A"
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= s.length <= 1000
    • +
    • s 由英文字母(小写和大写)、',''.' 组成
    • +
    • 1 <= numRows <= 1000
    • +
    +
    Related Topics
  • 字符串

  • 👍 1569
  • 👎 0
  • \ No newline at end of file From ae5ec1f3eda993ed6540edb517dc717249121285 Mon Sep 17 00:00:00 2001 From: shangliang Date: Thu, 3 Mar 2022 09:51:24 +0800 Subject: [PATCH 20/85] =?UTF-8?q?258=20=E5=90=84=E4=BD=8D=E7=9B=B8?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/leetcode/editor/cn/AddDigits.java | 69 +++++++++++++++++++ .../editor/cn/doc/content/AddDigits.md | 33 +++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/com/leetcode/editor/cn/AddDigits.java create mode 100644 src/com/leetcode/editor/cn/doc/content/AddDigits.md diff --git a/src/com/leetcode/editor/cn/AddDigits.java b/src/com/leetcode/editor/cn/AddDigits.java new file mode 100644 index 0000000..f197763 --- /dev/null +++ b/src/com/leetcode/editor/cn/AddDigits.java @@ -0,0 +1,69 @@ +package com.leetcode.editor.cn; + +//给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。返回这个结果。 +// +// +// +// 示例 1: +// +// +//输入: num = 38 +//输出: 2 +//解释: 各位相加的过程为: +//38 --> 3 + 8 --> 11 +//11 --> 1 + 1 --> 2 +//由于 2 是一位数,所以返回 2。 +// +// +// 示例 1: +// +// +//输入: num = 0 +//输出: 0 +// +// +// +// 提示: +// +// +// 0 <= num <= 2³¹ - 1 +// +// +// +// +// 进阶:你可以不使用循环或者递归,在 O(1) 时间复杂度内解决这个问题吗? +// Related Topics 数学 数论 模拟 👍 427 👎 0 + + +/** + * 258 各位相加 + * @date 2022-03-03 09:46:57 + * @author shang.liang + */ + public class AddDigits{ + public static void main(String[] args){ + Solution soution = new AddDigits().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int addDigits(int num) { + + if (num < 10) { + return num; + } + + int x = 0; + + while (num != 0) { + x += num % 10; + num = num / 10; + } + + return addDigits(x); + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode/editor/cn/doc/content/AddDigits.md b/src/com/leetcode/editor/cn/doc/content/AddDigits.md new file mode 100644 index 0000000..f22dca7 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/AddDigits.md @@ -0,0 +1,33 @@ +

    给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。返回这个结果。

    + +

     

    + +

    示例 1:

    + +
    +输入: num = 38
    +输出: 2 
    +解释: 各位相加的过程为:
    +38 --> 3 + 8 --> 11
    +11 --> 1 + 1 --> 2
    +由于 2 是一位数,所以返回 2。
    +
    + +

    示例 1:

    + +
    +输入: num = 0
    +输出: 0
    + +

     

    + +

    提示:

    + +
      +
    • 0 <= num <= 231 - 1
    • +
    + +

     

    + +

    进阶:你可以不使用循环或者递归,在 O(1) 时间复杂度内解决这个问题吗?

    +
    Related Topics
  • 数学
  • 数论
  • 模拟

  • 👍 427
  • 👎 0
  • \ No newline at end of file From b6c4c717b404c1c5f06a9afa34342fc8174c7972 Mon Sep 17 00:00:00 2001 From: shangliang Date: Fri, 4 Mar 2022 23:05:11 +0800 Subject: [PATCH 21/85] =?UTF-8?q?969=20=E7=85=8E=E9=A5=BC=E6=8E=92?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/PancakeSorting.java | 116 ++++++++++++++++++ .../editor/cn/doc/content/PancakeSorting.md | 49 ++++++++ 2 files changed, 165 insertions(+) create mode 100644 src/com/leetcode/editor/cn/PancakeSorting.java create mode 100644 src/com/leetcode/editor/cn/doc/content/PancakeSorting.md diff --git a/src/com/leetcode/editor/cn/PancakeSorting.java b/src/com/leetcode/editor/cn/PancakeSorting.java new file mode 100644 index 0000000..2f93450 --- /dev/null +++ b/src/com/leetcode/editor/cn/PancakeSorting.java @@ -0,0 +1,116 @@ +package com.leetcode.editor.cn; + +//给你一个整数数组 arr ,请使用 煎饼翻转 完成对数组的排序。 +// +// 一次煎饼翻转的执行过程如下: +// +// +// 选择一个整数 k ,1 <= k <= arr.length +// 反转子数组 arr[0...k-1](下标从 0 开始) +// +// +// 例如,arr = [3,2,1,4] ,选择 k = 3 进行一次煎饼翻转,反转子数组 [3,2,1] ,得到 arr = [1,2,3,4] 。 +// +// 以数组形式返回能使 arr 有序的煎饼翻转操作所对应的 k 值序列。任何将数组排序且翻转次数在 10 * arr.length 范围内的有效答案都将被判断 +//为正确。 +// +// +// +// 示例 1: +// +// +//输入:[3,2,4,1] +//输出:[4,2,4,3] +//解释: +//我们执行 4 次煎饼翻转,k 值分别为 4,2,4,和 3。 +//初始状态 arr = [3, 2, 4, 1] +//第一次翻转后(k = 4):arr = [1, 4, 2, 3] +//第二次翻转后(k = 2):arr = [4, 1, 2, 3] +//第三次翻转后(k = 4):arr = [3, 2, 1, 4] +//第四次翻转后(k = 3):arr = [1, 2, 3, 4],此时已完成排序。 +// +// +// 示例 2: +// +// +//输入:[1,2,3] +//输出:[] +//解释: +//输入已经排序,因此不需要翻转任何内容。 +//请注意,其他可能的答案,如 [3,3] ,也将被判断为正确。 +// +// +// +// +// 提示: +// +// +// 1 <= arr.length <= 100 +// 1 <= arr[i] <= arr.length +// arr 中的所有整数互不相同(即,arr 是从 1 到 arr.length 整数的一个排列) +// +// Related Topics 贪心 数组 双指针 排序 👍 231 👎 0 + + +import java.util.ArrayList; +import java.util.List; + +/** + * 969 煎饼排序 + * + * @author shang.liang + * @date 2022-02-19 20:50:44 + */ +public class PancakeSorting { + public static void main(String[] args) { + Solution soution = new PancakeSorting().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public List pancakeSort(int[] arr) { + + List resList = new ArrayList<>(); + + for (int i = arr.length; i > 1; i--) { + + int index = 0; + + for (int j = 0; j < i; j++) { + if (arr[j] >= arr[index]) { + index = j; + } + } + + if (index == i - 1) { + continue; + } + + swap(arr, index); + swap(arr, i - 1); + resList.add(index + 1); + resList.add(i); + } + + return resList; + } + + public void swap(int[] arr, int n) { + + int index = 0; + int temp = 0; + + while (index < n) { + temp = arr[index]; + arr[index] = arr[n]; + arr[n] = temp; + n--; + index++; + } + + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/com/leetcode/editor/cn/doc/content/PancakeSorting.md b/src/com/leetcode/editor/cn/doc/content/PancakeSorting.md new file mode 100644 index 0000000..d2b6b31 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/PancakeSorting.md @@ -0,0 +1,49 @@ +

    给你一个整数数组 arr ,请使用 煎饼翻转 完成对数组的排序。

    + +

    一次煎饼翻转的执行过程如下:

    + +
      +
    • 选择一个整数 k1 <= k <= arr.length
    • +
    • 反转子数组 arr[0...k-1]下标从 0 开始
    • +
    + +

    例如,arr = [3,2,1,4] ,选择 k = 3 进行一次煎饼翻转,反转子数组 [3,2,1] ,得到 arr = [1,2,3,4]

    + +

    以数组形式返回能使 arr 有序的煎饼翻转操作所对应的 k 值序列。任何将数组排序且翻转次数在 10 * arr.length 范围内的有效答案都将被判断为正确。

    + +

     

    + +

    示例 1:

    + +
    +输入:[3,2,4,1]
    +输出:[4,2,4,3]
    +解释:
    +我们执行 4 次煎饼翻转,k 值分别为 4,2,4,和 3。
    +初始状态 arr = [3, 2, 4, 1]
    +第一次翻转后(k = 4):arr = [1, 4, 2, 3]
    +第二次翻转后(k = 2):arr = [4, 1, 2, 3]
    +第三次翻转后(k = 4):arr = [3, 2, 1, 4]
    +第四次翻转后(k = 3):arr = [1, 2, 3, 4],此时已完成排序。 
    +
    + +

    示例 2:

    + +
    +输入:[1,2,3]
    +输出:[]
    +解释:
    +输入已经排序,因此不需要翻转任何内容。
    +请注意,其他可能的答案,如 [3,3] ,也将被判断为正确。
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= arr.length <= 100
    • +
    • 1 <= arr[i] <= arr.length
    • +
    • arr 中的所有整数互不相同(即,arr 是从 1arr.length 整数的一个排列)
    • +
    +
    Related Topics
  • 贪心
  • 数组
  • 双指针
  • 排序

  • 👍 231
  • 👎 0
  • \ No newline at end of file From 8c20942b6e8eef56926dcefa3956877af7132de7 Mon Sep 17 00:00:00 2001 From: shangliang Date: Sat, 5 Mar 2022 20:55:10 +0800 Subject: [PATCH 22/85] test --- test.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/test.txt b/test.txt index e69de29..30d74d2 100644 --- a/test.txt +++ b/test.txt @@ -0,0 +1 @@ +test \ No newline at end of file From 5264b448bf94d18832fe93af91d7af12e04b0815 Mon Sep 17 00:00:00 2001 From: shangliang Date: Sat, 5 Mar 2022 21:18:20 +0800 Subject: [PATCH 23/85] test --- test.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.txt b/test.txt index 30d74d2..f079749 100644 --- a/test.txt +++ b/test.txt @@ -1 +1 @@ -test \ No newline at end of file +test1 \ No newline at end of file From 7b7f743ef9cc0a42b1935fdf573f6e7822deff6b Mon Sep 17 00:00:00 2001 From: shangliang Date: Sat, 12 Mar 2022 09:40:08 +0800 Subject: [PATCH 24/85] =?UTF-8?q?590=20N=20=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/NAryTreePostorderTraversal.java | 108 ++++++++++++++++++ .../doc/content/NAryTreePostorderTraversal.md | 38 ++++++ 2 files changed, 146 insertions(+) create mode 100644 src/com/leetcode/editor/cn/NAryTreePostorderTraversal.java create mode 100644 src/com/leetcode/editor/cn/doc/content/NAryTreePostorderTraversal.md diff --git a/src/com/leetcode/editor/cn/NAryTreePostorderTraversal.java b/src/com/leetcode/editor/cn/NAryTreePostorderTraversal.java new file mode 100644 index 0000000..21f3460 --- /dev/null +++ b/src/com/leetcode/editor/cn/NAryTreePostorderTraversal.java @@ -0,0 +1,108 @@ +package com.leetcode.editor.cn; + +//给定一个 n 叉树的根节点 root ,返回 其节点值的 后序遍历 。 +// +// n 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 null 分隔(请参见示例)。 +// +// +// +// 示例 1: +// +// +// +// +//输入:root = [1,null,3,2,4,null,5,6] +//输出:[5,6,3,2,4,1] +// +// +// 示例 2: +// +// +// +// +//输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12, +//null,13,null,null,14] +//输出:[2,6,14,11,7,3,12,8,4,13,9,10,5,1] +// +// +// +// +// 提示: +// +// +// 节点总数在范围 [0, 10⁴] 内 +// 0 <= Node.val <= 10⁴ +// n 叉树的高度小于或等于 1000 +// +// +// +// +// 进阶:递归法很简单,你可以使用迭代法完成此题吗? +// Related Topics 栈 树 深度优先搜索 👍 194 👎 0 + + +import java.util.ArrayList; +import java.util.List; + +/** + * 590 N 叉树的后序遍历 + * + * @author shang.liang + * @date 2022-03-12 09:35:34 + */ +public class NAryTreePostorderTraversal { + public static void main(String[] args) { + Solution soution = new NAryTreePostorderTraversal().new Solution(); + + } + + class Node { + public int val; + public List children; + + public Node() { + } + + public Node(int _val) { + val = _val; + } + + public Node(int _val, List _children) { + val = _val; + children = _children; + } + } + //leetcode submit region begin(Prohibit modification and deletion) + + +// Definition for a Node. + + + class Solution { + public List postorder(Node root) { + + List res = new ArrayList<>(); + + + dfs(root, res); + + + return res; + } + + public void dfs(Node root, List res) { + + if (root == null) { + return; + } + + for (Node child : root.children) { + dfs(child, res); + } + + res.add(root.val); + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/com/leetcode/editor/cn/doc/content/NAryTreePostorderTraversal.md b/src/com/leetcode/editor/cn/doc/content/NAryTreePostorderTraversal.md new file mode 100644 index 0000000..c1f5cf9 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/NAryTreePostorderTraversal.md @@ -0,0 +1,38 @@ +

    给定一个 n 叉树的根节点 root ,返回 其节点值的 后序遍历

    + +

    n 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 null 分隔(请参见示例)。

    + +

     

    + +

    示例 1:

    + +

    + +
    +输入:root = [1,null,3,2,4,null,5,6]
    +输出:[5,6,3,2,4,1]
    +
    + +

    示例 2:

    + +

    + +
    +输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
    +输出:[2,6,14,11,7,3,12,8,4,13,9,10,5,1]
    +
    + +

     

    + +

    提示:

    + +
      +
    • 节点总数在范围 [0, 104]
    • +
    • 0 <= Node.val <= 104
    • +
    • n 叉树的高度小于或等于 1000
    • +
    + +

     

    + +

    进阶:递归法很简单,你可以使用迭代法完成此题吗?

    +
    Related Topics
  • 深度优先搜索

  • 👍 194
  • 👎 0
  • \ No newline at end of file From 2387043548bd9b97c81cfbda31dd3a81733278d1 Mon Sep 17 00:00:00 2001 From: shangliang Date: Wed, 16 Mar 2022 22:33:44 +0800 Subject: [PATCH 25/85] =?UTF-8?q?688=20=E9=AA=91=E5=A3=AB=E5=9C=A8?= =?UTF-8?q?=E6=A3=8B=E7=9B=98=E4=B8=8A=E7=9A=84=E6=A6=82=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/KnightProbabilityInChessboard.java | 94 +++++++++++++++++++ .../content/KnightProbabilityInChessboard.md | 41 ++++++++ 2 files changed, 135 insertions(+) create mode 100644 src/com/leetcode/editor/cn/KnightProbabilityInChessboard.java create mode 100644 src/com/leetcode/editor/cn/doc/content/KnightProbabilityInChessboard.md diff --git a/src/com/leetcode/editor/cn/KnightProbabilityInChessboard.java b/src/com/leetcode/editor/cn/KnightProbabilityInChessboard.java new file mode 100644 index 0000000..c5ba5b7 --- /dev/null +++ b/src/com/leetcode/editor/cn/KnightProbabilityInChessboard.java @@ -0,0 +1,94 @@ +package com.leetcode.editor.cn; + +//在一个 n x n 的国际象棋棋盘上,一个骑士从单元格 (row, column) 开始,并尝试进行 k 次移动。行和列是 从 0 开始 的,所以左上单元格 +//是 (0,0) ,右下单元格是 (n - 1, n - 1) 。 +// +// 象棋骑士有8种可能的走法,如下图所示。每次移动在基本方向上是两个单元格,然后在正交方向上是一个单元格。 +// +// +// +// 每次骑士要移动时,它都会随机从8种可能的移动中选择一种(即使棋子会离开棋盘),然后移动到那里。 +// +// 骑士继续移动,直到它走了 k 步或离开了棋盘。 +// +// 返回 骑士在棋盘停止移动后仍留在棋盘上的概率 。 +// +// +// +// 示例 1: +// +// +//输入: n = 3, k = 2, row = 0, column = 0 +//输出: 0.0625 +//解释: 有两步(到(1,2),(2,1))可以让骑士留在棋盘上。 +//在每一个位置上,也有两种移动可以让骑士留在棋盘上。 +//骑士留在棋盘上的总概率是0.0625。 +// +// +// 示例 2: +// +// +//输入: n = 1, k = 0, row = 0, column = 0 +//输出: 1.00000 +// +// +// +// +// 提示: +// +// +// 1 <= n <= 25 +// 0 <= k <= 100 +// 0 <= row, column <= n +// +// Related Topics 动态规划 👍 195 👎 0 + + +/** + * 688 骑士在棋盘上的概率 + * + * @author shang.liang + * @date 2022-02-17 11:27:42 + */ +public class KnightProbabilityInChessboard { + public static void main(String[] args) { + Solution soution = new KnightProbabilityInChessboard().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + + int[][] dirs = {{-2, -1}, {-2, 1}, + {2, -1}, {2, 1}, {-1, -2}, + {-1, 2}, {1, -2}, {1, 2}}; + + public double knightProbability(int n, int k, int row, int column) { + + double[][][] dp = new double[n][n][k + 1]; + + for (int i = 0; i <= k; i++) { + for (int j = 0; j < n; j++) { + for (int m = 0; m < n; m++) { + if (i == 0) { + dp[j][m][i] = 1; + continue; + } + for (int l = 0; l < dirs.length; l++) { + int jp = j + dirs[l][0]; + int mp = m + dirs[l][1]; + if (jp >= 0 && jp < n && mp >= 0 && mp < n) { + dp[j][m][i] += dp[jp][mp][i - 1] / 8; + } + } + } + } + } + + return dp[row][column][k]; + } + + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/com/leetcode/editor/cn/doc/content/KnightProbabilityInChessboard.md b/src/com/leetcode/editor/cn/doc/content/KnightProbabilityInChessboard.md new file mode 100644 index 0000000..20164dc --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/KnightProbabilityInChessboard.md @@ -0,0 +1,41 @@ +

    在一个 n x n 的国际象棋棋盘上,一个骑士从单元格 (row, column) 开始,并尝试进行 k 次移动。行和列是 从 0 开始 的,所以左上单元格是 (0,0) ,右下单元格是 (n - 1, n - 1)

    + +

    象棋骑士有8种可能的走法,如下图所示。每次移动在基本方向上是两个单元格,然后在正交方向上是一个单元格。

    + +

    + +

    每次骑士要移动时,它都会随机从8种可能的移动中选择一种(即使棋子会离开棋盘),然后移动到那里。

    + +

    骑士继续移动,直到它走了 k 步或离开了棋盘。

    + +

    返回 骑士在棋盘停止移动后仍留在棋盘上的概率

    + +

     

    + +

    示例 1:

    + +
    +输入: n = 3, k = 2, row = 0, column = 0
    +输出: 0.0625
    +解释: 有两步(到(1,2),(2,1))可以让骑士留在棋盘上。
    +在每一个位置上,也有两种移动可以让骑士留在棋盘上。
    +骑士留在棋盘上的总概率是0.0625。
    +
    + +

    示例 2:

    + +
    +输入: n = 1, k = 0, row = 0, column = 0
    +输出: 1.00000
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= n <= 25
    • +
    • 0 <= k <= 100
    • +
    • 0 <= row, column <= n
    • +
    +
    Related Topics
  • 动态规划

  • 👍 195
  • 👎 0
  • \ No newline at end of file From f7ce6b5b723474fa9afd95bb2c520b0961da5776 Mon Sep 17 00:00:00 2001 From: shangliang Date: Wed, 16 Mar 2022 22:33:58 +0800 Subject: [PATCH 26/85] =?UTF-8?q?688=20=E9=AA=91=E5=A3=AB=E5=9C=A8?= =?UTF-8?q?=E6=A3=8B=E7=9B=98=E4=B8=8A=E7=9A=84=E6=A6=82=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/leetcode/editor/cn/KnightProbabilityInChessboard.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/com/leetcode/editor/cn/KnightProbabilityInChessboard.java b/src/com/leetcode/editor/cn/KnightProbabilityInChessboard.java index c5ba5b7..5dbf777 100644 --- a/src/com/leetcode/editor/cn/KnightProbabilityInChessboard.java +++ b/src/com/leetcode/editor/cn/KnightProbabilityInChessboard.java @@ -81,6 +81,7 @@ public double knightProbability(int n, int k, int row, int column) { dp[j][m][i] += dp[jp][mp][i - 1] / 8; } } + } } } From 8c01e21c00479403d3418f88739d659fd83d48b9 Mon Sep 17 00:00:00 2001 From: shangliang Date: Wed, 16 Mar 2022 22:36:25 +0800 Subject: [PATCH 27/85] =?UTF-8?q?688=20=E9=AA=91=E5=A3=AB=E5=9C=A8?= =?UTF-8?q?=E6=A3=8B=E7=9B=98=E4=B8=8A=E7=9A=84=E6=A6=82=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/KnightProbabilityInChessboard.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/com/leetcode/editor/cn/KnightProbabilityInChessboard.java b/src/com/leetcode/editor/cn/KnightProbabilityInChessboard.java index 5dbf777..2f8adfa 100644 --- a/src/com/leetcode/editor/cn/KnightProbabilityInChessboard.java +++ b/src/com/leetcode/editor/cn/KnightProbabilityInChessboard.java @@ -74,9 +74,9 @@ public double knightProbability(int n, int k, int row, int column) { dp[j][m][i] = 1; continue; } - for (int l = 0; l < dirs.length; l++) { - int jp = j + dirs[l][0]; - int mp = m + dirs[l][1]; + for (int[] dir : dirs) { + int jp = j + dir[0]; + int mp = m + dir[1]; if (jp >= 0 && jp < n && mp >= 0 && mp < n) { dp[j][m][i] += dp[jp][mp][i - 1] / 8; } From eb82b1d9113fd616cef1a7cdfda4ad8b3a7cd980 Mon Sep 17 00:00:00 2001 From: shangliang Date: Mon, 21 Mar 2022 22:43:43 +0800 Subject: [PATCH 28/85] =?UTF-8?q?564=20=E5=AF=BB=E6=89=BE=E6=9C=80?= =?UTF-8?q?=E8=BF=91=E7=9A=84=E5=9B=9E=E6=96=87=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/FindTheClosestPalindrome.java | 58 +++++++++++++++++++ .../doc/content/FindTheClosestPalindrome.md | 32 ++++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/com/leetcode/editor/cn/FindTheClosestPalindrome.java create mode 100644 src/com/leetcode/editor/cn/doc/content/FindTheClosestPalindrome.md diff --git a/src/com/leetcode/editor/cn/FindTheClosestPalindrome.java b/src/com/leetcode/editor/cn/FindTheClosestPalindrome.java new file mode 100644 index 0000000..e6dbce3 --- /dev/null +++ b/src/com/leetcode/editor/cn/FindTheClosestPalindrome.java @@ -0,0 +1,58 @@ +package com.leetcode.editor.cn; + +//给定一个表示整数的字符串 n ,返回与它最近的回文整数(不包括自身)。如果不止一个,返回较小的那个。 +// +// “最近的”定义为两个整数差的绝对值最小。 +// +// +// +// 示例 1: +// +// +//输入: n = "123" +//输出: "121" +// +// +// 示例 2: +// +// +//输入: n = "1" +//输出: "0" +//解释: 0 和 2是最近的回文,但我们返回最小的,也就是 0。 +// +// +// +// +// 提示: +// +// +// 1 <= n.length <= 18 +// n 只由数字组成 +// n 不含前导 0 +// n 代表在 [1, 10¹⁸ - 1] 范围内的整数 +// +// Related Topics 数学 字符串 👍 163 👎 0 + + +/** + * 564 寻找最近的回文数 + * @date 2022-03-02 12:01:50 + * @author shang.liang + */ + public class FindTheClosestPalindrome{ + public static void main(String[] args){ + Solution soution = new FindTheClosestPalindrome().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public String nearestPalindromic(String n) { + + + return ""; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode/editor/cn/doc/content/FindTheClosestPalindrome.md b/src/com/leetcode/editor/cn/doc/content/FindTheClosestPalindrome.md new file mode 100644 index 0000000..7428035 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/FindTheClosestPalindrome.md @@ -0,0 +1,32 @@ +

    给定一个表示整数的字符串 n ,返回与它最近的回文整数(不包括自身)。如果不止一个,返回较小的那个。

    + +

    “最近的”定义为两个整数差的绝对值最小。

    + +

     

    + +

    示例 1:

    + +
    +输入: n = "123"
    +输出: "121"
    +
    + +

    示例 2:

    + +
    +输入: n = "1"
    +输出: "0"
    +解释: 0 和 2是最近的回文,但我们返回最小的,也就是 0。
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= n.length <= 18
    • +
    • n 只由数字组成
    • +
    • n 不含前导 0
    • +
    • n 代表在 [1, 1018 - 1] 范围内的整数
    • +
    +
    Related Topics
  • 数学
  • 字符串

  • 👍 163
  • 👎 0
  • \ No newline at end of file From 829f639be1e264978011e0a828efe2de1f2c77c8 Mon Sep 17 00:00:00 2001 From: shangliang Date: Mon, 21 Mar 2022 22:46:21 +0800 Subject: [PATCH 29/85] =?UTF-8?q?653=20=E4=B8=A4=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C=20IV=20-=20=E8=BE=93=E5=85=A5=20BST?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/TwoSumIvInputIsABst.java | 101 ++++++++++++++++++ .../cn/doc/content/TwoSumIvInputIsABst.md | 29 +++++ 2 files changed, 130 insertions(+) create mode 100644 src/com/leetcode/editor/cn/TwoSumIvInputIsABst.java create mode 100644 src/com/leetcode/editor/cn/doc/content/TwoSumIvInputIsABst.md diff --git a/src/com/leetcode/editor/cn/TwoSumIvInputIsABst.java b/src/com/leetcode/editor/cn/TwoSumIvInputIsABst.java new file mode 100644 index 0000000..f3bbd7d --- /dev/null +++ b/src/com/leetcode/editor/cn/TwoSumIvInputIsABst.java @@ -0,0 +1,101 @@ +package com.leetcode.editor.cn; + +//给定一个二叉搜索树 root 和一个目标结果 k,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true。 +// +// +// +// 示例 1: +// +// +//输入: root = [5,3,6,2,4,null,7], k = 9 +//输出: true +// +// +// 示例 2: +// +// +//输入: root = [5,3,6,2,4,null,7], k = 28 +//输出: false +// +// +// +// +// 提示: +// +// +// 二叉树的节点个数的范围是 [1, 10⁴]. +// -10⁴ <= Node.val <= 10⁴ +// root 为二叉搜索树 +// -10⁵ <= k <= 10⁵ +// +// Related Topics 树 深度优先搜索 广度优先搜索 二叉搜索树 哈希表 双指针 二叉树 👍 375 👎 0 + + +import com.liang.leetcode.bean.TreeNode; + +import java.util.HashSet; +import java.util.Set; + +/** + * 653 两数之和 IV - 输入 BST + * @date 2022-03-21 22:34:44 + * @author shang.liang + */ + public class TwoSumIvInputIsABst{ + public static void main(String[] args){ + Solution soution = new TwoSumIvInputIsABst().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + + Set set = new HashSet(); + + boolean flag = false; + + public void dfs(TreeNode node, int k) { + + if (flag) { + return; + } + + if (node == null) { + return; + } + + if (set.contains(k - node.val)) { + flag = true; + return; + } + + set.add(node.val); + dfs(node.left, k); + dfs(node.right, k); + } + + public boolean findTarget(TreeNode root, int k) { + + dfs(root, k); + + return flag; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode/editor/cn/doc/content/TwoSumIvInputIsABst.md b/src/com/leetcode/editor/cn/doc/content/TwoSumIvInputIsABst.md new file mode 100644 index 0000000..a47d0fd --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/TwoSumIvInputIsABst.md @@ -0,0 +1,29 @@ +

    给定一个二叉搜索树 root 和一个目标结果 k,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true

    + +

     

    + +

    示例 1:

    + +
    +输入: root = [5,3,6,2,4,null,7], k = 9
    +输出: true
    +
    + +

    示例 2:

    + +
    +输入: root = [5,3,6,2,4,null,7], k = 28
    +输出: false
    +
    + +

     

    + +

    提示:

    + +
      +
    • 二叉树的节点个数的范围是  [1, 104].
    • +
    • -104 <= Node.val <= 104
    • +
    • root 为二叉搜索树
    • +
    • -105 <= k <= 105
    • +
    +
    Related Topics
  • 深度优先搜索
  • 广度优先搜索
  • 二叉搜索树
  • 哈希表
  • 双指针
  • 二叉树

  • 👍 375
  • 👎 0
  • \ No newline at end of file From 5b2740c1d7a6f9d03dfb8c6ccaa9b44868ad6214 Mon Sep 17 00:00:00 2001 From: shangliang Date: Fri, 25 Mar 2022 23:29:48 +0800 Subject: [PATCH 30/85] =?UTF-8?q?172=20=E9=98=B6=E4=B9=98=E5=90=8E?= =?UTF-8?q?=E7=9A=84=E9=9B=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/FactorialTrailingZeroes.java | 74 +++++++++++++++++++ .../cn/doc/content/FactorialTrailingZeroes.md | 41 ++++++++++ 2 files changed, 115 insertions(+) create mode 100644 src/com/leetcode/editor/cn/FactorialTrailingZeroes.java create mode 100644 src/com/leetcode/editor/cn/doc/content/FactorialTrailingZeroes.md diff --git a/src/com/leetcode/editor/cn/FactorialTrailingZeroes.java b/src/com/leetcode/editor/cn/FactorialTrailingZeroes.java new file mode 100644 index 0000000..ae323b2 --- /dev/null +++ b/src/com/leetcode/editor/cn/FactorialTrailingZeroes.java @@ -0,0 +1,74 @@ +package com.leetcode.editor.cn; + +//给定一个整数 n ,返回 n! 结果中尾随零的数量。 +// +// 提示 n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1 +// +// +// +// 示例 1: +// +// +//输入:n = 3 +//输出:0 +//解释:3! = 6 ,不含尾随 0 +// +// +// 示例 2: +// +// +//输入:n = 5 +//输出:1 +//解释:5! = 120 ,有一个尾随 0 +// +// +// 示例 3: +// +// +//输入:n = 0 +//输出:0 +// +// +// +// +// 提示: +// +// +// 0 <= n <= 10⁴ +// +// +// +// +// 进阶:你可以设计并实现对数时间复杂度的算法来解决此问题吗? +// Related Topics 数学 👍 651 👎 0 + + +/** + * 172 阶乘后的零 + * @date 2022-03-25 23:25:58 + * @author shang.liang + */ + public class FactorialTrailingZeroes{ + public static void main(String[] args){ + Solution soution = new FactorialTrailingZeroes().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int trailingZeroes(int n) { + + int res = 0; + + for (int i = 5; i <= n; i+=5) { + for (int j = i; j % 5 == 0; j /= 5) { + res++; + } + } + + return res; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode/editor/cn/doc/content/FactorialTrailingZeroes.md b/src/com/leetcode/editor/cn/doc/content/FactorialTrailingZeroes.md new file mode 100644 index 0000000..621ff4b --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/FactorialTrailingZeroes.md @@ -0,0 +1,41 @@ +

    给定一个整数 n ,返回 n! 结果中尾随零的数量。

    + +

    提示 n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1

    + +

     

    + +

    示例 1:

    + +
    +输入:n = 3
    +输出:0
    +解释:3! = 6 ,不含尾随 0
    +
    + +

    示例 2:

    + +
    +输入:n = 5
    +输出:1
    +解释:5! = 120 ,有一个尾随 0
    +
    + +

    示例 3:

    + +
    +输入:n = 0
    +输出:0
    +
    + +

     

    + +

    提示:

    + +
      +
    • 0 <= n <= 104
    • +
    + +

     

    + +

    进阶:你可以设计并实现对数时间复杂度的算法来解决此问题吗?

    +
    Related Topics
  • 数学

  • 👍 651
  • 👎 0
  • \ No newline at end of file From ce679611abfeb582422b90a5445e25dc9e5b5d8c Mon Sep 17 00:00:00 2001 From: shangliang Date: Sat, 26 Mar 2022 12:36:32 +0800 Subject: [PATCH 31/85] =?UTF-8?q?682=20=E6=A3=92=E7=90=83=E6=AF=94?= =?UTF-8?q?=E8=B5=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/leetcode/editor/cn/BaseballGame.java | 118 ++++++++++++++++++ .../editor/cn/doc/content/BaseballGame.md | 64 ++++++++++ 2 files changed, 182 insertions(+) create mode 100644 src/com/leetcode/editor/cn/BaseballGame.java create mode 100644 src/com/leetcode/editor/cn/doc/content/BaseballGame.md diff --git a/src/com/leetcode/editor/cn/BaseballGame.java b/src/com/leetcode/editor/cn/BaseballGame.java new file mode 100644 index 0000000..529dda3 --- /dev/null +++ b/src/com/leetcode/editor/cn/BaseballGame.java @@ -0,0 +1,118 @@ +package com.leetcode.editor.cn; + +//你现在是一场采用特殊赛制棒球比赛的记录员。这场比赛由若干回合组成,过去几回合的得分可能会影响以后几回合的得分。 +// +// 比赛开始时,记录是空白的。你会得到一个记录操作的字符串列表 ops,其中 ops[i] 是你需要记录的第 i 项操作,ops 遵循下述规则: +// +// +// 整数 x - 表示本回合新获得分数 x +// "+" - 表示本回合新获得的得分是前两次得分的总和。题目数据保证记录此操作时前面总是存在两个有效的分数。 +// "D" - 表示本回合新获得的得分是前一次得分的两倍。题目数据保证记录此操作时前面总是存在一个有效的分数。 +// "C" - 表示前一次得分无效,将其从记录中移除。题目数据保证记录此操作时前面总是存在一个有效的分数。 +// +// +// 请你返回记录中所有得分的总和。 +// +// +// +// 示例 1: +// +// +//输入:ops = ["5","2","C","D","+"] +//输出:30 +//解释: +//"5" - 记录加 5 ,记录现在是 [5] +//"2" - 记录加 2 ,记录现在是 [5, 2] +//"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5]. +//"D" - 记录加 2 * 5 = 10 ,记录现在是 [5, 10]. +//"+" - 记录加 5 + 10 = 15 ,记录现在是 [5, 10, 15]. +//所有得分的总和 5 + 10 + 15 = 30 +// +// +// 示例 2: +// +// +//输入:ops = ["5","-2","4","C","D","9","+","+"] +//输出:27 +//解释: +//"5" - 记录加 5 ,记录现在是 [5] +//"-2" - 记录加 -2 ,记录现在是 [5, -2] +//"4" - 记录加 4 ,记录现在是 [5, -2, 4] +//"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5, -2] +//"D" - 记录加 2 * -2 = -4 ,记录现在是 [5, -2, -4] +//"9" - 记录加 9 ,记录现在是 [5, -2, -4, 9] +//"+" - 记录加 -4 + 9 = 5 ,记录现在是 [5, -2, -4, 9, 5] +//"+" - 记录加 9 + 5 = 14 ,记录现在是 [5, -2, -4, 9, 5, 14] +//所有得分的总和 5 + -2 + -4 + 9 + 5 + 14 = 27 +// +// +// 示例 3: +// +// +//输入:ops = ["1"] +//输出:1 +// +// +// +// +// 提示: +// +// +// 1 <= ops.length <= 1000 +// ops[i] 为 "C"、"D"、"+",或者一个表示整数的字符串。整数范围是 [-3 * 10⁴, 3 * 10⁴] +// 对于 "+" 操作,题目数据保证记录此操作时前面总是存在两个有效的分数 +// 对于 "C" 和 "D" 操作,题目数据保证记录此操作时前面总是存在一个有效的分数 +// +// Related Topics 栈 数组 模拟 👍 222 👎 0 + + +/** + * 682 棒球比赛 + * @date 2022-03-26 12:07:50 + * @author shang.liang + */ + public class BaseballGame{ + public static void main(String[] args){ + Solution soution = new BaseballGame().new Solution(); + String[] ops = new String[]{"5","-2","4","C","D","9","+","+"}; + System.out.println("soution.calPoints(ops) = " + soution.calPoints(ops)); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int calPoints(String[] ops) { + + int res = 0; + int[] score = new int[ops.length]; + + for (int i = 0,index=0; i < ops.length; i++, index++) { + switch (ops[i]) { + case "+": { + score[index] = score[index - 1] + score[index - 2]; + res += score[index]; + break; + } + case "D":{ + score[index] = score[index - 1] * 2; + res += score[index]; + break; + } + case "C": { + res -= score[index - 1]; + index -= 2; + break; + } + default: { + score[index] = Integer.parseInt(ops[i]); + res += score[index]; + } + } + } + + return res; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode/editor/cn/doc/content/BaseballGame.md b/src/com/leetcode/editor/cn/doc/content/BaseballGame.md new file mode 100644 index 0000000..e98f29f --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/BaseballGame.md @@ -0,0 +1,64 @@ +

    你现在是一场采用特殊赛制棒球比赛的记录员。这场比赛由若干回合组成,过去几回合的得分可能会影响以后几回合的得分。

    + +

    比赛开始时,记录是空白的。你会得到一个记录操作的字符串列表 ops,其中 ops[i] 是你需要记录的第 i 项操作,ops 遵循下述规则:

    + +
      +
    1. 整数 x - 表示本回合新获得分数 x
    2. +
    3. "+" - 表示本回合新获得的得分是前两次得分的总和。题目数据保证记录此操作时前面总是存在两个有效的分数。
    4. +
    5. "D" - 表示本回合新获得的得分是前一次得分的两倍。题目数据保证记录此操作时前面总是存在一个有效的分数。
    6. +
    7. "C" - 表示前一次得分无效,将其从记录中移除。题目数据保证记录此操作时前面总是存在一个有效的分数。
    8. +
    + +

    请你返回记录中所有得分的总和。

    + +

     

    + +

    示例 1:

    + +
    +输入:ops = ["5","2","C","D","+"]
    +输出:30
    +解释:
    +"5" - 记录加 5 ,记录现在是 [5]
    +"2" - 记录加 2 ,记录现在是 [5, 2]
    +"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5].
    +"D" - 记录加 2 * 5 = 10 ,记录现在是 [5, 10].
    +"+" - 记录加 5 + 10 = 15 ,记录现在是 [5, 10, 15].
    +所有得分的总和 5 + 10 + 15 = 30
    +
    + +

    示例 2:

    + +
    +输入:ops = ["5","-2","4","C","D","9","+","+"]
    +输出:27
    +解释:
    +"5" - 记录加 5 ,记录现在是 [5]
    +"-2" - 记录加 -2 ,记录现在是 [5, -2]
    +"4" - 记录加 4 ,记录现在是 [5, -2, 4]
    +"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5, -2]
    +"D" - 记录加 2 * -2 = -4 ,记录现在是 [5, -2, -4]
    +"9" - 记录加 9 ,记录现在是 [5, -2, -4, 9]
    +"+" - 记录加 -4 + 9 = 5 ,记录现在是 [5, -2, -4, 9, 5]
    +"+" - 记录加 9 + 5 = 14 ,记录现在是 [5, -2, -4, 9, 5, 14]
    +所有得分的总和 5 + -2 + -4 + 9 + 5 + 14 = 27
    +
    + +

    示例 3:

    + +
    +输入:ops = ["1"]
    +输出:1
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= ops.length <= 1000
    • +
    • ops[i]"C""D""+",或者一个表示整数的字符串。整数范围是 [-3 * 104, 3 * 104]
    • +
    • 对于 "+" 操作,题目数据保证记录此操作时前面总是存在两个有效的分数
    • +
    • 对于 "C""D" 操作,题目数据保证记录此操作时前面总是存在一个有效的分数
    • +
    +
    Related Topics
  • 数组
  • 模拟

  • 👍 222
  • 👎 0
  • \ No newline at end of file From c9dcbb28ba5a53006e9765d55ab7a7521c761111 Mon Sep 17 00:00:00 2001 From: shangliang Date: Sun, 27 Mar 2022 20:29:26 +0800 Subject: [PATCH 32/85] =?UTF-8?q?2028=20=E6=89=BE=E5=87=BA=E7=BC=BA?= =?UTF-8?q?=E5=A4=B1=E7=9A=84=E8=A7=82=E6=B5=8B=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/FindMissingObservations.java | 102 ++++++++++++++++++ .../cn/doc/content/FindMissingObservations.md | 54 ++++++++++ 2 files changed, 156 insertions(+) create mode 100644 src/com/leetcode/editor/cn/FindMissingObservations.java create mode 100644 src/com/leetcode/editor/cn/doc/content/FindMissingObservations.md diff --git a/src/com/leetcode/editor/cn/FindMissingObservations.java b/src/com/leetcode/editor/cn/FindMissingObservations.java new file mode 100644 index 0000000..44537cc --- /dev/null +++ b/src/com/leetcode/editor/cn/FindMissingObservations.java @@ -0,0 +1,102 @@ +package com.leetcode.editor.cn; + +//现有一份 n + m 次投掷单个 六面 骰子的观测数据,骰子的每个面从 1 到 6 编号。观测数据中缺失了 n 份,你手上只拿到剩余 m 次投掷的数据。幸好 +//你有之前计算过的这 n + m 次投掷数据的 平均值 。 +// +// 给你一个长度为 m 的整数数组 rolls ,其中 rolls[i] 是第 i 次观测的值。同时给你两个整数 mean 和 n 。 +// +// 返回一个长度为 n 的数组,包含所有缺失的观测数据,且满足这 n + m 次投掷的 平均值 是 mean 。如果存在多组符合要求的答案,只需要返回其中任意 +//一组即可。如果不存在答案,返回一个空数组。 +// +// k 个数字的 平均值 为这些数字求和后再除以 k 。 +// +// 注意 mean 是一个整数,所以 n + m 次投掷的总和需要被 n + m 整除。 +// +// +// +// 示例 1: +// +// +//输入:rolls = [3,2,4,3], mean = 4, n = 2 +//输出:[6,6] +//解释:所有 n + m 次投掷的平均值是 (3 + 2 + 4 + 3 + 6 + 6) / 6 = 4 。 +// +// +// 示例 2: +// +// +//输入:rolls = [1,5,6], mean = 3, n = 4 +//输出:[2,3,2,2] +//解释:所有 n + m 次投掷的平均值是 (1 + 5 + 6 + 2 + 3 + 2 + 2) / 7 = 3 。 +// +// +// 示例 3: +// +// +//输入:rolls = [1,2,3,4], mean = 6, n = 4 +//输出:[] +//解释:无论丢失的 4 次数据是什么,平均值都不可能是 6 。 +// +// +// 示例 4: +// +// +//输入:rolls = [1], mean = 3, n = 1 +//输出:[5] +//解释:所有 n + m 次投掷的平均值是 (1 + 5) / 2 = 3 。 +// +// +// +// +// 提示: +// +// +// m == rolls.length +// 1 <= n, m <= 10⁵ +// 1 <= rolls[i], mean <= 6 +// +// Related Topics 数组 数学 模拟 👍 47 👎 0 + + +import java.util.Arrays; + +/** + * 2028 找出缺失的观测数据 + * + * @author shang.liang + * @date 2022-03-27 19:59:17 + */ +public class FindMissingObservations { + public static void main(String[] args) { + Solution soution = new FindMissingObservations().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int[] missingRolls(int[] rolls, int mean, int n) { + + int[] res = new int[n]; + + int sum = Arrays.stream(rolls).sum(); + int nTotal = mean * rolls.length - sum + n * mean; + + if (nTotal < n || nTotal > 6 * n) { + return new int[0]; + } + + int index = 0; + for (int i = 0; i < nTotal; i++) { + if (index == n) { + index = 0; + } + res[index++]++; + + } + + return res; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/com/leetcode/editor/cn/doc/content/FindMissingObservations.md b/src/com/leetcode/editor/cn/doc/content/FindMissingObservations.md new file mode 100644 index 0000000..5bd1e1e --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/FindMissingObservations.md @@ -0,0 +1,54 @@ +

    现有一份 n + m 次投掷单个 六面 骰子的观测数据,骰子的每个面从 16 编号。观测数据中缺失了 n 份,你手上只拿到剩余 m 次投掷的数据。幸好你有之前计算过的这 n + m 次投掷数据的 平均值

    + +

    给你一个长度为 m 的整数数组 rolls ,其中 rolls[i] 是第 i 次观测的值。同时给你两个整数 meann

    + +

    返回一个长度为 n 的数组,包含所有缺失的观测数据,且满足这 n + m 次投掷的 平均值 mean 。如果存在多组符合要求的答案,只需要返回其中任意一组即可。如果不存在答案,返回一个空数组。

    + +

    k 个数字的 平均值 为这些数字求和后再除以 k

    + +

    注意 mean 是一个整数,所以 n + m 次投掷的总和需要被 n + m 整除。

    + +

     

    + +

    示例 1:

    + +
    +输入:rolls = [3,2,4,3], mean = 4, n = 2
    +输出:[6,6]
    +解释:所有 n + m 次投掷的平均值是 (3 + 2 + 4 + 3 + 6 + 6) / 6 = 4 。
    +
    + +

    示例 2:

    + +
    +输入:rolls = [1,5,6], mean = 3, n = 4
    +输出:[2,3,2,2]
    +解释:所有 n + m 次投掷的平均值是 (1 + 5 + 6 + 2 + 3 + 2 + 2) / 7 = 3 。
    +
    + +

    示例 3:

    + +
    +输入:rolls = [1,2,3,4], mean = 6, n = 4
    +输出:[]
    +解释:无论丢失的 4 次数据是什么,平均值都不可能是 6 。
    +
    + +

    示例 4:

    + +
    +输入:rolls = [1], mean = 3, n = 1
    +输出:[5]
    +解释:所有 n + m 次投掷的平均值是 (1 + 5) / 2 = 3 。
    +
    + +

     

    + +

    提示:

    + +
      +
    • m == rolls.length
    • +
    • 1 <= n, m <= 105
    • +
    • 1 <= rolls[i], mean <= 6
    • +
    +
    Related Topics
  • 数组
  • 数学
  • 模拟

  • 👍 47
  • 👎 0
  • \ No newline at end of file From 2a3a814e55c0d76e4111ac1fec5b6fe47a349e5c Mon Sep 17 00:00:00 2001 From: shangliang Date: Mon, 28 Mar 2022 21:58:47 +0800 Subject: [PATCH 33/85] =?UTF-8?q?693=20=E4=BA=A4=E6=9B=BF=E4=BD=8D?= =?UTF-8?q?=E4=BA=8C=E8=BF=9B=E5=88=B6=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/BinaryNumberWithAlternatingBits.java | 61 +++++++++++++++++++ .../BinaryNumberWithAlternatingBits.md | 34 +++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/com/leetcode/editor/cn/BinaryNumberWithAlternatingBits.java create mode 100644 src/com/leetcode/editor/cn/doc/content/BinaryNumberWithAlternatingBits.md diff --git a/src/com/leetcode/editor/cn/BinaryNumberWithAlternatingBits.java b/src/com/leetcode/editor/cn/BinaryNumberWithAlternatingBits.java new file mode 100644 index 0000000..073157c --- /dev/null +++ b/src/com/leetcode/editor/cn/BinaryNumberWithAlternatingBits.java @@ -0,0 +1,61 @@ +package com.leetcode.editor.cn; + +//给定一个正整数,检查它的二进制表示是否总是 0、1 交替出现:换句话说,就是二进制表示中相邻两位的数字永不相同。 +// +// +// +// 示例 1: +// +// +//输入:n = 5 +//输出:true +//解释:5 的二进制表示是:101 +// +// +// 示例 2: +// +// +//输入:n = 7 +//输出:false +//解释:7 的二进制表示是:111. +// +// 示例 3: +// +// +//输入:n = 11 +//输出:false +//解释:11 的二进制表示是:1011. +// +// +// +// 提示: +// +// +// 1 <= n <= 2³¹ - 1 +// +// Related Topics 位运算 👍 186 👎 0 + + +/** + * 693 交替位二进制数 + * @date 2022-03-28 21:54:45 + * @author shang.liang + */ + public class BinaryNumberWithAlternatingBits{ + public static void main(String[] args){ + Solution soution = new BinaryNumberWithAlternatingBits().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public boolean hasAlternatingBits(int n) { + + int a = n ^ n >> 1; + + return (a & (a + 1)) == 0; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode/editor/cn/doc/content/BinaryNumberWithAlternatingBits.md b/src/com/leetcode/editor/cn/doc/content/BinaryNumberWithAlternatingBits.md new file mode 100644 index 0000000..dcaea8f --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/BinaryNumberWithAlternatingBits.md @@ -0,0 +1,34 @@ +

    给定一个正整数,检查它的二进制表示是否总是 0、1 交替出现:换句话说,就是二进制表示中相邻两位的数字永不相同。

    + +

     

    + +

    示例 1:

    + +
    +输入:n = 5
    +输出:true
    +解释:5 的二进制表示是:101
    +
    + +

    示例 2:

    + +
    +输入:n = 7
    +输出:false
    +解释:7 的二进制表示是:111.
    + +

    示例 3:

    + +
    +输入:n = 11
    +输出:false
    +解释:11 的二进制表示是:1011.
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= n <= 231 - 1
    • +
    +
    Related Topics
  • 位运算

  • 👍 186
  • 👎 0
  • \ No newline at end of file From bc682c2384310d14c446137d769d319413a365f2 Mon Sep 17 00:00:00 2001 From: shangliang Date: Fri, 1 Apr 2022 23:36:21 +0800 Subject: [PATCH 34/85] =?UTF-8?q?954=20=E4=BA=8C=E5=80=8D=E6=95=B0?= =?UTF-8?q?=E5=AF=B9=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/ArrayOfDoubledPairs.java | 90 +++++++++++++++++++ .../cn/doc/content/ArrayOfDoubledPairs.md | 36 ++++++++ 2 files changed, 126 insertions(+) create mode 100644 src/com/leetcode/editor/cn/ArrayOfDoubledPairs.java create mode 100644 src/com/leetcode/editor/cn/doc/content/ArrayOfDoubledPairs.md diff --git a/src/com/leetcode/editor/cn/ArrayOfDoubledPairs.java b/src/com/leetcode/editor/cn/ArrayOfDoubledPairs.java new file mode 100644 index 0000000..75f9beb --- /dev/null +++ b/src/com/leetcode/editor/cn/ArrayOfDoubledPairs.java @@ -0,0 +1,90 @@ +package com.leetcode.editor.cn; + +//给定一个长度为偶数的整数数组 arr,只有对 arr 进行重组后可以满足 “对于每个 0 <= i < len(arr) / 2,都有 arr[2 * i +//+ 1] = 2 * arr[2 * i]” 时,返回 true;否则,返回 false。 +// +// +// +// 示例 1: +// +// +//输入:arr = [3,1,3,6] +//输出:false +// +// +// 示例 2: +// +// +//输入:arr = [2,1,2,6] +//输出:false +// +// +// 示例 3: +// +// +//输入:arr = [4,-2,2,-4] +//输出:true +//解释:可以用 [-2,-4] 和 [2,4] 这两组组成 [-2,-4,2,4] 或是 [2,4,-2,-4] +// +// +// +// +// 提示: +// +// +// 0 <= arr.length <= 3 * 10⁴ +// arr.length 是偶数 +// -10⁵ <= arr[i] <= 10⁵ +// +// Related Topics 贪心 数组 哈希表 排序 👍 158 👎 0 + + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; + +/** + * 954 二倍数对数组 + * @date 2022-04-01 23:16:30 + * @author shang.liang + */ + public class ArrayOfDoubledPairs{ + public static void main(String[] args){ + Solution soution = new ArrayOfDoubledPairs().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public boolean canReorderDoubled(int[] arr) { + + HashMap map = new HashMap<>(); + + for (int i = 0; i < arr.length; i++) { + map.put(arr[i], map.getOrDefault(arr[i], 0) + 1); + } + + List list = new ArrayList<>(); + for (Integer num :map.keySet()){ + list.add(num); + } + + Collections.sort(list, (a, b) -> Math.abs(a) - Math.abs(b)); + + for (Integer integer : list) { + + if (map.getOrDefault(integer * 2, 0) < map.get(integer)) { + return false; + } + + map.put(integer * 2, map.getOrDefault(integer * 2, 0) - map.get(integer)); + + } + + return true; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode/editor/cn/doc/content/ArrayOfDoubledPairs.md b/src/com/leetcode/editor/cn/doc/content/ArrayOfDoubledPairs.md new file mode 100644 index 0000000..a55ca86 --- /dev/null +++ b/src/com/leetcode/editor/cn/doc/content/ArrayOfDoubledPairs.md @@ -0,0 +1,36 @@ +

    给定一个长度为偶数的整数数组 arr,只有对 arr 进行重组后可以满足 “对于每个 0 <= i < len(arr) / 2,都有 arr[2 * i + 1] = 2 * arr[2 * i]” 时,返回 true;否则,返回 false

    + +

     

    + +

    示例 1:

    + +
    +输入:arr = [3,1,3,6]
    +输出:false
    +
    + +

    示例 2:

    + +
    +输入:arr = [2,1,2,6]
    +输出:false
    +
    + +

    示例 3:

    + +
    +输入:arr = [4,-2,2,-4]
    +输出:true
    +解释:可以用 [-2,-4] 和 [2,4] 这两组组成 [-2,-4,2,4] 或是 [2,4,-2,-4]
    +
    + +

     

    + +

    提示:

    + +
      +
    • 0 <= arr.length <= 3 * 104
    • +
    • arr.length 是偶数
    • +
    • -105 <= arr[i] <= 105
    • +
    +
    Related Topics
  • 贪心
  • 数组
  • 哈希表
  • 排序

  • 👍 158
  • 👎 0
  • \ No newline at end of file From b5a645bccabf32a8a9d15998ba8db460bbf47bb6 Mon Sep 17 00:00:00 2001 From: shangliang Date: Wed, 19 Oct 2022 19:37:45 +0800 Subject: [PATCH 35/85] =?UTF-8?q?1700=20=E6=97=A0=E6=B3=95=E5=90=83?= =?UTF-8?q?=E5=8D=88=E9=A4=90=E7=9A=84=E5=AD=A6=E7=94=9F=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/NumberOfStudentsUnableToEatLunch.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/com/leetcode/editor/cn/NumberOfStudentsUnableToEatLunch.java diff --git a/src/com/leetcode/editor/cn/NumberOfStudentsUnableToEatLunch.java b/src/com/leetcode/editor/cn/NumberOfStudentsUnableToEatLunch.java new file mode 100644 index 0000000..737394f --- /dev/null +++ b/src/com/leetcode/editor/cn/NumberOfStudentsUnableToEatLunch.java @@ -0,0 +1,90 @@ +package com.leetcode.editor.cn; + +//学校的自助午餐提供圆形和方形的三明治,分别用数字 0 和 1 表示。所有学生站在一个队列里,每个学生要么喜欢圆形的要么喜欢方形的。 餐厅里三明治的数量与学生 +//的数量相同。所有三明治都放在一个 栈 里,每一轮: +// +// +// 如果队列最前面的学生 喜欢 栈顶的三明治,那么会 拿走它 并离开队列。 +// 否则,这名学生会 放弃这个三明治 并回到队列的尾部。 +// +// +// 这个过程会一直持续到队列里所有学生都不喜欢栈顶的三明治为止。 +// +// 给你两个整数数组 students 和 sandwiches ,其中 sandwiches[i] 是栈里面第 i 个三明治的类型(i = 0 是栈的顶部) +//, students[j] 是初始队列里第 j 名学生对三明治的喜好(j = 0 是队列的最开始位置)。请你返回无法吃午餐的学生数量。 +// +// +// +// 示例 1: +// +// 输入:students = [1,1,0,0], sandwiches = [0,1,0,1] +//输出:0 +//解释: +//- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [1,0,0,1]。 +//- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [0,0,1,1]。 +//- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [0,1,1],三明治栈为 sandwiches = [1,0,1]。 +//- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [1,1,0]。 +//- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [1,0],三明治栈为 sandwiches = [0,1]。 +//- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [0,1]。 +//- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [1],三明治栈为 sandwiches = [1]。 +//- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [],三明治栈为 sandwiches = []。 +//所以所有学生都有三明治吃。 +// +// +// 示例 2: +// +// 输入:students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1] +//输出:3 +// +// +// +// +// 提示: +// +// +// 1 <= students.length, sandwiches.length <= 100 +// students.length == sandwiches.length +// sandwiches[i] 要么是 0 ,要么是 1 。 +// students[i] 要么是 0 ,要么是 1 。 +// +// +// Related Topics 栈 队列 数组 模拟 👍 111 👎 0 + + +import java.util.Arrays; + +/** + * 1700 无法吃午餐的学生数量 + * + * @author shang.liang + * @date 2022-10-19 18:58:24 + */ +public class NumberOfStudentsUnableToEatLunch { + public static void main(String[] args) { + Solution soution = new NumberOfStudentsUnableToEatLunch().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int countStudents(int[] students, int[] sandwiches) { + + int s1 = Arrays.stream(students).sum(); + int s0 = students.length - s1; + + for (int i = 0; i < sandwiches.length; i++) { + if (sandwiches[i] == 0 && s0 > 0) { + s0--; + } else if (sandwiches[i] == 1 && s1 > 0) { + s1--; + } else { + break; + } + } + return s0 + s1; + + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From 4bb1a1d9648650d6e4a5519c7603a6382ae81c78 Mon Sep 17 00:00:00 2001 From: shangliang Date: Sun, 23 Oct 2022 15:05:28 +0800 Subject: [PATCH 36/85] =?UTF-8?q?1768=20=E4=BA=A4=E6=9B=BF=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/MergeStringsAlternately.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/com/leetcode/editor/cn/MergeStringsAlternately.java diff --git a/src/com/leetcode/editor/cn/MergeStringsAlternately.java b/src/com/leetcode/editor/cn/MergeStringsAlternately.java new file mode 100644 index 0000000..224a44a --- /dev/null +++ b/src/com/leetcode/editor/cn/MergeStringsAlternately.java @@ -0,0 +1,91 @@ +package com.leetcode.editor.cn; + +//给你两个字符串 word1 和 word2 。请你从 word1 开始,通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长,就将多出来的字母追加到 +//合并后字符串的末尾。 +// +// 返回 合并后的字符串 。 +// +// +// +// 示例 1: +// +// +//输入:word1 = "abc", word2 = "pqr" +//输出:"apbqcr" +//解释:字符串合并情况如下所示: +//word1: a b c +//word2: p q r +//合并后: a p b q c r +// +// +// 示例 2: +// +// +//输入:word1 = "ab", word2 = "pqrs" +//输出:"apbqrs" +//解释:注意,word2 比 word1 长,"rs" 需要追加到合并后字符串的末尾。 +//word1: a b +//word2: p q r s +//合并后: a p b q r s +// +// +// 示例 3: +// +// +//输入:word1 = "abcd", word2 = "pq" +//输出:"apbqcd" +//解释:注意,word1 比 word2 长,"cd" 需要追加到合并后字符串的末尾。 +//word1: a b c d +//word2: p q +//合并后: a p b q c d +// +// +// +// +// 提示: +// +// +// 1 <= word1.length, word2.length <= 100 +// word1 和 word2 由小写英文字母组成 +// +// +// Related Topics 双指针 字符串 👍 54 👎 0 + + +/** + * 1768 交替合并字符串 + * @date 2022-10-23 15:00:28 + * @author shang.liang + */ + public class MergeStringsAlternately{ + public static void main(String[] args){ + Solution soution = new MergeStringsAlternately().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public String mergeAlternately(String word1, String word2) { + + int l1 = word1.length(); + int l2 = word2.length(); + + int i = 0, j = 0; + StringBuilder result = new StringBuilder(); + while (i < l1 || j < l2) { + if (i < l1) { + result.append(word1.charAt(i)); + i++; + } + if (j < l2) { + result.append(word2.charAt(j)); + j++; + } + } + + return result.toString(); + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } From 600c408a01e544fc131e831f1b950abc5240aea5 Mon Sep 17 00:00:00 2001 From: shangliang Date: Tue, 25 Oct 2022 21:16:29 +0800 Subject: [PATCH 37/85] =?UTF-8?q?915=20=E5=88=86=E5=89=B2=E6=95=B0?= =?UTF-8?q?=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PartitionArrayIntoDisjointIntervals.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/com/leetcode/editor/cn/PartitionArrayIntoDisjointIntervals.java diff --git a/src/com/leetcode/editor/cn/PartitionArrayIntoDisjointIntervals.java b/src/com/leetcode/editor/cn/PartitionArrayIntoDisjointIntervals.java new file mode 100644 index 0000000..81740fa --- /dev/null +++ b/src/com/leetcode/editor/cn/PartitionArrayIntoDisjointIntervals.java @@ -0,0 +1,80 @@ +package com.leetcode.editor.cn; + +//给定一个数组 nums ,将其划分为两个连续子数组 left 和 right, 使得: +// +// +// left 中的每个元素都小于或等于 right 中的每个元素。 +// left 和 right 都是非空的。 +// left 的长度要尽可能小。 +// +// +// 在完成这样的分组后返回 left 的 长度 。 +// +// 用例可以保证存在这样的划分方法。 +// +// +// +// 示例 1: +// +// +//输入:nums = [5,0,3,8,6] +//输出:3 +//解释:left = [5,0,3],right = [8,6] +// +// +// 示例 2: +// +// +//输入:nums = [1,1,1,0,6,12] +//输出:4 +//解释:left = [1,1,1,0],right = [6,12] +// +// +// +// +// 提示: +// +// +// 2 <= nums.length <= 10⁵ +// 0 <= nums[i] <= 10⁶ +// 可以保证至少有一种方法能够按题目所描述的那样对 nums 进行划分。 +// +// +// Related Topics 数组 👍 190 👎 0 + + +/** + * 915 分割数组 + * + * @author shang.liang + * @date 2022-10-24 18:29:36 + */ +public class PartitionArrayIntoDisjointIntervals { + public static void main(String[] args) { + Solution soution = new PartitionArrayIntoDisjointIntervals().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int partitionDisjoint(int[] nums) { + + int n = nums.length; + int[] min = new int[n + 10]; + min[n - 1] = nums[n - 1]; + for (int i = n - 2; i >= 0; i--) { + min[i] = Math.min(min[i + 1], nums[i]); + } + for (int i = 0, max = 0; i < n - 1; i++) { + max = Math.max(max, nums[i]); + if (max <= min[i + 1]) { + return i + 1; + } + } + + return -1; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From 0b6c6fe5c2fa194f98e3db24b1faafb72c2b9f73 Mon Sep 17 00:00:00 2001 From: shangliang Date: Wed, 26 Oct 2022 14:28:20 +0800 Subject: [PATCH 38/85] =?UTF-8?q?862=20=E5=92=8C=E8=87=B3=E5=B0=91?= =?UTF-8?q?=E4=B8=BA=20K=20=E7=9A=84=E6=9C=80=E7=9F=AD=E5=AD=90=E6=95=B0?= =?UTF-8?q?=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/ShortestSubarrayWithSumAtLeastK.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/com/leetcode/editor/cn/ShortestSubarrayWithSumAtLeastK.java diff --git a/src/com/leetcode/editor/cn/ShortestSubarrayWithSumAtLeastK.java b/src/com/leetcode/editor/cn/ShortestSubarrayWithSumAtLeastK.java new file mode 100644 index 0000000..83437e5 --- /dev/null +++ b/src/com/leetcode/editor/cn/ShortestSubarrayWithSumAtLeastK.java @@ -0,0 +1,82 @@ +package com.leetcode.editor.cn; + +//给你一个整数数组 nums 和一个整数 k ,找出 nums 中和至少为 k 的 最短非空子数组 ,并返回该子数组的长度。如果不存在这样的 子数组 ,返回 +//-1 。 +// +// 子数组 是数组中 连续 的一部分。 +// +// +// +// +// +// +// 示例 1: +// +// +//输入:nums = [1], k = 1 +//输出:1 +// +// +// 示例 2: +// +// +//输入:nums = [1,2], k = 4 +//输出:-1 +// +// +// 示例 3: +// +// +//输入:nums = [2,-1,2], k = 3 +//输出:3 +// +// +// +// +// 提示: +// +// +// 1 <= nums.length <= 10⁵ +// -10⁵ <= nums[i] <= 10⁵ +// 1 <= k <= 10⁹ +// +// +// Related Topics 队列 数组 二分查找 前缀和 滑动窗口 单调队列 堆(优先队列) 👍 520 👎 0 + + +import java.util.ArrayDeque; + +/** + * 862 和至少为 K 的最短子数组 + * + * @author shang.liang + * @date 2022-10-26 12:56:15 + */ +public class ShortestSubarrayWithSumAtLeastK { + public static void main(String[] args) { + Solution soution = new ShortestSubarrayWithSumAtLeastK().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int shortestSubarray(int[] nums, int k) { + int n = nums.length, ans = n + 1; + var s = new long[n + 1]; + for (var i = 0; i < n; ++i) + s[i + 1] = s[i] + nums[i]; // 计算前缀和 + var q = new ArrayDeque(); + for (var i = 0; i <= n; ++i) { + var curS = s[i]; + while (!q.isEmpty() && curS - s[q.peekFirst()] >= k) + ans = Math.min(ans, i - q.pollFirst()); // 优化一 + while (!q.isEmpty() && s[q.peekLast()] >= curS) + q.pollLast(); // 优化二 + q.addLast(i); + } + return ans > n ? -1 : ans; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From 8f15cd3fb5a545727436c70b286efe3180512783 Mon Sep 17 00:00:00 2001 From: shangliang Date: Sun, 30 Oct 2022 11:16:11 +0800 Subject: [PATCH 39/85] =?UTF-8?q?862=20=E5=92=8C=E8=87=B3=E5=B0=91?= =?UTF-8?q?=E4=B8=BA=20K=20=E7=9A=84=E6=9C=80=E7=9F=AD=E5=AD=90=E6=95=B0?= =?UTF-8?q?=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/ShortestSubarrayWithSumAtLeastK.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/com/leetcode/editor/cn/ShortestSubarrayWithSumAtLeastK.java b/src/com/leetcode/editor/cn/ShortestSubarrayWithSumAtLeastK.java index 83437e5..2188cb7 100644 --- a/src/com/leetcode/editor/cn/ShortestSubarrayWithSumAtLeastK.java +++ b/src/com/leetcode/editor/cn/ShortestSubarrayWithSumAtLeastK.java @@ -68,10 +68,8 @@ public int shortestSubarray(int[] nums, int k) { var q = new ArrayDeque(); for (var i = 0; i <= n; ++i) { var curS = s[i]; - while (!q.isEmpty() && curS - s[q.peekFirst()] >= k) - ans = Math.min(ans, i - q.pollFirst()); // 优化一 - while (!q.isEmpty() && s[q.peekLast()] >= curS) - q.pollLast(); // 优化二 + while (!q.isEmpty() && curS - s[q.peekFirst()] >= k) ans = Math.min(ans, i - q.pollFirst()); // 优化一 + while (!q.isEmpty() && s[q.peekLast()] >= curS) q.pollLast(); // 优化二 q.addLast(i); } return ans > n ? -1 : ans; From b314d90434bc0191789e64e5fc8297f9ae1b7a9e Mon Sep 17 00:00:00 2001 From: shangliang Date: Sun, 30 Oct 2022 11:48:31 +0800 Subject: [PATCH 40/85] =?UTF-8?q?862=20=E5=92=8C=E8=87=B3=E5=B0=91?= =?UTF-8?q?=E4=B8=BA=20K=20=E7=9A=84=E6=9C=80=E7=9F=AD=E5=AD=90=E6=95=B0?= =?UTF-8?q?=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/ShortestSubarrayWithSumAtLeastK.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/com/leetcode/editor/cn/ShortestSubarrayWithSumAtLeastK.java b/src/com/leetcode/editor/cn/ShortestSubarrayWithSumAtLeastK.java index 2188cb7..db919a5 100644 --- a/src/com/leetcode/editor/cn/ShortestSubarrayWithSumAtLeastK.java +++ b/src/com/leetcode/editor/cn/ShortestSubarrayWithSumAtLeastK.java @@ -1,3 +1,4 @@ +/* package com.leetcode.editor.cn; //给你一个整数数组 nums 和一个整数 k ,找出 nums 中和至少为 k 的 最短非空子数组 ,并返回该子数组的长度。如果不存在这样的 子数组 ,返回 @@ -46,12 +47,14 @@ import java.util.ArrayDeque; +*/ /** * 862 和至少为 K 的最短子数组 * * @author shang.liang * @date 2022-10-26 12:56:15 - */ + *//* + public class ShortestSubarrayWithSumAtLeastK { public static void main(String[] args) { Solution soution = new ShortestSubarrayWithSumAtLeastK().new Solution(); @@ -78,3 +81,4 @@ public int shortestSubarray(int[] nums, int k) { //leetcode submit region end(Prohibit modification and deletion) } +*/ From 7992b2b16c5e257a115973995a5a5c0e56716fdf Mon Sep 17 00:00:00 2001 From: shangliang Date: Sun, 30 Oct 2022 15:03:24 +0800 Subject: [PATCH 41/85] =?UTF-8?q?784=20=E5=AD=97=E6=AF=8D=E5=A4=A7?= =?UTF-8?q?=E5=B0=8F=E5=86=99=E5=85=A8=E6=8E=92=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/LetterCasePermutation.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/com/leetcode/editor/cn/LetterCasePermutation.java diff --git a/src/com/leetcode/editor/cn/LetterCasePermutation.java b/src/com/leetcode/editor/cn/LetterCasePermutation.java new file mode 100644 index 0000000..76503a3 --- /dev/null +++ b/src/com/leetcode/editor/cn/LetterCasePermutation.java @@ -0,0 +1,76 @@ +package com.leetcode.editor.cn; + +//给定一个字符串 s ,通过将字符串 s 中的每个字母转变大小写,我们可以获得一个新的字符串。 +// +// 返回 所有可能得到的字符串集合 。以 任意顺序 返回输出。 +// +// +// +// 示例 1: +// +// +//输入:s = "a1b2" +//输出:["a1b2", "a1B2", "A1b2", "A1B2"] +// +// +// 示例 2: +// +// +//输入: s = "3z4" +//输出: ["3z4","3Z4"] +// +// +// +// +// 提示: +// +// +// 1 <= s.length <= 12 +// s 由小写英文字母、大写英文字母和数字组成 +// +// +// Related Topics 位运算 字符串 回溯 👍 445 👎 0 + + +import java.util.ArrayList; +import java.util.List; + +/** + * 784 字母大小写全排列 + * + * @author shang.liang + * @date 2022-10-30 11:03:45 + */ +public class LetterCasePermutation { + public static void main(String[] args) { + Solution soution = new LetterCasePermutation().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + char[] cs; + List ans = new ArrayList<>(); + + public List letterCasePermutation(String s) { + cs = s.toCharArray(); + dfs(0, s.length(), new char[s.length()]); + return ans; + } + + void dfs(int idx, int n, char[] cur) { + if (idx == n) { + ans.add(String.valueOf(cur)); + return; + } + cur[idx] = cs[idx]; + dfs(idx + 1, n, cur); + if (Character.isLetter(cs[idx])) { + cur[idx] = (char) (cs[idx] ^ 32); + dfs(idx + 1, n, cur); + } + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From 4114a2fb01df95c779445b95dfb539922196e223 Mon Sep 17 00:00:00 2001 From: shangliang Date: Sat, 5 Nov 2022 12:12:23 +0800 Subject: [PATCH 42/85] =?UTF-8?q?1106=20=E8=A7=A3=E6=9E=90=E5=B8=83?= =?UTF-8?q?=E5=B0=94=E8=A1=A8=E8=BE=BE=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/ParsingABooleanExpression.java | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/com/leetcode/editor/cn/ParsingABooleanExpression.java diff --git a/src/com/leetcode/editor/cn/ParsingABooleanExpression.java b/src/com/leetcode/editor/cn/ParsingABooleanExpression.java new file mode 100644 index 0000000..243e35a --- /dev/null +++ b/src/com/leetcode/editor/cn/ParsingABooleanExpression.java @@ -0,0 +1,111 @@ +package com.leetcode.editor.cn; + +//给你一个以字符串形式表述的 布尔表达式(boolean) expression,返回该式的运算结果。 +// +// 有效的表达式需遵循以下约定: +// +// +// "t",运算结果为 True +// "f",运算结果为 False +// "!(expr)",运算过程为对内部表达式 expr 进行逻辑 非的运算(NOT) +// "&(expr1,expr2,...)",运算过程为对 2 个或以上内部表达式 expr1, expr2, ... 进行逻辑 与的运算(AND) +// "|(expr1,expr2,...)",运算过程为对 2 个或以上内部表达式 expr1, expr2, ... 进行逻辑 或的运算(OR) +// +// +// +// +// 示例 1: +// +// 输入:expression = "!(f)" +//输出:true +// +// +// 示例 2: +// +// 输入:expression = "|(f,t)" +//输出:true +// +// +// 示例 3: +// +// 输入:expression = "&(t,f)" +//输出:false +// +// +// 示例 4: +// +// 输入:expression = "|(&(t,f,t),!(t))" +//输出:false +// +// +// +// +// 提示: +// +// +// 1 <= expression.length <= 20000 +// expression[i] 由 {'(', ')', '&', '|', '!', 't', 'f', ','} 中的字符组成。 +// expression 是以上述形式给出的有效表达式,表示一个布尔值。 +// +// +// Related Topics 栈 递归 字符串 👍 112 👎 0 + + +import java.util.ArrayDeque; +import java.util.Deque; + +/** + * 1106 解析布尔表达式 + * + * @author shang.liang + * @date 2022-11-05 12:10:53 + */ +public class ParsingABooleanExpression { + public static void main(String[] args) { + Solution soution = new ParsingABooleanExpression().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public boolean parseBoolExpr(String expression) { + Deque stack = new ArrayDeque(); + int n = expression.length(); + for (int i = 0; i < n; i++) { + char c = expression.charAt(i); + if (c == ',') { + continue; + } else if (c != ')') { + stack.push(c); + } else { + int t = 0, f = 0; + while (stack.peek() != '(') { + char val = stack.pop(); + if (val == 't') { + t++; + } else { + f++; + } + } + stack.pop(); + char op = stack.pop(); + switch (op) { + case '!': + stack.push(f == 1 ? 't' : 'f'); + break; + case '&': + stack.push(f == 0 ? 't' : 'f'); + break; + case '|': + stack.push(t > 0 ? 't' : 'f'); + break; + default: + } + } + } + return stack.pop() == 't'; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From 58c47c2b4eeed7059e414f181f24583f785a5772 Mon Sep 17 00:00:00 2001 From: shangliang Date: Sun, 13 Nov 2022 20:50:30 +0800 Subject: [PATCH 43/85] =?UTF-8?q?791=20=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/CustomSortString.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/com/leetcode/editor/cn/CustomSortString.java diff --git a/src/com/leetcode/editor/cn/CustomSortString.java b/src/com/leetcode/editor/cn/CustomSortString.java new file mode 100644 index 0000000..cf5541e --- /dev/null +++ b/src/com/leetcode/editor/cn/CustomSortString.java @@ -0,0 +1,77 @@ +package com.leetcode.editor.cn; + +//给定两个字符串 order 和 s 。order 的所有单词都是 唯一 的,并且以前按照一些自定义的顺序排序。 +// +// 对 s 的字符进行置换,使其与排序的 order 相匹配。更具体地说,如果在 order 中的字符 x 出现字符 y 之前,那么在排列后的字符串中, x +//也应该出现在 y 之前。 +// +// 返回 满足这个性质的 s 的任意排列 。 +// +// +// +// 示例 1: +// +// +//输入: order = "cba", s = "abcd" +//输出: "cbad" +//解释: +//“a”、“b”、“c”是按顺序出现的,所以“a”、“b”、“c”的顺序应该是“c”、“b”、“a”。 +//因为“d”不是按顺序出现的,所以它可以在返回的字符串中的任何位置。“dcba”、“cdba”、“cbda”也是有效的输出。 +// +// 示例 2: +// +// +//输入: order = "cbafg", s = "abcd" +//输出: "cbad" +// +// +// +// +// 提示: +// +// +// 1 <= order.length <= 26 +// 1 <= s.length <= 200 +// order 和 s 由小写英文字母组成 +// order 中的所有字符都 不同 +// +// +// Related Topics 哈希表 字符串 排序 👍 163 👎 0 + + +import java.util.Arrays; + +/** + * 791 自定义字符串排序 + * + * @author shang.liang + * @date 2022-11-13 20:47:51 + */ +public class CustomSortString { + public static void main(String[] args) { + Solution soution = new CustomSortString().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public String customSortString(String order, String s) { + int[] val = new int[26]; + for (int i = 0; i < order.length(); ++i) { + val[order.charAt(i) - 'a'] = i + 1; + } + Character[] arr = new Character[s.length()]; + for (int i = 0; i < s.length(); ++i) { + arr[i] = s.charAt(i); + } + Arrays.sort(arr, (c0, c1) -> val[c0 - 'a'] - val[c1 - 'a']); + StringBuilder ans = new StringBuilder(); + for (int i = 0; i < s.length(); ++i) { + ans.append(arr[i]); + } + return ans.toString(); + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From 716df294ac01ee9ab5be7dd86ecc353e941d97aa Mon Sep 17 00:00:00 2001 From: shangliang Date: Sat, 19 Nov 2022 15:35:04 +0800 Subject: [PATCH 44/85] =?UTF-8?q?1732=20=E6=89=BE=E5=88=B0=E6=9C=80?= =?UTF-8?q?=E9=AB=98=E6=B5=B7=E6=8B=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/FindTheHighestAltitude.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/com/leetcode/editor/cn/FindTheHighestAltitude.java diff --git a/src/com/leetcode/editor/cn/FindTheHighestAltitude.java b/src/com/leetcode/editor/cn/FindTheHighestAltitude.java new file mode 100644 index 0000000..140f15d --- /dev/null +++ b/src/com/leetcode/editor/cn/FindTheHighestAltitude.java @@ -0,0 +1,67 @@ +package com.leetcode.editor.cn; + +//有一个自行车手打算进行一场公路骑行,这条路线总共由 n + 1 个不同海拔的点组成。自行车手从海拔为 0 的点 0 开始骑行。 +// +// 给你一个长度为 n 的整数数组 gain ,其中 gain[i] 是点 i 和点 i + 1 的 净海拔高度差(0 <= i < n)。请你返回 最高点的 +//海拔 。 +// +// +// +// 示例 1: +// +// +//输入:gain = [-5,1,5,0,-7] +//输出:1 +//解释:海拔高度依次为 [0,-5,-4,1,1,-6] 。最高海拔为 1 。 +// +// +// 示例 2: +// +// +//输入:gain = [-4,-3,-2,-1,4,3,2] +//输出:0 +//解释:海拔高度依次为 [0,-4,-7,-9,-10,-6,-3,-1] 。最高海拔为 0 。 +// +// +// +// +// 提示: +// +// +// n == gain.length +// 1 <= n <= 100 +// -100 <= gain[i] <= 100 +// +// +// Related Topics 数组 前缀和 👍 47 👎 0 + + +/** + * 1732 找到最高海拔 + * @date 2022-11-19 15:32:32 + * @author shang.liang + */ + public class FindTheHighestAltitude{ + public static void main(String[] args){ + Solution soution = new FindTheHighestAltitude().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int largestAltitude(int[] gain) { + + int current = 0; + int max = 0; + + for (int g : gain) { + current += g; + max = Math.max(max, current); + } + + return max; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } From 0e673b7e88730c1eb99232897e78c891274bea84 Mon Sep 17 00:00:00 2001 From: shangliang Date: Thu, 24 Nov 2022 13:30:45 +0800 Subject: [PATCH 45/85] =?UTF-8?q?795=20=E5=8C=BA=E9=97=B4=E5=AD=90?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E4=B8=AA=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NumberOfSubarraysWithBoundedMaximum.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/com/leetcode/editor/cn/NumberOfSubarraysWithBoundedMaximum.java diff --git a/src/com/leetcode/editor/cn/NumberOfSubarraysWithBoundedMaximum.java b/src/com/leetcode/editor/cn/NumberOfSubarraysWithBoundedMaximum.java new file mode 100644 index 0000000..b47c770 --- /dev/null +++ b/src/com/leetcode/editor/cn/NumberOfSubarraysWithBoundedMaximum.java @@ -0,0 +1,69 @@ +package com.leetcode.editor.cn; + +//给你一个整数数组 nums 和两个整数:left 及 right 。找出 nums 中连续、非空且其中最大元素在范围 [left, right] 内的子数组 +//,并返回满足条件的子数组的个数。 +// +// 生成的测试用例保证结果符合 32-bit 整数范围。 +// +// +// +// 示例 1: +// +// +//输入:nums = [2,1,4,3], left = 2, right = 3 +//输出:3 +//解释:满足条件的三个子数组:[2], [2, 1], [3] +// +// +// 示例 2: +// +// +//输入:nums = [2,9,2,5,6], left = 2, right = 8 +//输出:7 +// +// +// +// +// 提示: +// +// +// 1 <= nums.length <= 10⁵ +// 0 <= nums[i] <= 10⁹ +// 0 <= left <= right <= 10⁹ +// +// +// Related Topics 数组 双指针 👍 265 👎 0 + + +/** + * 795 区间子数组个数 + * @date 2022-11-24 13:25:36 + * @author shang.liang + */ + public class NumberOfSubarraysWithBoundedMaximum{ + public static void main(String[] args){ + Solution soution = new NumberOfSubarraysWithBoundedMaximum().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int numSubarrayBoundedMax(int[] nums, int left, int right) { + int res = 0, last2 = -1, last1 = -1; + for (int i = 0; i < nums.length; i++) { + if (nums[i] >= left && nums[i] <= right) { + last1 = i; + } else if (nums[i] > right) { + last2 = i; + last1 = -1; + } + if (last1 != -1) { + res += last1 - last2; + } + } + return res; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } From 58651e2ed0a1c646d6319e31e4e646470f23de59 Mon Sep 17 00:00:00 2001 From: shangliang Date: Sat, 26 Nov 2022 15:04:27 +0800 Subject: [PATCH 46/85] =?UTF-8?q?882=20=E7=BB=86=E5=88=86=E5=9B=BE?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E5=8F=AF=E5=88=B0=E8=BE=BE=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/ReachableNodesInSubdividedGraph.java | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/com/leetcode/editor/cn/ReachableNodesInSubdividedGraph.java diff --git a/src/com/leetcode/editor/cn/ReachableNodesInSubdividedGraph.java b/src/com/leetcode/editor/cn/ReachableNodesInSubdividedGraph.java new file mode 100644 index 0000000..cd592ad --- /dev/null +++ b/src/com/leetcode/editor/cn/ReachableNodesInSubdividedGraph.java @@ -0,0 +1,119 @@ +package com.leetcode.editor.cn; + +//给你一个无向图(原始图),图中有 n 个节点,编号从 0 到 n - 1 。你决定将图中的每条边 细分 为一条节点链,每条边之间的新节点数各不相同。 +// +// 图用由边组成的二维数组 edges 表示,其中 edges[i] = [ui, vi, cnti] 表示原始图中节点 ui 和 vi 之间存在一条边, +//cnti 是将边 细分 后的新节点总数。注意,cnti == 0 表示边不可细分。 +// +// 要 细分 边 [ui, vi] ,需要将其替换为 (cnti + 1) 条新边,和 cnti 个新节点。新节点为 x1, x2, ..., xcnti , +//新边为 [ui, x1], [x1, x2], [x2, x3], ..., [xcnti+1, xcnti], [xcnti, vi] 。 +// +// 现在得到一个 新的细分图 ,请你计算从节点 0 出发,可以到达多少个节点?如果节点间距离是 maxMoves 或更少,则视为 可以到达 。 +// +// 给你原始图和 maxMoves ,返回 新的细分图中从节点 0 出发 可到达的节点数 。 +// +// +// +// 示例 1: +// +// +//输入:edges = [[0,1,10],[0,2,1],[1,2,2]], maxMoves = 6, n = 3 +//输出:13 +//解释:边的细分情况如上图所示。 +//可以到达的节点已经用黄色标注出来。 +// +// +// 示例 2: +// +// +//输入:edges = [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], maxMoves = 10, n = 4 +//输出:23 +// +// +// 示例 3: +// +// +//输入:edges = [[1,2,4],[1,4,5],[1,3,1],[2,3,4],[3,4,5]], maxMoves = 17, n = 5 +//输出:1 +//解释:节点 0 与图的其余部分没有连通,所以只有节点 0 可以到达。 +// +// +// +// +// 提示: +// +// +// 0 <= edges.length <= min(n * (n - 1) / 2, 10⁴) +// edges[i].length == 3 +// 0 <= ui < vi < n +// 图中 不存在平行边 +// 0 <= cnti <= 10⁴ +// 0 <= maxMoves <= 10⁹ +// 1 <= n <= 3000 +// +// +// Related Topics 图 最短路 堆(优先队列) 👍 109 👎 0 + + +import java.util.*; + +/** + * 882 细分图中的可到达节点 + * @date 2022-11-26 15:00:26 + * @author shang.liang + */ + public class ReachableNodesInSubdividedGraph{ + public static void main(String[] args){ + Solution soution = new ReachableNodesInSubdividedGraph().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int reachableNodes(int[][] edges, int maxMoves, int n) { + List[] adList = new List[n]; + for (int i = 0; i < n; i++) { + adList[i] = new ArrayList(); + } + for (int[] edge : edges) { + int u = edge[0], v = edge[1], nodes = edge[2]; + adList[u].add(new int[]{v, nodes}); + adList[v].add(new int[]{u, nodes}); + } + Map used = new HashMap(); + Set visited = new HashSet(); + int reachableNodes = 0; + PriorityQueue pq = new PriorityQueue((a, b) -> a[0] - b[0]); + pq.offer(new int[]{0, 0}); + + while (!pq.isEmpty() && pq.peek()[0] <= maxMoves) { + int[] pair = pq.poll(); + int step = pair[0], u = pair[1]; + if (!visited.add(u)) { + continue; + } + reachableNodes++; + for (int[] next : adList[u]) { + int v = next[0], nodes = next[1]; + if (nodes + step + 1 <= maxMoves && !visited.contains(v)) { + pq.offer(new int[]{nodes + step + 1, v}); + } + used.put(encode(u, v, n), Math.min(nodes, maxMoves - step)); + } + } + + for (int[] edge : edges) { + int u = edge[0], v = edge[1], nodes = edge[2]; + reachableNodes += Math.min(nodes, used.getOrDefault(encode(u, v, n), 0) + used.getOrDefault(encode(v, u, n), 0)); + } + return reachableNodes; + } + + public int encode(int u, int v, int n) { + return u * n + v; + } + +} +//leetcode submit region end(Prohibit modification and deletion) + + } From 97332e407318e318840eb0c907c6ed759d13d68c Mon Sep 17 00:00:00 2001 From: shangliang Date: Mon, 26 Dec 2022 22:15:15 +0800 Subject: [PATCH 47/85] =?UTF-8?q?1759=20=E7=BB=9F=E8=AE=A1=E5=90=8C?= =?UTF-8?q?=E6=9E=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/CountNumberOfHomogenousSubstrings.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/com/leetcode/editor/cn/CountNumberOfHomogenousSubstrings.java diff --git a/src/com/leetcode/editor/cn/CountNumberOfHomogenousSubstrings.java b/src/com/leetcode/editor/cn/CountNumberOfHomogenousSubstrings.java new file mode 100644 index 0000000..f73baad --- /dev/null +++ b/src/com/leetcode/editor/cn/CountNumberOfHomogenousSubstrings.java @@ -0,0 +1,85 @@ +package com.leetcode.editor.cn; + +//给你一个字符串 s ,返回 s 中 同构子字符串 的数目。由于答案可能很大,只需返回对 10⁹ + 7 取余 后的结果。 +// +// 同构字符串 的定义为:如果一个字符串中的所有字符都相同,那么该字符串就是同构字符串。 +// +// 子字符串 是字符串中的一个连续字符序列。 +// +// +// +// 示例 1: +// +// 输入:s = "abbcccaa" +//输出:13 +//解释:同构子字符串如下所列: +//"a" 出现 3 次。 +//"aa" 出现 1 次。 +//"b" 出现 2 次。 +//"bb" 出现 1 次。 +//"c" 出现 3 次。 +//"cc" 出现 2 次。 +//"ccc" 出现 1 次。 +//3 + 1 + 2 + 1 + 3 + 2 + 1 = 13 +// +// 示例 2: +// +// 输入:s = "xy" +//输出:2 +//解释:同构子字符串是 "x" 和 "y" 。 +// +// 示例 3: +// +// 输入:s = "zzzzz" +//输出:15 +// +// +// +// +// 提示: +// +// +// 1 <= s.length <= 10⁵ +// s 由小写字符串组成 +// +// +// Related Topics 数学 字符串 👍 61 👎 0 + + +/** + * 1759 统计同构子字符串的数目 + * + * @author shang.liang + * @date 2022-12-26 22:13:37 + */ +public class CountNumberOfHomogenousSubstrings { + public static void main(String[] args) { + Solution soution = new CountNumberOfHomogenousSubstrings().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int countHomogenous(String s) { + final int MOD = 1000000007; + long res = 0; + char prev = s.charAt(0); + int cnt = 0; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c == prev) { + cnt++; + } else { + res += (long) (cnt + 1) * cnt / 2; + cnt = 1; + prev = c; + } + } + res += (long) (cnt + 1) * cnt / 2; + return (int) (res % MOD); + + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From b114fd7205299f61f2a98f2d4209f2093208c641 Mon Sep 17 00:00:00 2001 From: shangliang Date: Sun, 8 Jan 2023 01:22:44 +0800 Subject: [PATCH 48/85] =?UTF-8?q?2185=20=E7=BB=9F=E8=AE=A1=E5=8C=85?= =?UTF-8?q?=E5=90=AB=E7=BB=99=E5=AE=9A=E5=89=8D=E7=BC=80=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/CountingWordsWithAGivenPrefix.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/com/leetcode/editor/cn/CountingWordsWithAGivenPrefix.java diff --git a/src/com/leetcode/editor/cn/CountingWordsWithAGivenPrefix.java b/src/com/leetcode/editor/cn/CountingWordsWithAGivenPrefix.java new file mode 100644 index 0000000..9135b58 --- /dev/null +++ b/src/com/leetcode/editor/cn/CountingWordsWithAGivenPrefix.java @@ -0,0 +1,64 @@ +package com.leetcode.editor.cn; + +//给你一个字符串数组 words 和一个字符串 pref 。 +// +// 返回 words 中以 pref 作为 前缀 的字符串的数目。 +// +// 字符串 s 的 前缀 就是 s 的任一前导连续字符串。 +// +// +// +// 示例 1: +// +// 输入:words = ["pay","attention","practice","attend"], pref = "at" +//输出:2 +//解释:以 "at" 作为前缀的字符串有两个,分别是:"attention" 和 "attend" 。 +// +// +// 示例 2: +// +// 输入:words = ["leetcode","win","loops","success"], pref = "code" +//输出:0 +//解释:不存在以 "code" 作为前缀的字符串。 +// +// +// +// +// 提示: +// +// +// 1 <= words.length <= 100 +// 1 <= words[i].length, pref.length <= 100 +// words[i] 和 pref 由小写英文字母组成 +// +// +// Related Topics 数组 字符串 👍 13 👎 0 + + +/** + * 2185 统计包含给定前缀的字符串 + * + * @author shang.liang + * @date 2023-01-08 01:21:33 + */ +public class CountingWordsWithAGivenPrefix { + public static void main(String[] args) { + Solution soution = new CountingWordsWithAGivenPrefix().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int prefixCount(String[] words, String pref) { + int res = 0; + for (String word : words) { + if (word.startsWith(pref)) { + res++; + } + } + return res; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From ce47ae413375a0ed1a2d1076ad8b0360be8b1869 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Tue, 7 May 2024 01:07:46 +0800 Subject: [PATCH 49/85] AddDigits --- src/com/leetcodeTwo/editor/cn/AddDigits.java | 69 ++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/AddDigits.java diff --git a/src/com/leetcodeTwo/editor/cn/AddDigits.java b/src/com/leetcodeTwo/editor/cn/AddDigits.java new file mode 100644 index 0000000..915fa37 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/AddDigits.java @@ -0,0 +1,69 @@ +package com.leetcodeTwo.editor.cn; + +//给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。返回这个结果。 +// +// +// +// 示例 1: +// +// +//输入: num = 38 +//输出: 2 +//解释: 各位相加的过程为: +//38 --> 3 + 8 --> 11 +//11 --> 1 + 1 --> 2 +//由于 2 是一位数,所以返回 2。 +// +// +// 示例 1: +// +// +//输入: num = 0 +//输出: 0 +// +// +// +// 提示: +// +// +// 0 <= num <= 2³¹ - 1 +// +// +// +// +// 进阶:你可以不使用循环或者递归,在 O(1) 时间复杂度内解决这个问题吗? +// Related Topics 数学 数论 模拟 👍 427 👎 0 + + +/** + * 258 各位相加 + * @date 2022-03-03 09:46:57 + * @author shang.liang + */ + public class AddDigits{ + public static void main(String[] args){ + Solution soution = new AddDigits().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int addDigits(int num) { + + if (num < 10) { + return num; + } + + int x = 0; + + while (num != 0) { + x += num % 10; + num = num / 10; + } + + return addDigits(x); + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } From a57bd9f09db9b27ff032163eb91e1e0c487d5d85 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Tue, 2 Jan 2024 01:10:03 +0800 Subject: [PATCH 50/85] AddTwoNumbers --- .../leetcodeTwo/editor/cn/AddTwoNumbers.java | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/AddTwoNumbers.java diff --git a/src/com/leetcodeTwo/editor/cn/AddTwoNumbers.java b/src/com/leetcodeTwo/editor/cn/AddTwoNumbers.java new file mode 100644 index 0000000..ce3eb82 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/AddTwoNumbers.java @@ -0,0 +1,110 @@ +package com.leetcode.editor.cn; + +//给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。 +// +// 请你将两个数相加,并以相同形式返回一个表示和的链表。 +// +// 你可以假设除了数字 0 之外,这两个数都不会以 0 开头。 +// +// +// +// 示例 1: +// +// +//输入:l1 = [2,4,3], l2 = [5,6,4] +//输出:[7,0,8] +//解释:342 + 465 = 807. +// +// +// 示例 2: +// +// +//输入:l1 = [0], l2 = [0] +//输出:[0] +// +// +// 示例 3: +// +// +//输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] +//输出:[8,9,9,9,0,0,0,1] +// +// +// +// +// 提示: +// +// +// 每个链表中的节点数在范围 [1, 100] 内 +// 0 <= Node.val <= 9 +// 题目数据保证列表表示的数字不含前导零 +// +// Related Topics 递归 链表 数学 👍 7310 👎 0 + + +import com.liang.leetcode.bean.ListNode; + +/** + * 2 两数相加 + * + * @author shang.liang + * @date 2022-01-05 22:06:10 + */ +public class AddTwoNumbers { + public static void main(String[] args) { + Solution soution = new AddTwoNumbers().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + + /** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ + class Solution { + + private int flag = 0; + + public ListNode addTwoNumbers(ListNode l1, ListNode l2) { + + if (l1 == null && l2 == null) { + + if (flag == 1) { + return new ListNode(flag, null); + } + + return null; + } + + int value = flag; + flag = 0; + + if (l1 != null) { + value += l1.val; + } + if (l2 != null) { + value += l2.val; + } + + if (value >= 10) { + value = value % 10; + flag = 1; + } + + ListNode listNode = new ListNode(value, addTwoNumbers(l1 == null ? null : l1.next + , l2 == null ? null : l2.next)); + + + return listNode; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From b1a34096336e734047ea924e3cad2de4b1ce21b7 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Tue, 2 Jan 2024 01:10:10 +0800 Subject: [PATCH 51/85] ArrayOfDoubledPairs --- .../editor/cn/ArrayOfDoubledPairs.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/ArrayOfDoubledPairs.java diff --git a/src/com/leetcodeTwo/editor/cn/ArrayOfDoubledPairs.java b/src/com/leetcodeTwo/editor/cn/ArrayOfDoubledPairs.java new file mode 100644 index 0000000..75f9beb --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/ArrayOfDoubledPairs.java @@ -0,0 +1,90 @@ +package com.leetcode.editor.cn; + +//给定一个长度为偶数的整数数组 arr,只有对 arr 进行重组后可以满足 “对于每个 0 <= i < len(arr) / 2,都有 arr[2 * i +//+ 1] = 2 * arr[2 * i]” 时,返回 true;否则,返回 false。 +// +// +// +// 示例 1: +// +// +//输入:arr = [3,1,3,6] +//输出:false +// +// +// 示例 2: +// +// +//输入:arr = [2,1,2,6] +//输出:false +// +// +// 示例 3: +// +// +//输入:arr = [4,-2,2,-4] +//输出:true +//解释:可以用 [-2,-4] 和 [2,4] 这两组组成 [-2,-4,2,4] 或是 [2,4,-2,-4] +// +// +// +// +// 提示: +// +// +// 0 <= arr.length <= 3 * 10⁴ +// arr.length 是偶数 +// -10⁵ <= arr[i] <= 10⁵ +// +// Related Topics 贪心 数组 哈希表 排序 👍 158 👎 0 + + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; + +/** + * 954 二倍数对数组 + * @date 2022-04-01 23:16:30 + * @author shang.liang + */ + public class ArrayOfDoubledPairs{ + public static void main(String[] args){ + Solution soution = new ArrayOfDoubledPairs().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public boolean canReorderDoubled(int[] arr) { + + HashMap map = new HashMap<>(); + + for (int i = 0; i < arr.length; i++) { + map.put(arr[i], map.getOrDefault(arr[i], 0) + 1); + } + + List list = new ArrayList<>(); + for (Integer num :map.keySet()){ + list.add(num); + } + + Collections.sort(list, (a, b) -> Math.abs(a) - Math.abs(b)); + + for (Integer integer : list) { + + if (map.getOrDefault(integer * 2, 0) < map.get(integer)) { + return false; + } + + map.put(integer * 2, map.getOrDefault(integer * 2, 0) - map.get(integer)); + + } + + return true; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } From 3e6f9e4ad147c458978d9554eada20f8645f55f4 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Tue, 2 Jan 2024 01:10:18 +0800 Subject: [PATCH 52/85] BaseballGame --- .../leetcodeTwo/editor/cn/BaseballGame.java | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/BaseballGame.java diff --git a/src/com/leetcodeTwo/editor/cn/BaseballGame.java b/src/com/leetcodeTwo/editor/cn/BaseballGame.java new file mode 100644 index 0000000..529dda3 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/BaseballGame.java @@ -0,0 +1,118 @@ +package com.leetcode.editor.cn; + +//你现在是一场采用特殊赛制棒球比赛的记录员。这场比赛由若干回合组成,过去几回合的得分可能会影响以后几回合的得分。 +// +// 比赛开始时,记录是空白的。你会得到一个记录操作的字符串列表 ops,其中 ops[i] 是你需要记录的第 i 项操作,ops 遵循下述规则: +// +// +// 整数 x - 表示本回合新获得分数 x +// "+" - 表示本回合新获得的得分是前两次得分的总和。题目数据保证记录此操作时前面总是存在两个有效的分数。 +// "D" - 表示本回合新获得的得分是前一次得分的两倍。题目数据保证记录此操作时前面总是存在一个有效的分数。 +// "C" - 表示前一次得分无效,将其从记录中移除。题目数据保证记录此操作时前面总是存在一个有效的分数。 +// +// +// 请你返回记录中所有得分的总和。 +// +// +// +// 示例 1: +// +// +//输入:ops = ["5","2","C","D","+"] +//输出:30 +//解释: +//"5" - 记录加 5 ,记录现在是 [5] +//"2" - 记录加 2 ,记录现在是 [5, 2] +//"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5]. +//"D" - 记录加 2 * 5 = 10 ,记录现在是 [5, 10]. +//"+" - 记录加 5 + 10 = 15 ,记录现在是 [5, 10, 15]. +//所有得分的总和 5 + 10 + 15 = 30 +// +// +// 示例 2: +// +// +//输入:ops = ["5","-2","4","C","D","9","+","+"] +//输出:27 +//解释: +//"5" - 记录加 5 ,记录现在是 [5] +//"-2" - 记录加 -2 ,记录现在是 [5, -2] +//"4" - 记录加 4 ,记录现在是 [5, -2, 4] +//"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5, -2] +//"D" - 记录加 2 * -2 = -4 ,记录现在是 [5, -2, -4] +//"9" - 记录加 9 ,记录现在是 [5, -2, -4, 9] +//"+" - 记录加 -4 + 9 = 5 ,记录现在是 [5, -2, -4, 9, 5] +//"+" - 记录加 9 + 5 = 14 ,记录现在是 [5, -2, -4, 9, 5, 14] +//所有得分的总和 5 + -2 + -4 + 9 + 5 + 14 = 27 +// +// +// 示例 3: +// +// +//输入:ops = ["1"] +//输出:1 +// +// +// +// +// 提示: +// +// +// 1 <= ops.length <= 1000 +// ops[i] 为 "C"、"D"、"+",或者一个表示整数的字符串。整数范围是 [-3 * 10⁴, 3 * 10⁴] +// 对于 "+" 操作,题目数据保证记录此操作时前面总是存在两个有效的分数 +// 对于 "C" 和 "D" 操作,题目数据保证记录此操作时前面总是存在一个有效的分数 +// +// Related Topics 栈 数组 模拟 👍 222 👎 0 + + +/** + * 682 棒球比赛 + * @date 2022-03-26 12:07:50 + * @author shang.liang + */ + public class BaseballGame{ + public static void main(String[] args){ + Solution soution = new BaseballGame().new Solution(); + String[] ops = new String[]{"5","-2","4","C","D","9","+","+"}; + System.out.println("soution.calPoints(ops) = " + soution.calPoints(ops)); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int calPoints(String[] ops) { + + int res = 0; + int[] score = new int[ops.length]; + + for (int i = 0,index=0; i < ops.length; i++, index++) { + switch (ops[i]) { + case "+": { + score[index] = score[index - 1] + score[index - 2]; + res += score[index]; + break; + } + case "D":{ + score[index] = score[index - 1] * 2; + res += score[index]; + break; + } + case "C": { + res -= score[index - 1]; + index -= 2; + break; + } + default: { + score[index] = Integer.parseInt(ops[i]); + res += score[index]; + } + } + } + + return res; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } From 71042a1076fe0a72321b829350572dcad246d0f6 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Wed, 10 Jan 2024 11:12:27 +0800 Subject: [PATCH 53/85] BattleshipsInABoard --- .../editor/cn/BattleshipsInABoard.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/BattleshipsInABoard.java diff --git a/src/com/leetcodeTwo/editor/cn/BattleshipsInABoard.java b/src/com/leetcodeTwo/editor/cn/BattleshipsInABoard.java new file mode 100644 index 0000000..277eea2 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/BattleshipsInABoard.java @@ -0,0 +1,82 @@ +package com.leetcode.editor.cn;; + +//给你一个大小为 m x n 的矩阵 board 表示甲板,其中,每个单元格可以是一艘战舰 'X' 或者是一个空位 '.' ,返回在甲板 board 上放置的 +// 战舰 的数量。 +// +// 战舰 只能水平或者垂直放置在 board 上。换句话说,战舰只能按 1 x k(1 行,k 列)或 k x 1(k 行,1 列)的形状建造,其中 k 可以 +//是任意大小。两艘战舰之间至少有一个水平或垂直的空位分隔 (即没有相邻的战舰)。 +// +// +// +// 示例 1: +// +// +//输入:board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]] +//输出:2 +// +// +// 示例 2: +// +// +//输入:board = [["."]] +//输出:0 +// +// +// +// +// 提示: +// +// +// m == board.length +// n == board[i].length +// 1 <= m, n <= 200 +// board[i][j] 是 '.' 或 'X' +// +// +// +// +// 进阶:你可以实现一次扫描算法,并只使用 O(1) 额外空间,并且不修改 board 的值来解决这个问题吗? +// Related Topics 深度优先搜索 数组 矩阵 +// 👍 134 👎 0 + + +/** + * 419 甲板上的战舰 + * @date 2021-12-18 09:50:35 + * @author shang.liang + */ +public class BattleshipsInABoard{ + public static void main(String[] args) { + Solution solution = new BattleshipsInABoard().new Solution(); + + } + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int countBattleships(char[][] board) { + + int res = 0; + int row = board.length; + int col = board[0].length; + + for (int i = 0; i < row; i++) { + for (int j = 0; j < col; j++) { + if (board[i][j] != 'X') { + continue; + } + if (i > 0 && board[i - 1][j] == 'X') { + continue; + } + if (j > 0 && board[i][j - 1] == 'X') { + continue; + } + res++; + } + } + + return res; + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file From 2dff0080c95e8cac513e0141605daf260aa2c1ae Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Wed, 10 Jan 2024 11:12:33 +0800 Subject: [PATCH 54/85] BinaryNumberWithAlternatingBits --- .../cn/BinaryNumberWithAlternatingBits.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/BinaryNumberWithAlternatingBits.java diff --git a/src/com/leetcodeTwo/editor/cn/BinaryNumberWithAlternatingBits.java b/src/com/leetcodeTwo/editor/cn/BinaryNumberWithAlternatingBits.java new file mode 100644 index 0000000..073157c --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/BinaryNumberWithAlternatingBits.java @@ -0,0 +1,61 @@ +package com.leetcode.editor.cn; + +//给定一个正整数,检查它的二进制表示是否总是 0、1 交替出现:换句话说,就是二进制表示中相邻两位的数字永不相同。 +// +// +// +// 示例 1: +// +// +//输入:n = 5 +//输出:true +//解释:5 的二进制表示是:101 +// +// +// 示例 2: +// +// +//输入:n = 7 +//输出:false +//解释:7 的二进制表示是:111. +// +// 示例 3: +// +// +//输入:n = 11 +//输出:false +//解释:11 的二进制表示是:1011. +// +// +// +// 提示: +// +// +// 1 <= n <= 2³¹ - 1 +// +// Related Topics 位运算 👍 186 👎 0 + + +/** + * 693 交替位二进制数 + * @date 2022-03-28 21:54:45 + * @author shang.liang + */ + public class BinaryNumberWithAlternatingBits{ + public static void main(String[] args){ + Solution soution = new BinaryNumberWithAlternatingBits().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public boolean hasAlternatingBits(int n) { + + int a = n ^ n >> 1; + + return (a & (a + 1)) == 0; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } From e561ea8e77218f4b6c30423e18e30d53a34eb561 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Sun, 14 Jan 2024 15:34:44 +0800 Subject: [PATCH 55/85] ComplexNumberMultiplication --- .../cn/ComplexNumberMultiplication.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/ComplexNumberMultiplication.java diff --git a/src/com/leetcodeTwo/editor/cn/ComplexNumberMultiplication.java b/src/com/leetcodeTwo/editor/cn/ComplexNumberMultiplication.java new file mode 100644 index 0000000..e6c905c --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/ComplexNumberMultiplication.java @@ -0,0 +1,74 @@ +package com.leetcode.editor.cn; + +//复数 可以用字符串表示,遵循 "实部+虚部i" 的形式,并满足下述条件: +// +// +// 实部 是一个整数,取值范围是 [-100, 100] +// 虚部 也是一个整数,取值范围是 [-100, 100] +// i² == -1 +// +// +// 给你两个字符串表示的复数 num1 和 num2 ,请你遵循复数表示形式,返回表示它们乘积的字符串。 +// +// +// +// 示例 1: +// +// +//输入:num1 = "1+1i", num2 = "1+1i" +//输出:"0+2i" +//解释:(1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i ,你需要将它转换为 0+2i 的形式。 +// +// +// 示例 2: +// +// +//输入:num1 = "1+-1i", num2 = "1+-1i" +//输出:"0+-2i" +//解释:(1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i ,你需要将它转换为 0+-2i 的形式。 +// +// +// +// +// 提示: +// +// +// num1 和 num2 都是有效的复数表示。 +// +// Related Topics 数学 字符串 模拟 👍 123 👎 0 + + +/** + * 537 复数乘法 + * + * @author shang.liang + * @date 2022-02-25 22:53:28 + */ +public class ComplexNumberMultiplication { + public static void main(String[] args) { + Solution soution = new ComplexNumberMultiplication().new Solution(); + String nums1 = "1+1i"; + String nums2 = "1+1i"; + System.out.println("soution.complexNumberMultiply(nums1,nums2) = " + soution.complexNumberMultiply(nums1, nums2)); + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public String complexNumberMultiply(String num1, String num2) { + + String[] str1 = num1.split("\\+|i"); + String[] str2 = num2.split("[+i]"); + + // a + bi , c + di + int a = Integer.parseInt(str1[0]); + int b = Integer.parseInt(str1[1]); + int c = Integer.parseInt(str2[0]); + int d = Integer.parseInt(str2[1]); + + return (a * c - b * d) + "+" + (a * d + b * c) + "i"; + } + + } +//leetcode submit region end(Prohibit modification and deletion) + +} From 7c9b564e8f1e72c976cc2ac1d051f6bb01dfe33c Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Sun, 14 Jan 2024 15:34:50 +0800 Subject: [PATCH 56/85] ContainsDuplicateIi --- .../editor/cn/ContainsDuplicateIi.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/ContainsDuplicateIi.java diff --git a/src/com/leetcodeTwo/editor/cn/ContainsDuplicateIi.java b/src/com/leetcodeTwo/editor/cn/ContainsDuplicateIi.java new file mode 100644 index 0000000..ce4e8e5 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/ContainsDuplicateIi.java @@ -0,0 +1,72 @@ +package com.leetcode.editor.cn; + +//给你一个整数数组 nums 和一个整数 k ,判断数组中是否存在两个 不同的索引 i 和 j ,满足 nums[i] == nums[j] 且 abs(i +//- j) <= k 。如果存在,返回 true ;否则,返回 false 。 +// +// +// +// 示例 1: +// +// +//输入:nums = [1,2,3,1], k = 3 +//输出:true +// +// 示例 2: +// +// +//输入:nums = [1,0,1,1], k = 1 +//输出:true +// +// 示例 3: +// +// +//输入:nums = [1,2,3,1,2,3], k = 2 +//输出:false +// +// +// +// +// +// 提示: +// +// +// 1 <= nums.length <= 10⁵ +// -10⁹ <= nums[i] <= 10⁹ +// 0 <= k <= 10⁵ +// +// Related Topics 数组 哈希表 滑动窗口 👍 420 👎 0 + + +/** + * 219 存在重复元素 II + * + * @author shang.liang + * @date 2022-01-19 21:13:32 + */ +public class ContainsDuplicateIi { + public static void main(String[] args) { + Solution soution = new ContainsDuplicateIi().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public boolean containsNearbyDuplicate(int[] nums, int k) { + + for (int i = 0; i < nums.length; i++) { + + for (int j = i + 1; j <= Math.min(i + k, nums.length-1); j++) { + + if (nums[i] == nums[j]) { + return true; + } + + } + } + + return false; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From 055b9fbd02f29fb36f12fbf4e254d6ac3c9e77ff Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Sun, 14 Jan 2024 15:34:56 +0800 Subject: [PATCH 57/85] CountingWordsWithAGivenPrefix --- .../cn/CountingWordsWithAGivenPrefix.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/CountingWordsWithAGivenPrefix.java diff --git a/src/com/leetcodeTwo/editor/cn/CountingWordsWithAGivenPrefix.java b/src/com/leetcodeTwo/editor/cn/CountingWordsWithAGivenPrefix.java new file mode 100644 index 0000000..9135b58 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/CountingWordsWithAGivenPrefix.java @@ -0,0 +1,64 @@ +package com.leetcode.editor.cn; + +//给你一个字符串数组 words 和一个字符串 pref 。 +// +// 返回 words 中以 pref 作为 前缀 的字符串的数目。 +// +// 字符串 s 的 前缀 就是 s 的任一前导连续字符串。 +// +// +// +// 示例 1: +// +// 输入:words = ["pay","attention","practice","attend"], pref = "at" +//输出:2 +//解释:以 "at" 作为前缀的字符串有两个,分别是:"attention" 和 "attend" 。 +// +// +// 示例 2: +// +// 输入:words = ["leetcode","win","loops","success"], pref = "code" +//输出:0 +//解释:不存在以 "code" 作为前缀的字符串。 +// +// +// +// +// 提示: +// +// +// 1 <= words.length <= 100 +// 1 <= words[i].length, pref.length <= 100 +// words[i] 和 pref 由小写英文字母组成 +// +// +// Related Topics 数组 字符串 👍 13 👎 0 + + +/** + * 2185 统计包含给定前缀的字符串 + * + * @author shang.liang + * @date 2023-01-08 01:21:33 + */ +public class CountingWordsWithAGivenPrefix { + public static void main(String[] args) { + Solution soution = new CountingWordsWithAGivenPrefix().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int prefixCount(String[] words, String pref) { + int res = 0; + for (String word : words) { + if (word.startsWith(pref)) { + res++; + } + } + return res; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From f46cb7b08e1d71c8f3e020967db5b48875687e5e Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Sun, 14 Jan 2024 15:35:01 +0800 Subject: [PATCH 58/85] CountNumberOfHomogenousSubstrings --- .../cn/CountNumberOfHomogenousSubstrings.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/CountNumberOfHomogenousSubstrings.java diff --git a/src/com/leetcodeTwo/editor/cn/CountNumberOfHomogenousSubstrings.java b/src/com/leetcodeTwo/editor/cn/CountNumberOfHomogenousSubstrings.java new file mode 100644 index 0000000..f73baad --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/CountNumberOfHomogenousSubstrings.java @@ -0,0 +1,85 @@ +package com.leetcode.editor.cn; + +//给你一个字符串 s ,返回 s 中 同构子字符串 的数目。由于答案可能很大,只需返回对 10⁹ + 7 取余 后的结果。 +// +// 同构字符串 的定义为:如果一个字符串中的所有字符都相同,那么该字符串就是同构字符串。 +// +// 子字符串 是字符串中的一个连续字符序列。 +// +// +// +// 示例 1: +// +// 输入:s = "abbcccaa" +//输出:13 +//解释:同构子字符串如下所列: +//"a" 出现 3 次。 +//"aa" 出现 1 次。 +//"b" 出现 2 次。 +//"bb" 出现 1 次。 +//"c" 出现 3 次。 +//"cc" 出现 2 次。 +//"ccc" 出现 1 次。 +//3 + 1 + 2 + 1 + 3 + 2 + 1 = 13 +// +// 示例 2: +// +// 输入:s = "xy" +//输出:2 +//解释:同构子字符串是 "x" 和 "y" 。 +// +// 示例 3: +// +// 输入:s = "zzzzz" +//输出:15 +// +// +// +// +// 提示: +// +// +// 1 <= s.length <= 10⁵ +// s 由小写字符串组成 +// +// +// Related Topics 数学 字符串 👍 61 👎 0 + + +/** + * 1759 统计同构子字符串的数目 + * + * @author shang.liang + * @date 2022-12-26 22:13:37 + */ +public class CountNumberOfHomogenousSubstrings { + public static void main(String[] args) { + Solution soution = new CountNumberOfHomogenousSubstrings().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int countHomogenous(String s) { + final int MOD = 1000000007; + long res = 0; + char prev = s.charAt(0); + int cnt = 0; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c == prev) { + cnt++; + } else { + res += (long) (cnt + 1) * cnt / 2; + cnt = 1; + prev = c; + } + } + res += (long) (cnt + 1) * cnt / 2; + return (int) (res % MOD); + + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From 4180768f557bee93550f154f0f83cd32590113e4 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Sun, 14 Jan 2024 15:35:08 +0800 Subject: [PATCH 59/85] CountNumberOfPairsWithAbsoluteDifferenceK --- ...tNumberOfPairsWithAbsoluteDifferenceK.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/CountNumberOfPairsWithAbsoluteDifferenceK.java diff --git a/src/com/leetcodeTwo/editor/cn/CountNumberOfPairsWithAbsoluteDifferenceK.java b/src/com/leetcodeTwo/editor/cn/CountNumberOfPairsWithAbsoluteDifferenceK.java new file mode 100644 index 0000000..55aa4ca --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/CountNumberOfPairsWithAbsoluteDifferenceK.java @@ -0,0 +1,89 @@ +package com.leetcode.editor.cn; + +//给你一个整数数组 nums 和一个整数 k ,请你返回数对 (i, j) 的数目,满足 i < j 且 |nums[i] - nums[j]| == k 。 +// +// +// |x| 的值定义为: +// +// +// 如果 x >= 0 ,那么值为 x 。 +// 如果 x < 0 ,那么值为 -x 。 +// +// +// +// +// 示例 1: +// +// 输入:nums = [1,2,2,1], k = 1 +//输出:4 +//解释:差的绝对值为 1 的数对为: +//- [1,2,2,1] +//- [1,2,2,1] +//- [1,2,2,1] +//- [1,2,2,1] +// +// +// 示例 2: +// +// 输入:nums = [1,3], k = 3 +//输出:0 +//解释:没有任何数对差的绝对值为 3 。 +// +// +// 示例 3: +// +// 输入:nums = [3,2,1,5,4], k = 2 +//输出:3 +//解释:差的绝对值为 2 的数对为: +//- [3,2,1,5,4] +//- [3,2,1,5,4] +//- [3,2,1,5,4] +// +// +// +// +// 提示: +// +// +// 1 <= nums.length <= 200 +// 1 <= nums[i] <= 100 +// 1 <= k <= 99 +// +// Related Topics 数组 哈希表 计数 👍 26 👎 0 + + +/** + * 2006 差的绝对值为 K 的数对数目 + * + * @author shang.liang + * @date 2022-02-09 10:00:57 + */ +public class CountNumberOfPairsWithAbsoluteDifferenceK { + public static void main(String[] args) { + Solution soution = new CountNumberOfPairsWithAbsoluteDifferenceK().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int countKDifference(int[] nums, int k) { + + int tmpNum, res = 0; + + for (int i = 0; i < nums.length; i++) { + for (int j = i + 1; j < nums.length; j++) { + + tmpNum = nums[i] - nums[j]; + + if (tmpNum == k || tmpNum + k == 0) { + res++; + } + } + } + + return res; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From ccb393157f2a59af358e42bc47c2feeed82f485f Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Wed, 24 Jan 2024 10:22:43 +0800 Subject: [PATCH 60/85] CountVowelsPermutation --- .../editor/cn/CountVowelsPermutation.java | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/CountVowelsPermutation.java diff --git a/src/com/leetcodeTwo/editor/cn/CountVowelsPermutation.java b/src/com/leetcodeTwo/editor/cn/CountVowelsPermutation.java new file mode 100644 index 0000000..0dc8a6a --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/CountVowelsPermutation.java @@ -0,0 +1,104 @@ +package com.leetcode.editor.cn; + +//给你一个整数 n,请你帮忙统计一下我们可以按下述规则形成多少个长度为 n 的字符串: +// +// +// 字符串中的每个字符都应当是小写元音字母('a', 'e', 'i', 'o', 'u') +// 每个元音 'a' 后面都只能跟着 'e' +// 每个元音 'e' 后面只能跟着 'a' 或者是 'i' +// 每个元音 'i' 后面 不能 再跟着另一个 'i' +// 每个元音 'o' 后面只能跟着 'i' 或者是 'u' +// 每个元音 'u' 后面只能跟着 'a' +// +// +// 由于答案可能会很大,所以请你返回 模 10^9 + 7 之后的结果。 +// +// +// +// 示例 1: +// +// 输入:n = 1 +//输出:5 +//解释:所有可能的字符串分别是:"a", "e", "i" , "o" 和 "u"。 +// +// +// 示例 2: +// +// 输入:n = 2 +//输出:10 +//解释:所有可能的字符串分别是:"ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou" 和 "ua"。 +// +// +// 示例 3: +// +// 输入:n = 5 +//输出:68 +// +// +// +// 提示: +// +// +// 1 <= n <= 2 * 10^4 +// +// Related Topics 动态规划 👍 101 👎 0 + + +/** + * 1220 统计元音字母序列的数目 + * + * @author shang.liang + * @date 2022-01-17 16:12:33 + */ +public class CountVowelsPermutation { + public static void main(String[] args) { + Solution soution = new CountVowelsPermutation().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int countVowelPermutation(int n) { + + long mod = 1000000007; + + long[] dp = new long[5]; + + for (int i = 0; i < 5; i++) { + dp[i] = 1; + } + + long[] ndp = new long[5]; + /* + 每个元音 'a' 后面都只能跟着 'e' + 每个元音 'e' 后面只能跟着 'a' 或者是 'i' + 每个元音 'i' 后面 不能 再跟着另一个 'i' + 每个元音 'o' 后面只能跟着 'i' 或者是 'u' + 每个元音 'u' 后面只能跟着 'a' + */ + /* + 元音字母 ‘a’ 前面只能跟着 ‘e’,‘i’,‘u’; + 元音字母 ‘e’ 前面只能跟着 ‘a’,‘i’; + 每个元音 ‘i’ 前面只能跟着 ‘e’,‘o’; + 每个元音 ‘o’ 前面只能跟着 ‘i’; + 每个元音 ‘u’ 前面只能跟着 ‘o’,‘i’; + */ + for (int i = 2; i <= n; i++) { + ndp[0] = (dp[1] + dp[2] + dp[4]) % mod; + ndp[1] = (dp[0] + dp[2]) % mod; + ndp[2] = (dp[1] + dp[3]) % mod; + ndp[3] = dp[2] % mod; + ndp[4] = (dp[2] + dp[3]) % mod; + System.arraycopy(ndp, 0, dp, 0, 5); + } + + long ans = 0; + for (int i = 0; i < 5; ++i) { + ans = (ans + dp[i]) % mod; + } + return (int)ans; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From 2684d9c876cf6d05f3980ffdd4556fd14f395b67 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Wed, 24 Jan 2024 10:23:03 +0800 Subject: [PATCH 61/85] CustomSortString --- .../editor/cn/CustomSortString.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/CustomSortString.java diff --git a/src/com/leetcodeTwo/editor/cn/CustomSortString.java b/src/com/leetcodeTwo/editor/cn/CustomSortString.java new file mode 100644 index 0000000..cf5541e --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/CustomSortString.java @@ -0,0 +1,77 @@ +package com.leetcode.editor.cn; + +//给定两个字符串 order 和 s 。order 的所有单词都是 唯一 的,并且以前按照一些自定义的顺序排序。 +// +// 对 s 的字符进行置换,使其与排序的 order 相匹配。更具体地说,如果在 order 中的字符 x 出现字符 y 之前,那么在排列后的字符串中, x +//也应该出现在 y 之前。 +// +// 返回 满足这个性质的 s 的任意排列 。 +// +// +// +// 示例 1: +// +// +//输入: order = "cba", s = "abcd" +//输出: "cbad" +//解释: +//“a”、“b”、“c”是按顺序出现的,所以“a”、“b”、“c”的顺序应该是“c”、“b”、“a”。 +//因为“d”不是按顺序出现的,所以它可以在返回的字符串中的任何位置。“dcba”、“cdba”、“cbda”也是有效的输出。 +// +// 示例 2: +// +// +//输入: order = "cbafg", s = "abcd" +//输出: "cbad" +// +// +// +// +// 提示: +// +// +// 1 <= order.length <= 26 +// 1 <= s.length <= 200 +// order 和 s 由小写英文字母组成 +// order 中的所有字符都 不同 +// +// +// Related Topics 哈希表 字符串 排序 👍 163 👎 0 + + +import java.util.Arrays; + +/** + * 791 自定义字符串排序 + * + * @author shang.liang + * @date 2022-11-13 20:47:51 + */ +public class CustomSortString { + public static void main(String[] args) { + Solution soution = new CustomSortString().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public String customSortString(String order, String s) { + int[] val = new int[26]; + for (int i = 0; i < order.length(); ++i) { + val[order.charAt(i) - 'a'] = i + 1; + } + Character[] arr = new Character[s.length()]; + for (int i = 0; i < s.length(); ++i) { + arr[i] = s.charAt(i); + } + Arrays.sort(arr, (c0, c1) -> val[c0 - 'a'] - val[c1 - 'a']); + StringBuilder ans = new StringBuilder(); + for (int i = 0; i < s.length(); ++i) { + ans.append(arr[i]); + } + return ans.toString(); + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From 785be25e2c88a1f1b30b74626caf34c9f9a53ad2 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Wed, 31 Jan 2024 09:35:14 +0800 Subject: [PATCH 62/85] EvenOddTree --- .../leetcodeTwo/editor/cn/EvenOddTree.java | 161 ++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/EvenOddTree.java diff --git a/src/com/leetcodeTwo/editor/cn/EvenOddTree.java b/src/com/leetcodeTwo/editor/cn/EvenOddTree.java new file mode 100644 index 0000000..a588006 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/EvenOddTree.java @@ -0,0 +1,161 @@ +package com.leetcode.editor.cn; + +import com.liang.leetcode.bean.TreeNode; + +import java.util.HashMap; +import java.util.Map; + +; + +//如果一棵二叉树满足下述几个条件,则可以称为 奇偶树 : +// +// +// 二叉树根节点所在层下标为 0 ,根的子节点所在层下标为 1 ,根的孙节点所在层下标为 2 ,依此类推。 +// 偶数下标 层上的所有节点的值都是 奇 整数,从左到右按顺序 严格递增 +// 奇数下标 层上的所有节点的值都是 偶 整数,从左到右按顺序 严格递减 +// +// +// 给你二叉树的根节点,如果二叉树为 奇偶树 ,则返回 true ,否则返回 false 。 +// +// +// +// 示例 1: +// +// +// +// +//输入:root = [1,10,4,3,null,7,9,12,8,6,null,null,2] +//输出:true +//解释:每一层的节点值分别是: +//0 层:[1] +//1 层:[10,4] +//2 层:[3,7,9] +//3 层:[12,8,6,2] +//由于 0 层和 2 层上的节点值都是奇数且严格递增,而 1 层和 3 层上的节点值都是偶数且严格递减,因此这是一棵奇偶树。 +// +// +// 示例 2: +// +// +// +// +//输入:root = [5,4,2,3,3,7] +//输出:false +//解释:每一层的节点值分别是: +//0 层:[5] +//1 层:[4,2] +//2 层:[3,3,7] +//2 层上的节点值不满足严格递增的条件,所以这不是一棵奇偶树。 +// +// +// 示例 3: +// +// +// +// +//输入:root = [5,9,1,3,5,7] +//输出:false +//解释:1 层上的节点值应为偶数。 +// +// +// 示例 4: +// +// +//输入:root = [1] +//输出:true +// +// +// 示例 5: +// +// +//输入:root = [11,8,6,1,3,9,11,30,20,18,16,12,10,4,2,17] +//输出:true +// +// +// +// +// 提示: +// +// +// 树中节点数在范围 [1, 105] 内 +// 1 <= Node.val <= 106 +// +// Related Topics 树 广度优先搜索 二叉树 +// 👍 58 👎 0 + + +/** + * 1609 奇偶树 + * @date 2021-12-25 22:24:07 + * @author shang.liang + */ +public class EvenOddTree{ + public static void main(String[] args) { + Solution solution = new EvenOddTree().new Solution(); + + } + +//leetcode submit region begin(Prohibit modification and deletion) +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + + Map map = new HashMap<>(); + + public boolean isEvenOddTree(TreeNode root) { + return dfs(root, 0); + } + + public boolean dfs(TreeNode root, int index) { + + // 奇数层为 false , 偶数层为 true + boolean flag = index % 2 == 0; + + int curValue = root.val; + int preValue = map.getOrDefault(index, flag ? 0 : Integer.MAX_VALUE); + + /** + * 偶数行 + * 值为偶数 或者 值是递减,return false + */ + if (flag && (curValue % 2 == 0 || curValue <= preValue)) { + return false; + } + + /** + * 奇数行 + * 值为奇数 或者 值是递增,return false + */ + if (!flag && (curValue % 2 != 0 || curValue >= preValue)) { + return false; + } + + map.put(index, root.val); + + if (root.left != null && !dfs(root.left, index + 1)) { + return false; + } + + if (root.right != null && !dfs(root.right, index + 1)) { + return false; + } + + return true; + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file From 9c66cc0134a6893e2ec36bf338aecd5702ebdfe3 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Tue, 20 Feb 2024 17:44:22 +0800 Subject: [PATCH 63/85] FactorialTrailingZeroes --- .../editor/cn/FactorialTrailingZeroes.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/FactorialTrailingZeroes.java diff --git a/src/com/leetcodeTwo/editor/cn/FactorialTrailingZeroes.java b/src/com/leetcodeTwo/editor/cn/FactorialTrailingZeroes.java new file mode 100644 index 0000000..ae323b2 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/FactorialTrailingZeroes.java @@ -0,0 +1,74 @@ +package com.leetcode.editor.cn; + +//给定一个整数 n ,返回 n! 结果中尾随零的数量。 +// +// 提示 n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1 +// +// +// +// 示例 1: +// +// +//输入:n = 3 +//输出:0 +//解释:3! = 6 ,不含尾随 0 +// +// +// 示例 2: +// +// +//输入:n = 5 +//输出:1 +//解释:5! = 120 ,有一个尾随 0 +// +// +// 示例 3: +// +// +//输入:n = 0 +//输出:0 +// +// +// +// +// 提示: +// +// +// 0 <= n <= 10⁴ +// +// +// +// +// 进阶:你可以设计并实现对数时间复杂度的算法来解决此问题吗? +// Related Topics 数学 👍 651 👎 0 + + +/** + * 172 阶乘后的零 + * @date 2022-03-25 23:25:58 + * @author shang.liang + */ + public class FactorialTrailingZeroes{ + public static void main(String[] args){ + Solution soution = new FactorialTrailingZeroes().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int trailingZeroes(int n) { + + int res = 0; + + for (int i = 5; i <= n; i+=5) { + for (int j = i; j % 5 == 0; j /= 5) { + res++; + } + } + + return res; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } From e19218fe0ea5fb945080239b7ebf10c01e8063a0 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Tue, 20 Feb 2024 17:44:28 +0800 Subject: [PATCH 64/85] FindCenterOfStarGraph --- .../editor/cn/FindCenterOfStarGraph.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/FindCenterOfStarGraph.java diff --git a/src/com/leetcodeTwo/editor/cn/FindCenterOfStarGraph.java b/src/com/leetcodeTwo/editor/cn/FindCenterOfStarGraph.java new file mode 100644 index 0000000..b42cd97 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/FindCenterOfStarGraph.java @@ -0,0 +1,73 @@ +package com.leetcode.editor.cn; + +//有一个无向的 星型 图,由 n 个编号从 1 到 n 的节点组成。星型图有一个 中心 节点,并且恰有 n - 1 条边将中心节点与其他每个节点连接起来。 +// +// 给你一个二维整数数组 edges ,其中 edges[i] = [ui, vi] 表示在节点 ui 和 vi 之间存在一条边。请你找出并返回 edges +//所表示星型图的中心节点。 +// +// +// +// 示例 1: +// +// +//输入:edges = [[1,2],[2,3],[4,2]] +//输出:2 +//解释:如上图所示,节点 2 与其他每个节点都相连,所以节点 2 是中心节点。 +// +// +// 示例 2: +// +// +//输入:edges = [[1,2],[5,1],[1,3],[1,4]] +//输出:1 +// +// +// +// +// 提示: +// +// +// 3 <= n <= 10⁵ +// edges.length == n - 1 +// edges[i].length == 2 +// 1 <= ui, vi <= n +// ui != vi +// 题目数据给出的 edges 表示一个有效的星型图 +// +// Related Topics 图 👍 23 👎 0 + + +/** + * 1791 找出星型图的中心节点 + * @date 2022-02-18 09:22:47 + * @author shang.liang + */ + public class FindCenterOfStarGraph{ + public static void main(String[] args){ + Solution soution = new FindCenterOfStarGraph().new Solution(); + int[][] edges = new int[][]{{1, 2}, {2, 3}, {4, 2}}; + System.out.println("soution.findCenter(edges) = " + soution.findCenter(edges)); + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int findCenter(int[][] edges) { + + int[] numsLine = new int[edges.length + 2]; + + for (int[] edge : edges) { + numsLine[edge[0]]++; + numsLine[edge[1]]++; + } + + for (int i = 0; i < numsLine.length; i++) { + if (numsLine[i] == edges.length) { + return i; + } + } + return 1; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } From b8dbddca52d5bb3da10fe4485268a95ba377be36 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Wed, 28 Feb 2024 10:07:37 +0800 Subject: [PATCH 65/85] FindKPairsWithSmallestSums --- .../editor/cn/FindKPairsWithSmallestSums.java | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/FindKPairsWithSmallestSums.java diff --git a/src/com/leetcodeTwo/editor/cn/FindKPairsWithSmallestSums.java b/src/com/leetcodeTwo/editor/cn/FindKPairsWithSmallestSums.java new file mode 100644 index 0000000..1e2d652 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/FindKPairsWithSmallestSums.java @@ -0,0 +1,98 @@ +package com.leetcode.editor.cn; + +//给定两个以升序排列的整数数组 nums1 和 nums2 , 以及一个整数 k 。 +// +// 定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2 。 +// +// 请找到和最小的 k 个数对 (u1,v1), (u2,v2) ... (uk,vk) 。 +// +// +// +// 示例 1: +// +// +//输入: nums1 = [1,7,11], nums2 = [2,4,6], k = 3 +//输出: [1,2],[1,4],[1,6] +//解释: 返回序列中的前 3 对数: +// [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6] +// +// +// 示例 2: +// +// +//输入: nums1 = [1,1,2], nums2 = [1,2,3], k = 2 +//输出: [1,1],[1,1] +//解释: 返回序列中的前 2 对数: +// [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3] +// +// +// 示例 3: +// +// +//输入: nums1 = [1,2], nums2 = [3], k = 3 +//输出: [1,3],[2,3] +//解释: 也可能序列中所有的数对都被返回:[1,3],[2,3] +// +// +// +// +// 提示: +// +// +// 1 <= nums1.length, nums2.length <= 10⁴ +// -10⁹ <= nums1[i], nums2[i] <= 10⁹ +// nums1, nums2 均为升序排列 +// 1 <= k <= 1000 +// +// Related Topics 数组 堆(优先队列) 👍 268 👎 0 + + +import java.util.ArrayList; +import java.util.List; +import java.util.PriorityQueue; + +/** + * 373 查找和最小的K对数字 + * + * @author shang.liang + * @date 2022-01-14 10:01:19 + */ +public class FindKPairsWithSmallestSums { + public static void main(String[] args) { + Solution soution = new FindKPairsWithSmallestSums().new Solution(); + int[] nums1 = new int[]{1, 7, 11}; + int[] nums2 = new int[]{2, 4, 6}; + List> lists = soution.kSmallestPairs(nums1, nums2, 2); + System.out.println("lists = " + lists); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public List> kSmallestPairs(int[] nums1, int[] nums2, int k) { + PriorityQueue pq = new PriorityQueue<>(k, (o1, o2)->{ + return nums1[o1[0]] + nums2[o1[1]] - nums1[o2[0]] - nums2[o2[1]]; + }); + List> ans = new ArrayList<>(); + int m = nums1.length; + int n = nums2.length; + for (int i = 0; i < Math.min(m, k); i++) { + pq.offer(new int[]{i, 0}); + } + while (k-- > 0 && !pq.isEmpty()) { + int[] idxPair = pq.poll(); + List list = new ArrayList<>(); + list.add(nums1[idxPair[0]]); + list.add(nums2[idxPair[1]]); + ans.add(list); + if (idxPair[1] + 1 < n) { + pq.offer(new int[]{idxPair[0], idxPair[1] + 1}); + } + } + + return ans; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From f8605dade24eb627c511edd2d31ee2ad248a63b3 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Wed, 28 Feb 2024 10:07:43 +0800 Subject: [PATCH 66/85] FindMissingObservations --- .../editor/cn/FindMissingObservations.java | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/FindMissingObservations.java diff --git a/src/com/leetcodeTwo/editor/cn/FindMissingObservations.java b/src/com/leetcodeTwo/editor/cn/FindMissingObservations.java new file mode 100644 index 0000000..44537cc --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/FindMissingObservations.java @@ -0,0 +1,102 @@ +package com.leetcode.editor.cn; + +//现有一份 n + m 次投掷单个 六面 骰子的观测数据,骰子的每个面从 1 到 6 编号。观测数据中缺失了 n 份,你手上只拿到剩余 m 次投掷的数据。幸好 +//你有之前计算过的这 n + m 次投掷数据的 平均值 。 +// +// 给你一个长度为 m 的整数数组 rolls ,其中 rolls[i] 是第 i 次观测的值。同时给你两个整数 mean 和 n 。 +// +// 返回一个长度为 n 的数组,包含所有缺失的观测数据,且满足这 n + m 次投掷的 平均值 是 mean 。如果存在多组符合要求的答案,只需要返回其中任意 +//一组即可。如果不存在答案,返回一个空数组。 +// +// k 个数字的 平均值 为这些数字求和后再除以 k 。 +// +// 注意 mean 是一个整数,所以 n + m 次投掷的总和需要被 n + m 整除。 +// +// +// +// 示例 1: +// +// +//输入:rolls = [3,2,4,3], mean = 4, n = 2 +//输出:[6,6] +//解释:所有 n + m 次投掷的平均值是 (3 + 2 + 4 + 3 + 6 + 6) / 6 = 4 。 +// +// +// 示例 2: +// +// +//输入:rolls = [1,5,6], mean = 3, n = 4 +//输出:[2,3,2,2] +//解释:所有 n + m 次投掷的平均值是 (1 + 5 + 6 + 2 + 3 + 2 + 2) / 7 = 3 。 +// +// +// 示例 3: +// +// +//输入:rolls = [1,2,3,4], mean = 6, n = 4 +//输出:[] +//解释:无论丢失的 4 次数据是什么,平均值都不可能是 6 。 +// +// +// 示例 4: +// +// +//输入:rolls = [1], mean = 3, n = 1 +//输出:[5] +//解释:所有 n + m 次投掷的平均值是 (1 + 5) / 2 = 3 。 +// +// +// +// +// 提示: +// +// +// m == rolls.length +// 1 <= n, m <= 10⁵ +// 1 <= rolls[i], mean <= 6 +// +// Related Topics 数组 数学 模拟 👍 47 👎 0 + + +import java.util.Arrays; + +/** + * 2028 找出缺失的观测数据 + * + * @author shang.liang + * @date 2022-03-27 19:59:17 + */ +public class FindMissingObservations { + public static void main(String[] args) { + Solution soution = new FindMissingObservations().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int[] missingRolls(int[] rolls, int mean, int n) { + + int[] res = new int[n]; + + int sum = Arrays.stream(rolls).sum(); + int nTotal = mean * rolls.length - sum + n * mean; + + if (nTotal < n || nTotal > 6 * n) { + return new int[0]; + } + + int index = 0; + for (int i = 0; i < nTotal; i++) { + if (index == n) { + index = 0; + } + res[index++]++; + + } + + return res; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From 153c2aaf29174f981223a7643cdfeede2bec9892 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Tue, 5 Mar 2024 15:03:52 +0800 Subject: [PATCH 67/85] FindTheClosestPalindrome --- .../editor/cn/FindTheClosestPalindrome.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/FindTheClosestPalindrome.java diff --git a/src/com/leetcodeTwo/editor/cn/FindTheClosestPalindrome.java b/src/com/leetcodeTwo/editor/cn/FindTheClosestPalindrome.java new file mode 100644 index 0000000..e6dbce3 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/FindTheClosestPalindrome.java @@ -0,0 +1,58 @@ +package com.leetcode.editor.cn; + +//给定一个表示整数的字符串 n ,返回与它最近的回文整数(不包括自身)。如果不止一个,返回较小的那个。 +// +// “最近的”定义为两个整数差的绝对值最小。 +// +// +// +// 示例 1: +// +// +//输入: n = "123" +//输出: "121" +// +// +// 示例 2: +// +// +//输入: n = "1" +//输出: "0" +//解释: 0 和 2是最近的回文,但我们返回最小的,也就是 0。 +// +// +// +// +// 提示: +// +// +// 1 <= n.length <= 18 +// n 只由数字组成 +// n 不含前导 0 +// n 代表在 [1, 10¹⁸ - 1] 范围内的整数 +// +// Related Topics 数学 字符串 👍 163 👎 0 + + +/** + * 564 寻找最近的回文数 + * @date 2022-03-02 12:01:50 + * @author shang.liang + */ + public class FindTheClosestPalindrome{ + public static void main(String[] args){ + Solution soution = new FindTheClosestPalindrome().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public String nearestPalindromic(String n) { + + + return ""; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } From a08c1bb6e2af8ee3fa304c0636bb28c29076f924 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Fri, 8 Mar 2024 17:23:03 +0800 Subject: [PATCH 68/85] FindTheHighestAltitude --- .../editor/cn/FindTheHighestAltitude.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/FindTheHighestAltitude.java diff --git a/src/com/leetcodeTwo/editor/cn/FindTheHighestAltitude.java b/src/com/leetcodeTwo/editor/cn/FindTheHighestAltitude.java new file mode 100644 index 0000000..140f15d --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/FindTheHighestAltitude.java @@ -0,0 +1,67 @@ +package com.leetcode.editor.cn; + +//有一个自行车手打算进行一场公路骑行,这条路线总共由 n + 1 个不同海拔的点组成。自行车手从海拔为 0 的点 0 开始骑行。 +// +// 给你一个长度为 n 的整数数组 gain ,其中 gain[i] 是点 i 和点 i + 1 的 净海拔高度差(0 <= i < n)。请你返回 最高点的 +//海拔 。 +// +// +// +// 示例 1: +// +// +//输入:gain = [-5,1,5,0,-7] +//输出:1 +//解释:海拔高度依次为 [0,-5,-4,1,1,-6] 。最高海拔为 1 。 +// +// +// 示例 2: +// +// +//输入:gain = [-4,-3,-2,-1,4,3,2] +//输出:0 +//解释:海拔高度依次为 [0,-4,-7,-9,-10,-6,-3,-1] 。最高海拔为 0 。 +// +// +// +// +// 提示: +// +// +// n == gain.length +// 1 <= n <= 100 +// -100 <= gain[i] <= 100 +// +// +// Related Topics 数组 前缀和 👍 47 👎 0 + + +/** + * 1732 找到最高海拔 + * @date 2022-11-19 15:32:32 + * @author shang.liang + */ + public class FindTheHighestAltitude{ + public static void main(String[] args){ + Solution soution = new FindTheHighestAltitude().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int largestAltitude(int[] gain) { + + int current = 0; + int max = 0; + + for (int g : gain) { + current += g; + max = Math.max(max, current); + } + + return max; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } From e892d80e673730b3cf3babcb6151a80ef43d857d Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Tue, 12 Mar 2024 11:02:13 +0800 Subject: [PATCH 69/85] FriendsOfAppropriateAges --- .../editor/cn/FriendsOfAppropriateAges.java | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/FriendsOfAppropriateAges.java diff --git a/src/com/leetcodeTwo/editor/cn/FriendsOfAppropriateAges.java b/src/com/leetcodeTwo/editor/cn/FriendsOfAppropriateAges.java new file mode 100644 index 0000000..6afec8c --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/FriendsOfAppropriateAges.java @@ -0,0 +1,120 @@ +package com.leetcode.editor.cn; + +import java.util.Arrays; + +; + +//在社交媒体网站上有 n 个用户。给你一个整数数组 ages ,其中 ages[i] 是第 i 个用户的年龄。 +// +// 如果下述任意一个条件为真,那么用户 x 将不会向用户 y(x != y)发送好友请求: +// +// +// age[y] <= 0.5 * age[x] + 7 +// age[y] > age[x] +// age[y] > 100 && age[x] < 100 +// +// +// 否则,x 将会向 y 发送一条好友请求。 +// +// 注意,如果 x 向 y 发送一条好友请求,y 不必也向 x 发送一条好友请求。另外,用户不会向自己发送好友请求。 +// +// 返回在该社交媒体网站上产生的好友请求总数。 +// +// +// +// 示例 1: +// +// +//输入:ages = [16,16] +//输出:2 +//解释:2 人互发好友请求。 +// +// +// 示例 2: +// +// +//输入:ages = [16,17,18] +//输出:2 +//解释:产生的好友请求为 17 -> 16 ,18 -> 17 。 +// +// +// 示例 3: +// +// +//输入:ages = [20,30,100,110,120] +//输出:3 +//解释:产生的好友请求为 110 -> 100 ,120 -> 110 ,120 -> 100 。 +// +// +// +// +// 提示: +// +// +// n == ages.length +// 1 <= n <= 2 * 104 +// 1 <= ages[i] <= 120 +// +// Related Topics 数组 双指针 二分查找 排序 +// 👍 99 👎 0 + +/** + * 825 适龄的朋友 + * + * @author shang.liang + * @date 2021-12-27 10:17:10 + */ +public class FriendsOfAppropriateAges { + public static void main(String[] args) { + Solution solution = new FriendsOfAppropriateAges().new Solution(); + + int[] ages = new int[]{16, 17, 18}; + System.out.println("solution.numFriendRequests(ages) = " + solution.numFriendRequests(ages)); + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int numFriendRequests(int[] ages) { + + Arrays.sort(ages); + int n = ages.length, ans = 0; + for (int k = 0, i = 0, j = 0; k < n; k++) { + while (i < k && !check(ages[i], ages[k])) { + i++; + } + if (j < k) { + j = k; + } + while (j < n && check(ages[j], ages[k])) { + j++; + } + if (j > i) { + ans += j - i - 1; + } + } + return ans; + } + + public boolean check(int x, int y) { + + // age[y] <= 0.5 * age[x] + 7 + // age[y] > age[x] + // age[y] > 100 && age[x] < 100 + + if (y <= (0.5 * x + 7)) { + return false; + } + + if (y > x) { + return false; + } + + if ((x < 100) && (y > 100)) { + return false; + } + + return true; + } + } +//leetcode submit region end(Prohibit modification and deletion) +} \ No newline at end of file From 8d5c0f7789dd967858159ada4e80eacf215d65d1 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Thu, 21 Mar 2024 15:54:24 +0800 Subject: [PATCH 70/85] IncreasingTripletSubsequence --- .../cn/IncreasingTripletSubsequence.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/IncreasingTripletSubsequence.java diff --git a/src/com/leetcodeTwo/editor/cn/IncreasingTripletSubsequence.java b/src/com/leetcodeTwo/editor/cn/IncreasingTripletSubsequence.java new file mode 100644 index 0000000..2ce3ee0 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/IncreasingTripletSubsequence.java @@ -0,0 +1,75 @@ +package com.leetcode.editor.cn; + +//给你一个整数数组 nums ,判断这个数组中是否存在长度为 3 的递增子序列。 +// +// 如果存在这样的三元组下标 (i, j, k) 且满足 i < j < k ,使得 nums[i] < nums[j] < nums[k] ,返回 +//true ;否则,返回 false 。 +// +// +// +// 示例 1: +// +// +//输入:nums = [1,2,3,4,5] +//输出:true +//解释:任何 i < j < k 的三元组都满足题意 +// +// +// 示例 2: +// +// +//输入:nums = [5,4,3,2,1] +//输出:false +//解释:不存在满足题意的三元组 +// +// 示例 3: +// +// +//输入:nums = [2,1,5,0,4,6] +//输出:true +//解释:三元组 (3, 4, 5) 满足题意,因为 nums[3] == 0 < nums[4] == 4 < nums[5] == 6 +// +// +// +// +// 提示: +// +// +// 1 <= nums.length <= 5 * 10⁵ +// -2³¹ <= nums[i] <= 2³¹ - 1 +// +// +// +// +// 进阶:你能实现时间复杂度为 O(n) ,空间复杂度为 O(1) 的解决方案吗? +// Related Topics 贪心 数组 👍 489 👎 0 + + +/** + * 334 递增的三元子序列 + * + * @author shang.liang + * @date 2022-01-12 21:30:33 + */ +public class IncreasingTripletSubsequence { + public static void main(String[] args) { + Solution soution = new IncreasingTripletSubsequence().new Solution(); + int[] nums = new int[]{1, 5, 0, 6}; + System.out.println("soution.increasingTriplet(nums) = " + soution.increasingTriplet(nums)); + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public boolean increasingTriplet(int[] nums) { + int a = Integer.MAX_VALUE, b = Integer.MAX_VALUE; + + for (int x : nums) + if (x <= a) a = x; + else if (x <= b) b = x; + else return true; + return false; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From 6c7770fa1f8dd8515a08c1ac739f6aba2ccaa57d Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Thu, 21 Mar 2024 15:54:29 +0800 Subject: [PATCH 71/85] KnightProbabilityInChessboard --- .../cn/KnightProbabilityInChessboard.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/KnightProbabilityInChessboard.java diff --git a/src/com/leetcodeTwo/editor/cn/KnightProbabilityInChessboard.java b/src/com/leetcodeTwo/editor/cn/KnightProbabilityInChessboard.java new file mode 100644 index 0000000..2f8adfa --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/KnightProbabilityInChessboard.java @@ -0,0 +1,95 @@ +package com.leetcode.editor.cn; + +//在一个 n x n 的国际象棋棋盘上,一个骑士从单元格 (row, column) 开始,并尝试进行 k 次移动。行和列是 从 0 开始 的,所以左上单元格 +//是 (0,0) ,右下单元格是 (n - 1, n - 1) 。 +// +// 象棋骑士有8种可能的走法,如下图所示。每次移动在基本方向上是两个单元格,然后在正交方向上是一个单元格。 +// +// +// +// 每次骑士要移动时,它都会随机从8种可能的移动中选择一种(即使棋子会离开棋盘),然后移动到那里。 +// +// 骑士继续移动,直到它走了 k 步或离开了棋盘。 +// +// 返回 骑士在棋盘停止移动后仍留在棋盘上的概率 。 +// +// +// +// 示例 1: +// +// +//输入: n = 3, k = 2, row = 0, column = 0 +//输出: 0.0625 +//解释: 有两步(到(1,2),(2,1))可以让骑士留在棋盘上。 +//在每一个位置上,也有两种移动可以让骑士留在棋盘上。 +//骑士留在棋盘上的总概率是0.0625。 +// +// +// 示例 2: +// +// +//输入: n = 1, k = 0, row = 0, column = 0 +//输出: 1.00000 +// +// +// +// +// 提示: +// +// +// 1 <= n <= 25 +// 0 <= k <= 100 +// 0 <= row, column <= n +// +// Related Topics 动态规划 👍 195 👎 0 + + +/** + * 688 骑士在棋盘上的概率 + * + * @author shang.liang + * @date 2022-02-17 11:27:42 + */ +public class KnightProbabilityInChessboard { + public static void main(String[] args) { + Solution soution = new KnightProbabilityInChessboard().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + + int[][] dirs = {{-2, -1}, {-2, 1}, + {2, -1}, {2, 1}, {-1, -2}, + {-1, 2}, {1, -2}, {1, 2}}; + + public double knightProbability(int n, int k, int row, int column) { + + double[][][] dp = new double[n][n][k + 1]; + + for (int i = 0; i <= k; i++) { + for (int j = 0; j < n; j++) { + for (int m = 0; m < n; m++) { + if (i == 0) { + dp[j][m][i] = 1; + continue; + } + for (int[] dir : dirs) { + int jp = j + dir[0]; + int mp = m + dir[1]; + if (jp >= 0 && jp < n && mp >= 0 && mp < n) { + dp[j][m][i] += dp[jp][mp][i - 1] / 8; + } + } + + } + } + } + + return dp[row][column][k]; + } + + } +//leetcode submit region end(Prohibit modification and deletion) + +} From cceef3b055700b5017c80fc328ca11f29bd64f14 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Sun, 24 Mar 2024 12:08:39 +0800 Subject: [PATCH 72/85] LargestNumberAtLeastTwiceOfOthers --- .../cn/LargestNumberAtLeastTwiceOfOthers.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/LargestNumberAtLeastTwiceOfOthers.java diff --git a/src/com/leetcodeTwo/editor/cn/LargestNumberAtLeastTwiceOfOthers.java b/src/com/leetcodeTwo/editor/cn/LargestNumberAtLeastTwiceOfOthers.java new file mode 100644 index 0000000..81d3111 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/LargestNumberAtLeastTwiceOfOthers.java @@ -0,0 +1,86 @@ +package com.leetcode.editor.cn; + +//给你一个整数数组 nums ,其中总是存在 唯一的 一个最大整数 。 +// +// 请你找出数组中的最大元素并检查它是否 至少是数组中每个其他数字的两倍 。如果是,则返回 最大元素的下标 ,否则返回 -1 。 +// +// +// +// 示例 1: +// +// +//输入:nums = [3,6,1,0] +//输出:1 +//解释:6 是最大的整数,对于数组中的其他整数,6 大于数组中其他元素的两倍。6 的下标是 1 ,所以返回 1 。 +// +// +// 示例 2: +// +// +//输入:nums = [1,2,3,4] +//输出:-1 +//解释:4 没有超过 3 的两倍大,所以返回 -1 。 +// +// 示例 3: +// +// +//输入:nums = [1] +//输出:0 +//解释:因为不存在其他数字,所以认为现有数字 1 至少是其他数字的两倍。 +// +// +// +// +// 提示: +// +// +// 1 <= nums.length <= 50 +// 0 <= nums[i] <= 100 +// nums 中的最大元素是唯一的 +// +// Related Topics 数组 排序 👍 149 👎 0 + + +/** + * 747 至少是其他数字两倍的最大数 + * + * @author shang.liang + * @date 2022-01-13 21:36:15 + */ +public class LargestNumberAtLeastTwiceOfOthers { + public static void main(String[] args) { + Solution soution = new LargestNumberAtLeastTwiceOfOthers().new Solution(); + int[] nums = new int[]{0, 1, 1, 2}; + System.out.println("soution.dominantIndex(nums) = " + soution.dominantIndex(nums)); + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int dominantIndex(int[] nums) { + + int a = Integer.MIN_VALUE; + int b = Integer.MIN_VALUE; + int index = -1; + + for (int i = 0; i < nums.length; i++) { + + if (nums[i] < a) { + b = Math.max(b, nums[i]); + continue; + } + b = Math.max(a, b); + a = nums[i]; + index = i; + + } + + if (a < b * 2) { + return -1; + } + + return index; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From 603190f5cad1c4c7d07cf1ef818c31276e97fd15 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Sun, 24 Mar 2024 12:08:47 +0800 Subject: [PATCH 73/85] LetterCasePermutation --- .../editor/cn/LetterCasePermutation.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/LetterCasePermutation.java diff --git a/src/com/leetcodeTwo/editor/cn/LetterCasePermutation.java b/src/com/leetcodeTwo/editor/cn/LetterCasePermutation.java new file mode 100644 index 0000000..76503a3 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/LetterCasePermutation.java @@ -0,0 +1,76 @@ +package com.leetcode.editor.cn; + +//给定一个字符串 s ,通过将字符串 s 中的每个字母转变大小写,我们可以获得一个新的字符串。 +// +// 返回 所有可能得到的字符串集合 。以 任意顺序 返回输出。 +// +// +// +// 示例 1: +// +// +//输入:s = "a1b2" +//输出:["a1b2", "a1B2", "A1b2", "A1B2"] +// +// +// 示例 2: +// +// +//输入: s = "3z4" +//输出: ["3z4","3Z4"] +// +// +// +// +// 提示: +// +// +// 1 <= s.length <= 12 +// s 由小写英文字母、大写英文字母和数字组成 +// +// +// Related Topics 位运算 字符串 回溯 👍 445 👎 0 + + +import java.util.ArrayList; +import java.util.List; + +/** + * 784 字母大小写全排列 + * + * @author shang.liang + * @date 2022-10-30 11:03:45 + */ +public class LetterCasePermutation { + public static void main(String[] args) { + Solution soution = new LetterCasePermutation().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + char[] cs; + List ans = new ArrayList<>(); + + public List letterCasePermutation(String s) { + cs = s.toCharArray(); + dfs(0, s.length(), new char[s.length()]); + return ans; + } + + void dfs(int idx, int n, char[] cur) { + if (idx == n) { + ans.add(String.valueOf(cur)); + return; + } + cur[idx] = cs[idx]; + dfs(idx + 1, n, cur); + if (Character.isLetter(cs[idx])) { + cur[idx] = (char) (cs[idx] ^ 32); + dfs(idx + 1, n, cur); + } + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From aa73b5630e91d7470bd32ca00eab4a9cc397c15c Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Sun, 24 Mar 2024 12:08:53 +0800 Subject: [PATCH 74/85] LinkedListRandomNode --- .../editor/cn/LinkedListRandomNode.java | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/LinkedListRandomNode.java diff --git a/src/com/leetcodeTwo/editor/cn/LinkedListRandomNode.java b/src/com/leetcodeTwo/editor/cn/LinkedListRandomNode.java new file mode 100644 index 0000000..06d0832 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/LinkedListRandomNode.java @@ -0,0 +1,115 @@ +package com.leetcode.editor.cn; + +//给你一个单链表,随机选择链表的一个节点,并返回相应的节点值。每个节点 被选中的概率一样 。 +// +// 实现 Solution 类: +// +// +// Solution(ListNode head) 使用整数数组初始化对象。 +// int getRandom() 从链表中随机选择一个节点并返回该节点的值。链表中所有节点被选中的概率相等。 +// +// +// +// +// 示例: +// +// +//输入 +//["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"] +//[[[1, 2, 3]], [], [], [], [], []] +//输出 +//[null, 1, 3, 2, 2, 3] +// +//解释 +//Solution solution = new Solution([1, 2, 3]); +//solution.getRandom(); // 返回 1 +//solution.getRandom(); // 返回 3 +//solution.getRandom(); // 返回 2 +//solution.getRandom(); // 返回 2 +//solution.getRandom(); // 返回 3 +//// getRandom() 方法应随机返回 1、2、3中的一个,每个元素被返回的概率相等。 +// +// +// +// 提示: +// +// +// 链表中的节点数在范围 [1, 10⁴] 内 +// -10⁴ <= Node.val <= 10⁴ +// 至多调用 getRandom 方法 10⁴ 次 +// +// +// +// +// 进阶: +// +// +// 如果链表非常大且长度未知,该怎么处理? +// 你能否在不使用额外空间的情况下解决此问题? +// +// Related Topics 水塘抽样 链表 数学 随机化 👍 231 👎 0 + + +import com.liang.leetcode.bean.ListNode; + +import java.util.Random; + +/** + * 382 链表随机节点 + * + * @author shang.liang + * @date 2022-01-16 19:31:06 + */ +public class LinkedListRandomNode { + public static void main(String[] args) { + Solution soution = new LinkedListRandomNode().new Solution(new ListNode()); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + + /** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ + class Solution { + + ListNode head; + Random random; + + public Solution(ListNode head) { + this.head = head; + random = new Random(); + } + + public int getRandom() { + int res = 0; + int i = 1; + + for (ListNode node = head; node != null; node = node.next) { + + if (random.nextInt(i) == 0) { + res = node.val; + } + + i++; + } + + return res; + } + } + +/** + * Your Solution object will be instantiated and called as such: + * Solution obj = new Solution(head); + * int param_1 = obj.getRandom(); + */ +//leetcode submit region end(Prohibit modification and deletion) + +} From 643ebca10b0c90b3bbf185f1817031f43e196d6f Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Wed, 3 Apr 2024 17:12:02 +0800 Subject: [PATCH 75/85] LongestHappyString --- .../editor/cn/LongestHappyString.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/LongestHappyString.java diff --git a/src/com/leetcodeTwo/editor/cn/LongestHappyString.java b/src/com/leetcodeTwo/editor/cn/LongestHappyString.java new file mode 100644 index 0000000..1d0e6ce --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/LongestHappyString.java @@ -0,0 +1,89 @@ +package com.leetcode.editor.cn; + +//如果字符串中不含有任何 'aaa','bbb' 或 'ccc' 这样的字符串作为子串,那么该字符串就是一个「快乐字符串」。 +// +// 给你三个整数 a,b ,c,请你返回 任意一个 满足下列全部条件的字符串 s: +// +// +// s 是一个尽可能长的快乐字符串。 +// s 中 最多 有a 个字母 'a'、b 个字母 'b'、c 个字母 'c' 。 +// s 中只含有 'a'、'b' 、'c' 三种字母。 +// +// +// 如果不存在这样的字符串 s ,请返回一个空字符串 ""。 +// +// +// +// 示例 1: +// +// 输入:a = 1, b = 1, c = 7 +//输出:"ccaccbcc" +//解释:"ccbccacc" 也是一种正确答案。 +// +// +// 示例 2: +// +// 输入:a = 2, b = 2, c = 1 +//输出:"aabbc" +// +// +// 示例 3: +// +// 输入:a = 7, b = 1, c = 0 +//输出:"aabaa" +//解释:这是该测试用例的唯一正确答案。 +// +// +// +// 提示: +// +// +// 0 <= a, b, c <= 100 +// a + b + c > 0 +// +// Related Topics 贪心 字符串 堆(优先队列) 👍 168 👎 0 + + +import java.util.PriorityQueue; + +/** + * 1405 最长快乐字符串 + * + * @author shang.liang + * @date 2022-02-07 22:59:44 + */ +public class LongestHappyString { + public static void main(String[] args) { + Solution soution = new LongestHappyString().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public String longestDiverseString(int a, int b, int c) { + + PriorityQueue q = new PriorityQueue<>((x, y) -> y[1] - x[1]); + if (a > 0) q.add(new int[]{0, a}); + if (b > 0) q.add(new int[]{1, b}); + if (c > 0) q.add(new int[]{2, c}); + StringBuilder sb = new StringBuilder(); + while (!q.isEmpty()) { + int[] cur = q.poll(); + int n = sb.length(); + if (n >= 2 && sb.charAt(n - 1) - 'a' == cur[0] && sb.charAt(n - 2) - 'a' == cur[0]) { + if (q.isEmpty()) break; + int[] next = q.poll(); + sb.append((char) (next[0] + 'a')); + if (--next[1] != 0) q.add(next); + q.add(cur); + } else { + sb.append((char) (cur[0] + 'a')); + if (--cur[1] != 0) q.add(cur); + } + } + return sb.toString(); + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From c172bf05a3bd10efe0071db3ee287305b5ab542a Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Mon, 8 Apr 2024 11:35:12 +0800 Subject: [PATCH 76/85] LuckyNumbersInAMatrix --- .../editor/cn/LuckyNumbersInAMatrix.java | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/LuckyNumbersInAMatrix.java diff --git a/src/com/leetcodeTwo/editor/cn/LuckyNumbersInAMatrix.java b/src/com/leetcodeTwo/editor/cn/LuckyNumbersInAMatrix.java new file mode 100644 index 0000000..1a2f8a1 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/LuckyNumbersInAMatrix.java @@ -0,0 +1,94 @@ +package com.leetcode.editor.cn; + +//给你一个 m * n 的矩阵,矩阵中的数字 各不相同 。请你按 任意 顺序返回矩阵中的所有幸运数。 +// +// 幸运数是指矩阵中满足同时下列两个条件的元素: +// +// +// 在同一行的所有元素中最小 +// 在同一列的所有元素中最大 +// +// +// +// +// 示例 1: +// +// 输入:matrix = [[3,7,8],[9,11,13],[15,16,17]] +//输出:[15] +//解释:15 是唯一的幸运数,因为它是其所在行中的最小值,也是所在列中的最大值。 +// +// +// 示例 2: +// +// 输入:matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]] +//输出:[12] +//解释:12 是唯一的幸运数,因为它是其所在行中的最小值,也是所在列中的最大值。 +// +// +// 示例 3: +// +// 输入:matrix = [[7,8],[1,2]] +//输出:[7] +// +// +// +// +// 提示: +// +// +// m == mat.length +// n == mat[i].length +// 1 <= n, m <= 50 +// 1 <= matrix[i][j] <= 10^5 +// 矩阵中的所有元素都是不同的 +// +// Related Topics 数组 矩阵 👍 95 👎 0 + + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * 1380 矩阵中的幸运数 + * @date 2022-02-15 17:36:20 + * @author shang.liang + */ + public class LuckyNumbersInAMatrix{ + public static void main(String[] args){ + Solution soution = new LuckyNumbersInAMatrix().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public List luckyNumbers (int[][] matrix) { + + List res = new ArrayList<>(); + + int[] dpRow = new int[matrix.length]; + Arrays.fill(dpRow, Integer.MAX_VALUE); + int[] dpCol = new int[matrix[0].length]; + + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[i].length; j++) { + dpRow[i] = Math.min(dpRow[i], matrix[i][j]); + dpCol[j] = Math.max(dpCol[j], matrix[i][j]); + } + } + + + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[0].length; j++) { + if (dpRow[i] == matrix[i][j] && dpCol[j] == matrix[i][j]) { + res.add(matrix[i][j]); + } + } + } + + return res; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } From ae1bf7a430edf471fe3597d0495383f045e5ac2b Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Mon, 8 Apr 2024 11:35:17 +0800 Subject: [PATCH 77/85] MaximumDifferenceBetweenIncreasingElements --- ...umDifferenceBetweenIncreasingElements.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/MaximumDifferenceBetweenIncreasingElements.java diff --git a/src/com/leetcodeTwo/editor/cn/MaximumDifferenceBetweenIncreasingElements.java b/src/com/leetcodeTwo/editor/cn/MaximumDifferenceBetweenIncreasingElements.java new file mode 100644 index 0000000..2a131ae --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/MaximumDifferenceBetweenIncreasingElements.java @@ -0,0 +1,83 @@ +package com.leetcode.editor.cn; + +//给你一个下标从 0 开始的整数数组 nums ,该数组的大小为 n ,请你计算 nums[j] - nums[i] 能求得的 最大差值 ,其中 0 <= +//i < j < n 且 nums[i] < nums[j] 。 +// +// 返回 最大差值 。如果不存在满足要求的 i 和 j ,返回 -1 。 +// +// +// +// 示例 1: +// +// 输入:nums = [7,1,5,4] +//输出:4 +//解释: +//最大差值出现在 i = 1 且 j = 2 时,nums[j] - nums[i] = 5 - 1 = 4 。 +//注意,尽管 i = 1 且 j = 0 时 ,nums[j] - nums[i] = 7 - 1 = 6 > 4 ,但 i > j 不满足题面要求,所以 6 +// 不是有效的答案。 +// +// +// 示例 2: +// +// 输入:nums = [9,4,3,2] +//输出:-1 +//解释: +//不存在同时满足 i < j 和 nums[i] < nums[j] 这两个条件的 i, j 组合。 +// +// +// 示例 3: +// +// 输入:nums = [1,5,2,10] +//输出:9 +//解释: +//最大差值出现在 i = 0 且 j = 3 时,nums[j] - nums[i] = 10 - 1 = 9 。 +// +// +// +// +// 提示: +// +// +// n == nums.length +// 2 <= n <= 1000 +// 1 <= nums[i] <= 10⁹ +// +// Related Topics 数组 👍 63 👎 0 + + +/** + * 2016 增量元素之间的最大差值 + * + * @author shang.liang + * @date 2022-02-26 18:06:16 + */ +public class MaximumDifferenceBetweenIncreasingElements { + public static void main(String[] args) { + Solution soution = new MaximumDifferenceBetweenIncreasingElements().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int maximumDifference(int[] nums) { + + int min = nums[0]; + int ans = -1; + + for (int i = 1; i < nums.length; i++) { + + if (nums[i] > min) { + ans = Math.max(ans, nums[i] - min); + } else { + min = nums[i]; + } + + } + + return ans; + + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From 5b9daa756ffbac61bdcb735fc003e00e550ab182 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Thu, 18 Apr 2024 16:44:25 +0800 Subject: [PATCH 78/85] MaximumNumberOfEatenApples --- .../editor/cn/MaximumNumberOfEatenApples.java | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/MaximumNumberOfEatenApples.java diff --git a/src/com/leetcodeTwo/editor/cn/MaximumNumberOfEatenApples.java b/src/com/leetcodeTwo/editor/cn/MaximumNumberOfEatenApples.java new file mode 100644 index 0000000..be20308 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/MaximumNumberOfEatenApples.java @@ -0,0 +1,118 @@ +package com.leetcode.editor.cn; + +import java.util.PriorityQueue; + +; + +//有一棵特殊的苹果树,一连 n 天,每天都可以长出若干个苹果。在第 i 天,树上会长出 apples[i] 个苹果,这些苹果将会在 days[i] 天后(也就 +//是说,第 i + days[i] 天时)腐烂,变得无法食用。也可能有那么几天,树上不会长出新的苹果,此时用 apples[i] == 0 且 days[i] = +//= 0 表示。 +// +// 你打算每天 最多 吃一个苹果来保证营养均衡。注意,你可以在这 n 天之后继续吃苹果。 +// +// 给你两个长度为 n 的整数数组 days 和 apples ,返回你可以吃掉的苹果的最大数目。 +// +// +// +// 示例 1: +// +// 输入:apples = [1,2,3,5,2], days = [3,2,1,4,2] +//输出:7 +//解释:你可以吃掉 7 个苹果: +//- 第一天,你吃掉第一天长出来的苹果。 +//- 第二天,你吃掉一个第二天长出来的苹果。 +//- 第三天,你吃掉一个第二天长出来的苹果。过了这一天,第三天长出来的苹果就已经腐烂了。 +//- 第四天到第七天,你吃的都是第四天长出来的苹果。 +// +// +// 示例 2: +// +// 输入:apples = [3,0,0,0,0,2], days = [3,0,0,0,0,2] +//输出:5 +//解释:你可以吃掉 5 个苹果: +//- 第一天到第三天,你吃的都是第一天长出来的苹果。 +//- 第四天和第五天不吃苹果。 +//- 第六天和第七天,你吃的都是第六天长出来的苹果。 +// +// +// +// +// 提示: +// +// +// apples.length == n +// days.length == n +// 1 <= n <= 2 * 104 +// 0 <= apples[i], days[i] <= 2 * 104 +// 只有在 apples[i] = 0 时,days[i] = 0 才成立 +// +// Related Topics 贪心 数组 堆(优先队列) +// 👍 56 👎 0 + +/** + * 1705 吃苹果的最大数目 + * + * @author shang.liang + * @date 2021-12-24 08:33:22 + */ +public class MaximumNumberOfEatenApples { + public static void main(String[] args) { + Solution solution = new MaximumNumberOfEatenApples().new Solution(); + int[] apples = new int[]{1, 10, 17, 10, 12, 6, 20, 8, 8, 22, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 5, 2, 1, 0, 0, 0, 0, 0, 0, 23}; + int[] days = new int[]{19999, 11, 18, 22, 5, 2, 14, 5, 20, 7, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 1, 4, 2, 7, 0, 0, 0, 0, 0, 0, 1}; + System.out.println(solution.eatenApples(apples, days)); + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int eatenApples(int[] apples, int[] days) { + + int res = 0; + int day = 0; + + PriorityQueue queue = new PriorityQueue<>((o1, o2) -> (o1[0] - o2[0])); + + for (int i = 0; i < apples.length; i++) { + + if (apples[i] > 0) { + queue.add(new Integer[]{days[i] + i, apples[i]}); + } + + boolean eat = false; + while (!queue.isEmpty() && !eat) { + Integer[] poll = queue.peek(); + if (poll[1] > 0 && poll[0] > day) { + eat = true; + res++; + if(poll[1] == 1){ + queue.poll(); + }else{ + queue.peek()[1]--; + } + } else { + queue.poll(); + } + } + day++; + } + + while (!queue.isEmpty()) { + Integer[] poll = queue.peek(); + + if (poll[1] > 0 && poll[0] > day) { + day++; + res++; + if(poll[1] == 1){ + queue.poll(); + }else{ + queue.peek()[1]--; + } + } else { + queue.poll(); + } + } + return res; + } + } +//leetcode submit region end(Prohibit modification and deletion) +} \ No newline at end of file From 958785959e7250f621013fcbd5df11e17281f468 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Thu, 18 Apr 2024 16:44:32 +0800 Subject: [PATCH 79/85] MergeStringsAlternately --- .../editor/cn/MergeStringsAlternately.java | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/MergeStringsAlternately.java diff --git a/src/com/leetcodeTwo/editor/cn/MergeStringsAlternately.java b/src/com/leetcodeTwo/editor/cn/MergeStringsAlternately.java new file mode 100644 index 0000000..224a44a --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/MergeStringsAlternately.java @@ -0,0 +1,91 @@ +package com.leetcode.editor.cn; + +//给你两个字符串 word1 和 word2 。请你从 word1 开始,通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长,就将多出来的字母追加到 +//合并后字符串的末尾。 +// +// 返回 合并后的字符串 。 +// +// +// +// 示例 1: +// +// +//输入:word1 = "abc", word2 = "pqr" +//输出:"apbqcr" +//解释:字符串合并情况如下所示: +//word1: a b c +//word2: p q r +//合并后: a p b q c r +// +// +// 示例 2: +// +// +//输入:word1 = "ab", word2 = "pqrs" +//输出:"apbqrs" +//解释:注意,word2 比 word1 长,"rs" 需要追加到合并后字符串的末尾。 +//word1: a b +//word2: p q r s +//合并后: a p b q r s +// +// +// 示例 3: +// +// +//输入:word1 = "abcd", word2 = "pq" +//输出:"apbqcd" +//解释:注意,word1 比 word2 长,"cd" 需要追加到合并后字符串的末尾。 +//word1: a b c d +//word2: p q +//合并后: a p b q c d +// +// +// +// +// 提示: +// +// +// 1 <= word1.length, word2.length <= 100 +// word1 和 word2 由小写英文字母组成 +// +// +// Related Topics 双指针 字符串 👍 54 👎 0 + + +/** + * 1768 交替合并字符串 + * @date 2022-10-23 15:00:28 + * @author shang.liang + */ + public class MergeStringsAlternately{ + public static void main(String[] args){ + Solution soution = new MergeStringsAlternately().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public String mergeAlternately(String word1, String word2) { + + int l1 = word1.length(); + int l2 = word2.length(); + + int i = 0, j = 0; + StringBuilder result = new StringBuilder(); + while (i < l1 || j < l2) { + if (i < l1) { + result.append(word1.charAt(i)); + i++; + } + if (j < l2) { + result.append(word2.charAt(j)); + j++; + } + } + + return result.toString(); + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } From dbb2af82acecc2182c09ebdc2730b6e590fa39b7 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Sun, 21 Apr 2024 20:05:42 +0800 Subject: [PATCH 80/85] MinimumDifferenceBetweenHighestAndLowestOfKScores --- ...renceBetweenHighestAndLowestOfKScores.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/MinimumDifferenceBetweenHighestAndLowestOfKScores.java diff --git a/src/com/leetcodeTwo/editor/cn/MinimumDifferenceBetweenHighestAndLowestOfKScores.java b/src/com/leetcodeTwo/editor/cn/MinimumDifferenceBetweenHighestAndLowestOfKScores.java new file mode 100644 index 0000000..2be9e3d --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/MinimumDifferenceBetweenHighestAndLowestOfKScores.java @@ -0,0 +1,80 @@ +package com.leetcode.editor.cn; + +//给你一个 下标从 0 开始 的整数数组 nums ,其中 nums[i] 表示第 i 名学生的分数。另给你一个整数 k 。 +// +// 从数组中选出任意 k 名学生的分数,使这 k 个分数间 最高分 和 最低分 的 差值 达到 最小化 。 +// +// 返回可能的 最小差值 。 +// +// +// +// 示例 1: +// +// 输入:nums = [90], k = 1 +//输出:0 +//解释:选出 1 名学生的分数,仅有 1 种方法: +//- [90] 最高分和最低分之间的差值是 90 - 90 = 0 +//可能的最小差值是 0 +// +// +// 示例 2: +// +// 输入:nums = [9,4,1,7], k = 2 +//输出:2 +//解释:选出 2 名学生的分数,有 6 种方法: +//- [9,4,1,7] 最高分和最低分之间的差值是 9 - 4 = 5 +//- [9,4,1,7] 最高分和最低分之间的差值是 9 - 1 = 8 +//- [9,4,1,7] 最高分和最低分之间的差值是 9 - 7 = 2 +//- [9,4,1,7] 最高分和最低分之间的差值是 4 - 1 = 3 +//- [9,4,1,7] 最高分和最低分之间的差值是 7 - 4 = 3 +//- [9,4,1,7] 最高分和最低分之间的差值是 7 - 1 = 6 +//可能的最小差值是 2 +// +// +// +// 提示: +// +// +// 1 <= k <= nums.length <= 1000 +// 0 <= nums[i] <= 10⁵ +// +// Related Topics 数组 排序 滑动窗口 👍 15 👎 0 + + +import java.util.Arrays; + +/** + * 1984 学生分数的最小差值 + * @date 2022-02-11 08:46:52 + * @author shang.liang + */ + public class MinimumDifferenceBetweenHighestAndLowestOfKScores{ + public static void main(String[] args){ + Solution soution = new MinimumDifferenceBetweenHighestAndLowestOfKScores().new Solution(); + int[] nums = new int[]{9, 4, 1, 7}; + int k = 2; + System.out.println("soution.minimumDifference(nums,k) = " + soution.minimumDifference(nums, k)); + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int minimumDifference(int[] nums, int k) { + + int res = Integer.MAX_VALUE; + int index = 0; + + Arrays.sort(nums); + + for (int i = k-1; i < nums.length; i++) { + + int tmp = nums[i] - nums[index++]; + res = Math.min(res, tmp); + + } + + return res; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } From 13857a279ed7b0398228347d8e81aed1139dd050 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Sun, 21 Apr 2024 20:05:49 +0800 Subject: [PATCH 81/85] NAryTreePostorderTraversal --- .../editor/cn/NAryTreePostorderTraversal.java | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/NAryTreePostorderTraversal.java diff --git a/src/com/leetcodeTwo/editor/cn/NAryTreePostorderTraversal.java b/src/com/leetcodeTwo/editor/cn/NAryTreePostorderTraversal.java new file mode 100644 index 0000000..21f3460 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/NAryTreePostorderTraversal.java @@ -0,0 +1,108 @@ +package com.leetcode.editor.cn; + +//给定一个 n 叉树的根节点 root ,返回 其节点值的 后序遍历 。 +// +// n 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 null 分隔(请参见示例)。 +// +// +// +// 示例 1: +// +// +// +// +//输入:root = [1,null,3,2,4,null,5,6] +//输出:[5,6,3,2,4,1] +// +// +// 示例 2: +// +// +// +// +//输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12, +//null,13,null,null,14] +//输出:[2,6,14,11,7,3,12,8,4,13,9,10,5,1] +// +// +// +// +// 提示: +// +// +// 节点总数在范围 [0, 10⁴] 内 +// 0 <= Node.val <= 10⁴ +// n 叉树的高度小于或等于 1000 +// +// +// +// +// 进阶:递归法很简单,你可以使用迭代法完成此题吗? +// Related Topics 栈 树 深度优先搜索 👍 194 👎 0 + + +import java.util.ArrayList; +import java.util.List; + +/** + * 590 N 叉树的后序遍历 + * + * @author shang.liang + * @date 2022-03-12 09:35:34 + */ +public class NAryTreePostorderTraversal { + public static void main(String[] args) { + Solution soution = new NAryTreePostorderTraversal().new Solution(); + + } + + class Node { + public int val; + public List children; + + public Node() { + } + + public Node(int _val) { + val = _val; + } + + public Node(int _val, List _children) { + val = _val; + children = _children; + } + } + //leetcode submit region begin(Prohibit modification and deletion) + + +// Definition for a Node. + + + class Solution { + public List postorder(Node root) { + + List res = new ArrayList<>(); + + + dfs(root, res); + + + return res; + } + + public void dfs(Node root, List res) { + + if (root == null) { + return; + } + + for (Node child : root.children) { + dfs(child, res); + } + + res.add(root.val); + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From 905914a1407237a41da36e5064f75b3314421cb6 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Tue, 30 Apr 2024 01:05:59 +0800 Subject: [PATCH 82/85] NumberOfRectanglesThatCanFormTheLargestSquare --- ...RectanglesThatCanFormTheLargestSquare.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/NumberOfRectanglesThatCanFormTheLargestSquare.java diff --git a/src/com/leetcodeTwo/editor/cn/NumberOfRectanglesThatCanFormTheLargestSquare.java b/src/com/leetcodeTwo/editor/cn/NumberOfRectanglesThatCanFormTheLargestSquare.java new file mode 100644 index 0000000..9f2993c --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/NumberOfRectanglesThatCanFormTheLargestSquare.java @@ -0,0 +1,77 @@ +package com.leetcode.editor.cn; + +//给你一个数组 rectangles ,其中 rectangles[i] = [li, wi] 表示第 i 个矩形的长度为 li 、宽度为 wi 。 +// +// 如果存在 k 同时满足 k <= li 和 k <= wi ,就可以将第 i 个矩形切成边长为 k 的正方形。例如,矩形 [4,6] 可以切成边长最大为 +//4 的正方形。 +// +// 设 maxLen 为可以从矩形数组 rectangles 切分得到的 最大正方形 的边长。 +// +// 请你统计有多少个矩形能够切出边长为 maxLen 的正方形,并返回矩形 数目 。 +// +// +// +// 示例 1: +// +// +//输入:rectangles = [[5,8],[3,9],[5,12],[16,5]] +//输出:3 +//解释:能从每个矩形中切出的最大正方形边长分别是 [5,3,5,5] 。 +//最大正方形的边长为 5 ,可以由 3 个矩形切分得到。 +// +// +// 示例 2: +// +// +//输入:rectangles = [[2,3],[3,7],[4,3],[3,7]] +//输出:3 +// +// +// +// +// 提示: +// +// +// 1 <= rectangles.length <= 1000 +// rectangles[i].length == 2 +// 1 <= li, wi <= 10⁹ +// li != wi +// +// Related Topics 数组 👍 47 👎 0 + + +/** + * 1725 可以形成最大正方形的矩形数目 + * @date 2022-02-04 22:08:26 + * @author shang.liang + */ + public class NumberOfRectanglesThatCanFormTheLargestSquare{ + public static void main(String[] args){ + Solution soution = new NumberOfRectanglesThatCanFormTheLargestSquare().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int countGoodRectangles(int[][] rectangles) { + + int res = 0; + int maxLen = 0; + + for (int[] rectangle : rectangles) { + int currLen = Math.min(rectangle[0], rectangle[1]); + + if (currLen == maxLen) { + res++; + } else if (currLen > maxLen) { + res = 1; + maxLen = currLen; + } + } + + return res; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } From 32e9c760efcaa434bd7dbb5c8d427aede383126e Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Tue, 30 Apr 2024 01:06:05 +0800 Subject: [PATCH 83/85] NumberOfStudentsUnableToEatLunch --- .../cn/NumberOfStudentsUnableToEatLunch.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/NumberOfStudentsUnableToEatLunch.java diff --git a/src/com/leetcodeTwo/editor/cn/NumberOfStudentsUnableToEatLunch.java b/src/com/leetcodeTwo/editor/cn/NumberOfStudentsUnableToEatLunch.java new file mode 100644 index 0000000..737394f --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/NumberOfStudentsUnableToEatLunch.java @@ -0,0 +1,90 @@ +package com.leetcode.editor.cn; + +//学校的自助午餐提供圆形和方形的三明治,分别用数字 0 和 1 表示。所有学生站在一个队列里,每个学生要么喜欢圆形的要么喜欢方形的。 餐厅里三明治的数量与学生 +//的数量相同。所有三明治都放在一个 栈 里,每一轮: +// +// +// 如果队列最前面的学生 喜欢 栈顶的三明治,那么会 拿走它 并离开队列。 +// 否则,这名学生会 放弃这个三明治 并回到队列的尾部。 +// +// +// 这个过程会一直持续到队列里所有学生都不喜欢栈顶的三明治为止。 +// +// 给你两个整数数组 students 和 sandwiches ,其中 sandwiches[i] 是栈里面第 i 个三明治的类型(i = 0 是栈的顶部) +//, students[j] 是初始队列里第 j 名学生对三明治的喜好(j = 0 是队列的最开始位置)。请你返回无法吃午餐的学生数量。 +// +// +// +// 示例 1: +// +// 输入:students = [1,1,0,0], sandwiches = [0,1,0,1] +//输出:0 +//解释: +//- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [1,0,0,1]。 +//- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [0,0,1,1]。 +//- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [0,1,1],三明治栈为 sandwiches = [1,0,1]。 +//- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [1,1,0]。 +//- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [1,0],三明治栈为 sandwiches = [0,1]。 +//- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [0,1]。 +//- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [1],三明治栈为 sandwiches = [1]。 +//- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [],三明治栈为 sandwiches = []。 +//所以所有学生都有三明治吃。 +// +// +// 示例 2: +// +// 输入:students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1] +//输出:3 +// +// +// +// +// 提示: +// +// +// 1 <= students.length, sandwiches.length <= 100 +// students.length == sandwiches.length +// sandwiches[i] 要么是 0 ,要么是 1 。 +// students[i] 要么是 0 ,要么是 1 。 +// +// +// Related Topics 栈 队列 数组 模拟 👍 111 👎 0 + + +import java.util.Arrays; + +/** + * 1700 无法吃午餐的学生数量 + * + * @author shang.liang + * @date 2022-10-19 18:58:24 + */ +public class NumberOfStudentsUnableToEatLunch { + public static void main(String[] args) { + Solution soution = new NumberOfStudentsUnableToEatLunch().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int countStudents(int[] students, int[] sandwiches) { + + int s1 = Arrays.stream(students).sum(); + int s0 = students.length - s1; + + for (int i = 0; i < sandwiches.length; i++) { + if (sandwiches[i] == 0 && s0 > 0) { + s0--; + } else if (sandwiches[i] == 1 && s1 > 0) { + s1--; + } else { + break; + } + } + return s0 + s1; + + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} From 55275cb12146183e0a507cad1bafebc0aa5ef515 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Mon, 6 May 2024 11:46:14 +0800 Subject: [PATCH 84/85] NumberOfSubarraysWithBoundedMaximum --- .../NumberOfSubarraysWithBoundedMaximum.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/com/leetcodeTwo/editor/cn/NumberOfSubarraysWithBoundedMaximum.java diff --git a/src/com/leetcodeTwo/editor/cn/NumberOfSubarraysWithBoundedMaximum.java b/src/com/leetcodeTwo/editor/cn/NumberOfSubarraysWithBoundedMaximum.java new file mode 100644 index 0000000..b47c770 --- /dev/null +++ b/src/com/leetcodeTwo/editor/cn/NumberOfSubarraysWithBoundedMaximum.java @@ -0,0 +1,69 @@ +package com.leetcode.editor.cn; + +//给你一个整数数组 nums 和两个整数:left 及 right 。找出 nums 中连续、非空且其中最大元素在范围 [left, right] 内的子数组 +//,并返回满足条件的子数组的个数。 +// +// 生成的测试用例保证结果符合 32-bit 整数范围。 +// +// +// +// 示例 1: +// +// +//输入:nums = [2,1,4,3], left = 2, right = 3 +//输出:3 +//解释:满足条件的三个子数组:[2], [2, 1], [3] +// +// +// 示例 2: +// +// +//输入:nums = [2,9,2,5,6], left = 2, right = 8 +//输出:7 +// +// +// +// +// 提示: +// +// +// 1 <= nums.length <= 10⁵ +// 0 <= nums[i] <= 10⁹ +// 0 <= left <= right <= 10⁹ +// +// +// Related Topics 数组 双指针 👍 265 👎 0 + + +/** + * 795 区间子数组个数 + * @date 2022-11-24 13:25:36 + * @author shang.liang + */ + public class NumberOfSubarraysWithBoundedMaximum{ + public static void main(String[] args){ + Solution soution = new NumberOfSubarraysWithBoundedMaximum().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int numSubarrayBoundedMax(int[] nums, int left, int right) { + int res = 0, last2 = -1, last1 = -1; + for (int i = 0; i < nums.length; i++) { + if (nums[i] >= left && nums[i] <= right) { + last1 = i; + } else if (nums[i] > right) { + last2 = i; + last1 = -1; + } + if (last1 != -1) { + res += last1 - last2; + } + } + return res; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } From 1d933fb951b352fc4ae05bf653f16b4b6a484760 Mon Sep 17 00:00:00 2001 From: lnrCoder Date: Tue, 14 May 2024 18:41:08 +0800 Subject: [PATCH 85/85] =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/leetcodeTwo/editor/cn/AddDigits.java | 69 -------- .../leetcodeTwo/editor/cn/AddTwoNumbers.java | 110 ------------ .../editor/cn/ArrayOfDoubledPairs.java | 90 ---------- .../leetcodeTwo/editor/cn/BaseballGame.java | 118 ------------- .../editor/cn/BattleshipsInABoard.java | 82 --------- .../cn/BinaryNumberWithAlternatingBits.java | 61 ------- .../cn/ComplexNumberMultiplication.java | 74 -------- .../editor/cn/ContainsDuplicateIi.java | 72 -------- .../cn/CountNumberOfHomogenousSubstrings.java | 85 --------- ...tNumberOfPairsWithAbsoluteDifferenceK.java | 89 ---------- .../editor/cn/CountVowelsPermutation.java | 104 ----------- .../cn/CountingWordsWithAGivenPrefix.java | 64 ------- .../editor/cn/CustomSortString.java | 77 --------- .../leetcodeTwo/editor/cn/EvenOddTree.java | 161 ------------------ .../editor/cn/FactorialTrailingZeroes.java | 74 -------- .../editor/cn/FindCenterOfStarGraph.java | 73 -------- .../editor/cn/FindKPairsWithSmallestSums.java | 98 ----------- .../editor/cn/FindMissingObservations.java | 102 ----------- .../editor/cn/FindTheClosestPalindrome.java | 58 ------- .../editor/cn/FindTheHighestAltitude.java | 67 -------- .../editor/cn/FriendsOfAppropriateAges.java | 120 ------------- .../cn/IncreasingTripletSubsequence.java | 75 -------- .../cn/KnightProbabilityInChessboard.java | 95 ----------- .../cn/LargestNumberAtLeastTwiceOfOthers.java | 86 ---------- .../editor/cn/LetterCasePermutation.java | 76 --------- .../editor/cn/LinkedListRandomNode.java | 115 ------------- .../editor/cn/LongestHappyString.java | 89 ---------- .../editor/cn/LuckyNumbersInAMatrix.java | 94 ---------- ...umDifferenceBetweenIncreasingElements.java | 83 --------- .../editor/cn/MaximumNumberOfEatenApples.java | 118 ------------- .../editor/cn/MergeStringsAlternately.java | 91 ---------- ...renceBetweenHighestAndLowestOfKScores.java | 80 --------- .../editor/cn/NAryTreePostorderTraversal.java | 108 ------------ ...RectanglesThatCanFormTheLargestSquare.java | 77 --------- .../cn/NumberOfStudentsUnableToEatLunch.java | 90 ---------- .../NumberOfSubarraysWithBoundedMaximum.java | 69 -------- 36 files changed, 3194 deletions(-) delete mode 100644 src/com/leetcodeTwo/editor/cn/AddDigits.java delete mode 100644 src/com/leetcodeTwo/editor/cn/AddTwoNumbers.java delete mode 100644 src/com/leetcodeTwo/editor/cn/ArrayOfDoubledPairs.java delete mode 100644 src/com/leetcodeTwo/editor/cn/BaseballGame.java delete mode 100644 src/com/leetcodeTwo/editor/cn/BattleshipsInABoard.java delete mode 100644 src/com/leetcodeTwo/editor/cn/BinaryNumberWithAlternatingBits.java delete mode 100644 src/com/leetcodeTwo/editor/cn/ComplexNumberMultiplication.java delete mode 100644 src/com/leetcodeTwo/editor/cn/ContainsDuplicateIi.java delete mode 100644 src/com/leetcodeTwo/editor/cn/CountNumberOfHomogenousSubstrings.java delete mode 100644 src/com/leetcodeTwo/editor/cn/CountNumberOfPairsWithAbsoluteDifferenceK.java delete mode 100644 src/com/leetcodeTwo/editor/cn/CountVowelsPermutation.java delete mode 100644 src/com/leetcodeTwo/editor/cn/CountingWordsWithAGivenPrefix.java delete mode 100644 src/com/leetcodeTwo/editor/cn/CustomSortString.java delete mode 100644 src/com/leetcodeTwo/editor/cn/EvenOddTree.java delete mode 100644 src/com/leetcodeTwo/editor/cn/FactorialTrailingZeroes.java delete mode 100644 src/com/leetcodeTwo/editor/cn/FindCenterOfStarGraph.java delete mode 100644 src/com/leetcodeTwo/editor/cn/FindKPairsWithSmallestSums.java delete mode 100644 src/com/leetcodeTwo/editor/cn/FindMissingObservations.java delete mode 100644 src/com/leetcodeTwo/editor/cn/FindTheClosestPalindrome.java delete mode 100644 src/com/leetcodeTwo/editor/cn/FindTheHighestAltitude.java delete mode 100644 src/com/leetcodeTwo/editor/cn/FriendsOfAppropriateAges.java delete mode 100644 src/com/leetcodeTwo/editor/cn/IncreasingTripletSubsequence.java delete mode 100644 src/com/leetcodeTwo/editor/cn/KnightProbabilityInChessboard.java delete mode 100644 src/com/leetcodeTwo/editor/cn/LargestNumberAtLeastTwiceOfOthers.java delete mode 100644 src/com/leetcodeTwo/editor/cn/LetterCasePermutation.java delete mode 100644 src/com/leetcodeTwo/editor/cn/LinkedListRandomNode.java delete mode 100644 src/com/leetcodeTwo/editor/cn/LongestHappyString.java delete mode 100644 src/com/leetcodeTwo/editor/cn/LuckyNumbersInAMatrix.java delete mode 100644 src/com/leetcodeTwo/editor/cn/MaximumDifferenceBetweenIncreasingElements.java delete mode 100644 src/com/leetcodeTwo/editor/cn/MaximumNumberOfEatenApples.java delete mode 100644 src/com/leetcodeTwo/editor/cn/MergeStringsAlternately.java delete mode 100644 src/com/leetcodeTwo/editor/cn/MinimumDifferenceBetweenHighestAndLowestOfKScores.java delete mode 100644 src/com/leetcodeTwo/editor/cn/NAryTreePostorderTraversal.java delete mode 100644 src/com/leetcodeTwo/editor/cn/NumberOfRectanglesThatCanFormTheLargestSquare.java delete mode 100644 src/com/leetcodeTwo/editor/cn/NumberOfStudentsUnableToEatLunch.java delete mode 100644 src/com/leetcodeTwo/editor/cn/NumberOfSubarraysWithBoundedMaximum.java diff --git a/src/com/leetcodeTwo/editor/cn/AddDigits.java b/src/com/leetcodeTwo/editor/cn/AddDigits.java deleted file mode 100644 index 915fa37..0000000 --- a/src/com/leetcodeTwo/editor/cn/AddDigits.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.leetcodeTwo.editor.cn; - -//给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。返回这个结果。 -// -// -// -// 示例 1: -// -// -//输入: num = 38 -//输出: 2 -//解释: 各位相加的过程为: -//38 --> 3 + 8 --> 11 -//11 --> 1 + 1 --> 2 -//由于 2 是一位数,所以返回 2。 -// -// -// 示例 1: -// -// -//输入: num = 0 -//输出: 0 -// -// -// -// 提示: -// -// -// 0 <= num <= 2³¹ - 1 -// -// -// -// -// 进阶:你可以不使用循环或者递归,在 O(1) 时间复杂度内解决这个问题吗? -// Related Topics 数学 数论 模拟 👍 427 👎 0 - - -/** - * 258 各位相加 - * @date 2022-03-03 09:46:57 - * @author shang.liang - */ - public class AddDigits{ - public static void main(String[] args){ - Solution soution = new AddDigits().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public int addDigits(int num) { - - if (num < 10) { - return num; - } - - int x = 0; - - while (num != 0) { - x += num % 10; - num = num / 10; - } - - return addDigits(x); - } -} -//leetcode submit region end(Prohibit modification and deletion) - - } diff --git a/src/com/leetcodeTwo/editor/cn/AddTwoNumbers.java b/src/com/leetcodeTwo/editor/cn/AddTwoNumbers.java deleted file mode 100644 index ce3eb82..0000000 --- a/src/com/leetcodeTwo/editor/cn/AddTwoNumbers.java +++ /dev/null @@ -1,110 +0,0 @@ -package com.leetcode.editor.cn; - -//给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。 -// -// 请你将两个数相加,并以相同形式返回一个表示和的链表。 -// -// 你可以假设除了数字 0 之外,这两个数都不会以 0 开头。 -// -// -// -// 示例 1: -// -// -//输入:l1 = [2,4,3], l2 = [5,6,4] -//输出:[7,0,8] -//解释:342 + 465 = 807. -// -// -// 示例 2: -// -// -//输入:l1 = [0], l2 = [0] -//输出:[0] -// -// -// 示例 3: -// -// -//输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] -//输出:[8,9,9,9,0,0,0,1] -// -// -// -// -// 提示: -// -// -// 每个链表中的节点数在范围 [1, 100] 内 -// 0 <= Node.val <= 9 -// 题目数据保证列表表示的数字不含前导零 -// -// Related Topics 递归 链表 数学 👍 7310 👎 0 - - -import com.liang.leetcode.bean.ListNode; - -/** - * 2 两数相加 - * - * @author shang.liang - * @date 2022-01-05 22:06:10 - */ -public class AddTwoNumbers { - public static void main(String[] args) { - Solution soution = new AddTwoNumbers().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) - - /** - * Definition for singly-linked list. - * public class ListNode { - * int val; - * ListNode next; - * ListNode() {} - * ListNode(int val) { this.val = val; } - * ListNode(int val, ListNode next) { this.val = val; this.next = next; } - * } - */ - class Solution { - - private int flag = 0; - - public ListNode addTwoNumbers(ListNode l1, ListNode l2) { - - if (l1 == null && l2 == null) { - - if (flag == 1) { - return new ListNode(flag, null); - } - - return null; - } - - int value = flag; - flag = 0; - - if (l1 != null) { - value += l1.val; - } - if (l2 != null) { - value += l2.val; - } - - if (value >= 10) { - value = value % 10; - flag = 1; - } - - ListNode listNode = new ListNode(value, addTwoNumbers(l1 == null ? null : l1.next - , l2 == null ? null : l2.next)); - - - return listNode; - } - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/ArrayOfDoubledPairs.java b/src/com/leetcodeTwo/editor/cn/ArrayOfDoubledPairs.java deleted file mode 100644 index 75f9beb..0000000 --- a/src/com/leetcodeTwo/editor/cn/ArrayOfDoubledPairs.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.leetcode.editor.cn; - -//给定一个长度为偶数的整数数组 arr,只有对 arr 进行重组后可以满足 “对于每个 0 <= i < len(arr) / 2,都有 arr[2 * i -//+ 1] = 2 * arr[2 * i]” 时,返回 true;否则,返回 false。 -// -// -// -// 示例 1: -// -// -//输入:arr = [3,1,3,6] -//输出:false -// -// -// 示例 2: -// -// -//输入:arr = [2,1,2,6] -//输出:false -// -// -// 示例 3: -// -// -//输入:arr = [4,-2,2,-4] -//输出:true -//解释:可以用 [-2,-4] 和 [2,4] 这两组组成 [-2,-4,2,4] 或是 [2,4,-2,-4] -// -// -// -// -// 提示: -// -// -// 0 <= arr.length <= 3 * 10⁴ -// arr.length 是偶数 -// -10⁵ <= arr[i] <= 10⁵ -// -// Related Topics 贪心 数组 哈希表 排序 👍 158 👎 0 - - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; - -/** - * 954 二倍数对数组 - * @date 2022-04-01 23:16:30 - * @author shang.liang - */ - public class ArrayOfDoubledPairs{ - public static void main(String[] args){ - Solution soution = new ArrayOfDoubledPairs().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public boolean canReorderDoubled(int[] arr) { - - HashMap map = new HashMap<>(); - - for (int i = 0; i < arr.length; i++) { - map.put(arr[i], map.getOrDefault(arr[i], 0) + 1); - } - - List list = new ArrayList<>(); - for (Integer num :map.keySet()){ - list.add(num); - } - - Collections.sort(list, (a, b) -> Math.abs(a) - Math.abs(b)); - - for (Integer integer : list) { - - if (map.getOrDefault(integer * 2, 0) < map.get(integer)) { - return false; - } - - map.put(integer * 2, map.getOrDefault(integer * 2, 0) - map.get(integer)); - - } - - return true; - } -} -//leetcode submit region end(Prohibit modification and deletion) - - } diff --git a/src/com/leetcodeTwo/editor/cn/BaseballGame.java b/src/com/leetcodeTwo/editor/cn/BaseballGame.java deleted file mode 100644 index 529dda3..0000000 --- a/src/com/leetcodeTwo/editor/cn/BaseballGame.java +++ /dev/null @@ -1,118 +0,0 @@ -package com.leetcode.editor.cn; - -//你现在是一场采用特殊赛制棒球比赛的记录员。这场比赛由若干回合组成,过去几回合的得分可能会影响以后几回合的得分。 -// -// 比赛开始时,记录是空白的。你会得到一个记录操作的字符串列表 ops,其中 ops[i] 是你需要记录的第 i 项操作,ops 遵循下述规则: -// -// -// 整数 x - 表示本回合新获得分数 x -// "+" - 表示本回合新获得的得分是前两次得分的总和。题目数据保证记录此操作时前面总是存在两个有效的分数。 -// "D" - 表示本回合新获得的得分是前一次得分的两倍。题目数据保证记录此操作时前面总是存在一个有效的分数。 -// "C" - 表示前一次得分无效,将其从记录中移除。题目数据保证记录此操作时前面总是存在一个有效的分数。 -// -// -// 请你返回记录中所有得分的总和。 -// -// -// -// 示例 1: -// -// -//输入:ops = ["5","2","C","D","+"] -//输出:30 -//解释: -//"5" - 记录加 5 ,记录现在是 [5] -//"2" - 记录加 2 ,记录现在是 [5, 2] -//"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5]. -//"D" - 记录加 2 * 5 = 10 ,记录现在是 [5, 10]. -//"+" - 记录加 5 + 10 = 15 ,记录现在是 [5, 10, 15]. -//所有得分的总和 5 + 10 + 15 = 30 -// -// -// 示例 2: -// -// -//输入:ops = ["5","-2","4","C","D","9","+","+"] -//输出:27 -//解释: -//"5" - 记录加 5 ,记录现在是 [5] -//"-2" - 记录加 -2 ,记录现在是 [5, -2] -//"4" - 记录加 4 ,记录现在是 [5, -2, 4] -//"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5, -2] -//"D" - 记录加 2 * -2 = -4 ,记录现在是 [5, -2, -4] -//"9" - 记录加 9 ,记录现在是 [5, -2, -4, 9] -//"+" - 记录加 -4 + 9 = 5 ,记录现在是 [5, -2, -4, 9, 5] -//"+" - 记录加 9 + 5 = 14 ,记录现在是 [5, -2, -4, 9, 5, 14] -//所有得分的总和 5 + -2 + -4 + 9 + 5 + 14 = 27 -// -// -// 示例 3: -// -// -//输入:ops = ["1"] -//输出:1 -// -// -// -// -// 提示: -// -// -// 1 <= ops.length <= 1000 -// ops[i] 为 "C"、"D"、"+",或者一个表示整数的字符串。整数范围是 [-3 * 10⁴, 3 * 10⁴] -// 对于 "+" 操作,题目数据保证记录此操作时前面总是存在两个有效的分数 -// 对于 "C" 和 "D" 操作,题目数据保证记录此操作时前面总是存在一个有效的分数 -// -// Related Topics 栈 数组 模拟 👍 222 👎 0 - - -/** - * 682 棒球比赛 - * @date 2022-03-26 12:07:50 - * @author shang.liang - */ - public class BaseballGame{ - public static void main(String[] args){ - Solution soution = new BaseballGame().new Solution(); - String[] ops = new String[]{"5","-2","4","C","D","9","+","+"}; - System.out.println("soution.calPoints(ops) = " + soution.calPoints(ops)); - - } - - //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public int calPoints(String[] ops) { - - int res = 0; - int[] score = new int[ops.length]; - - for (int i = 0,index=0; i < ops.length; i++, index++) { - switch (ops[i]) { - case "+": { - score[index] = score[index - 1] + score[index - 2]; - res += score[index]; - break; - } - case "D":{ - score[index] = score[index - 1] * 2; - res += score[index]; - break; - } - case "C": { - res -= score[index - 1]; - index -= 2; - break; - } - default: { - score[index] = Integer.parseInt(ops[i]); - res += score[index]; - } - } - } - - return res; - } -} -//leetcode submit region end(Prohibit modification and deletion) - - } diff --git a/src/com/leetcodeTwo/editor/cn/BattleshipsInABoard.java b/src/com/leetcodeTwo/editor/cn/BattleshipsInABoard.java deleted file mode 100644 index 277eea2..0000000 --- a/src/com/leetcodeTwo/editor/cn/BattleshipsInABoard.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.leetcode.editor.cn;; - -//给你一个大小为 m x n 的矩阵 board 表示甲板,其中,每个单元格可以是一艘战舰 'X' 或者是一个空位 '.' ,返回在甲板 board 上放置的 -// 战舰 的数量。 -// -// 战舰 只能水平或者垂直放置在 board 上。换句话说,战舰只能按 1 x k(1 行,k 列)或 k x 1(k 行,1 列)的形状建造,其中 k 可以 -//是任意大小。两艘战舰之间至少有一个水平或垂直的空位分隔 (即没有相邻的战舰)。 -// -// -// -// 示例 1: -// -// -//输入:board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]] -//输出:2 -// -// -// 示例 2: -// -// -//输入:board = [["."]] -//输出:0 -// -// -// -// -// 提示: -// -// -// m == board.length -// n == board[i].length -// 1 <= m, n <= 200 -// board[i][j] 是 '.' 或 'X' -// -// -// -// -// 进阶:你可以实现一次扫描算法,并只使用 O(1) 额外空间,并且不修改 board 的值来解决这个问题吗? -// Related Topics 深度优先搜索 数组 矩阵 -// 👍 134 👎 0 - - -/** - * 419 甲板上的战舰 - * @date 2021-12-18 09:50:35 - * @author shang.liang - */ -public class BattleshipsInABoard{ - public static void main(String[] args) { - Solution solution = new BattleshipsInABoard().new Solution(); - - } - -//leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public int countBattleships(char[][] board) { - - int res = 0; - int row = board.length; - int col = board[0].length; - - for (int i = 0; i < row; i++) { - for (int j = 0; j < col; j++) { - if (board[i][j] != 'X') { - continue; - } - if (i > 0 && board[i - 1][j] == 'X') { - continue; - } - if (j > 0 && board[i][j - 1] == 'X') { - continue; - } - res++; - } - } - - return res; - } -} -//leetcode submit region end(Prohibit modification and deletion) - -} \ No newline at end of file diff --git a/src/com/leetcodeTwo/editor/cn/BinaryNumberWithAlternatingBits.java b/src/com/leetcodeTwo/editor/cn/BinaryNumberWithAlternatingBits.java deleted file mode 100644 index 073157c..0000000 --- a/src/com/leetcodeTwo/editor/cn/BinaryNumberWithAlternatingBits.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.leetcode.editor.cn; - -//给定一个正整数,检查它的二进制表示是否总是 0、1 交替出现:换句话说,就是二进制表示中相邻两位的数字永不相同。 -// -// -// -// 示例 1: -// -// -//输入:n = 5 -//输出:true -//解释:5 的二进制表示是:101 -// -// -// 示例 2: -// -// -//输入:n = 7 -//输出:false -//解释:7 的二进制表示是:111. -// -// 示例 3: -// -// -//输入:n = 11 -//输出:false -//解释:11 的二进制表示是:1011. -// -// -// -// 提示: -// -// -// 1 <= n <= 2³¹ - 1 -// -// Related Topics 位运算 👍 186 👎 0 - - -/** - * 693 交替位二进制数 - * @date 2022-03-28 21:54:45 - * @author shang.liang - */ - public class BinaryNumberWithAlternatingBits{ - public static void main(String[] args){ - Solution soution = new BinaryNumberWithAlternatingBits().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public boolean hasAlternatingBits(int n) { - - int a = n ^ n >> 1; - - return (a & (a + 1)) == 0; - } -} -//leetcode submit region end(Prohibit modification and deletion) - - } diff --git a/src/com/leetcodeTwo/editor/cn/ComplexNumberMultiplication.java b/src/com/leetcodeTwo/editor/cn/ComplexNumberMultiplication.java deleted file mode 100644 index e6c905c..0000000 --- a/src/com/leetcodeTwo/editor/cn/ComplexNumberMultiplication.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.leetcode.editor.cn; - -//复数 可以用字符串表示,遵循 "实部+虚部i" 的形式,并满足下述条件: -// -// -// 实部 是一个整数,取值范围是 [-100, 100] -// 虚部 也是一个整数,取值范围是 [-100, 100] -// i² == -1 -// -// -// 给你两个字符串表示的复数 num1 和 num2 ,请你遵循复数表示形式,返回表示它们乘积的字符串。 -// -// -// -// 示例 1: -// -// -//输入:num1 = "1+1i", num2 = "1+1i" -//输出:"0+2i" -//解释:(1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i ,你需要将它转换为 0+2i 的形式。 -// -// -// 示例 2: -// -// -//输入:num1 = "1+-1i", num2 = "1+-1i" -//输出:"0+-2i" -//解释:(1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i ,你需要将它转换为 0+-2i 的形式。 -// -// -// -// -// 提示: -// -// -// num1 和 num2 都是有效的复数表示。 -// -// Related Topics 数学 字符串 模拟 👍 123 👎 0 - - -/** - * 537 复数乘法 - * - * @author shang.liang - * @date 2022-02-25 22:53:28 - */ -public class ComplexNumberMultiplication { - public static void main(String[] args) { - Solution soution = new ComplexNumberMultiplication().new Solution(); - String nums1 = "1+1i"; - String nums2 = "1+1i"; - System.out.println("soution.complexNumberMultiply(nums1,nums2) = " + soution.complexNumberMultiply(nums1, nums2)); - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public String complexNumberMultiply(String num1, String num2) { - - String[] str1 = num1.split("\\+|i"); - String[] str2 = num2.split("[+i]"); - - // a + bi , c + di - int a = Integer.parseInt(str1[0]); - int b = Integer.parseInt(str1[1]); - int c = Integer.parseInt(str2[0]); - int d = Integer.parseInt(str2[1]); - - return (a * c - b * d) + "+" + (a * d + b * c) + "i"; - } - - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/ContainsDuplicateIi.java b/src/com/leetcodeTwo/editor/cn/ContainsDuplicateIi.java deleted file mode 100644 index ce4e8e5..0000000 --- a/src/com/leetcodeTwo/editor/cn/ContainsDuplicateIi.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.leetcode.editor.cn; - -//给你一个整数数组 nums 和一个整数 k ,判断数组中是否存在两个 不同的索引 i 和 j ,满足 nums[i] == nums[j] 且 abs(i -//- j) <= k 。如果存在,返回 true ;否则,返回 false 。 -// -// -// -// 示例 1: -// -// -//输入:nums = [1,2,3,1], k = 3 -//输出:true -// -// 示例 2: -// -// -//输入:nums = [1,0,1,1], k = 1 -//输出:true -// -// 示例 3: -// -// -//输入:nums = [1,2,3,1,2,3], k = 2 -//输出:false -// -// -// -// -// -// 提示: -// -// -// 1 <= nums.length <= 10⁵ -// -10⁹ <= nums[i] <= 10⁹ -// 0 <= k <= 10⁵ -// -// Related Topics 数组 哈希表 滑动窗口 👍 420 👎 0 - - -/** - * 219 存在重复元素 II - * - * @author shang.liang - * @date 2022-01-19 21:13:32 - */ -public class ContainsDuplicateIi { - public static void main(String[] args) { - Solution soution = new ContainsDuplicateIi().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public boolean containsNearbyDuplicate(int[] nums, int k) { - - for (int i = 0; i < nums.length; i++) { - - for (int j = i + 1; j <= Math.min(i + k, nums.length-1); j++) { - - if (nums[i] == nums[j]) { - return true; - } - - } - } - - return false; - } - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/CountNumberOfHomogenousSubstrings.java b/src/com/leetcodeTwo/editor/cn/CountNumberOfHomogenousSubstrings.java deleted file mode 100644 index f73baad..0000000 --- a/src/com/leetcodeTwo/editor/cn/CountNumberOfHomogenousSubstrings.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.leetcode.editor.cn; - -//给你一个字符串 s ,返回 s 中 同构子字符串 的数目。由于答案可能很大,只需返回对 10⁹ + 7 取余 后的结果。 -// -// 同构字符串 的定义为:如果一个字符串中的所有字符都相同,那么该字符串就是同构字符串。 -// -// 子字符串 是字符串中的一个连续字符序列。 -// -// -// -// 示例 1: -// -// 输入:s = "abbcccaa" -//输出:13 -//解释:同构子字符串如下所列: -//"a" 出现 3 次。 -//"aa" 出现 1 次。 -//"b" 出现 2 次。 -//"bb" 出现 1 次。 -//"c" 出现 3 次。 -//"cc" 出现 2 次。 -//"ccc" 出现 1 次。 -//3 + 1 + 2 + 1 + 3 + 2 + 1 = 13 -// -// 示例 2: -// -// 输入:s = "xy" -//输出:2 -//解释:同构子字符串是 "x" 和 "y" 。 -// -// 示例 3: -// -// 输入:s = "zzzzz" -//输出:15 -// -// -// -// -// 提示: -// -// -// 1 <= s.length <= 10⁵ -// s 由小写字符串组成 -// -// -// Related Topics 数学 字符串 👍 61 👎 0 - - -/** - * 1759 统计同构子字符串的数目 - * - * @author shang.liang - * @date 2022-12-26 22:13:37 - */ -public class CountNumberOfHomogenousSubstrings { - public static void main(String[] args) { - Solution soution = new CountNumberOfHomogenousSubstrings().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public int countHomogenous(String s) { - final int MOD = 1000000007; - long res = 0; - char prev = s.charAt(0); - int cnt = 0; - for (int i = 0; i < s.length(); i++) { - char c = s.charAt(i); - if (c == prev) { - cnt++; - } else { - res += (long) (cnt + 1) * cnt / 2; - cnt = 1; - prev = c; - } - } - res += (long) (cnt + 1) * cnt / 2; - return (int) (res % MOD); - - } - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/CountNumberOfPairsWithAbsoluteDifferenceK.java b/src/com/leetcodeTwo/editor/cn/CountNumberOfPairsWithAbsoluteDifferenceK.java deleted file mode 100644 index 55aa4ca..0000000 --- a/src/com/leetcodeTwo/editor/cn/CountNumberOfPairsWithAbsoluteDifferenceK.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.leetcode.editor.cn; - -//给你一个整数数组 nums 和一个整数 k ,请你返回数对 (i, j) 的数目,满足 i < j 且 |nums[i] - nums[j]| == k 。 -// -// -// |x| 的值定义为: -// -// -// 如果 x >= 0 ,那么值为 x 。 -// 如果 x < 0 ,那么值为 -x 。 -// -// -// -// -// 示例 1: -// -// 输入:nums = [1,2,2,1], k = 1 -//输出:4 -//解释:差的绝对值为 1 的数对为: -//- [1,2,2,1] -//- [1,2,2,1] -//- [1,2,2,1] -//- [1,2,2,1] -// -// -// 示例 2: -// -// 输入:nums = [1,3], k = 3 -//输出:0 -//解释:没有任何数对差的绝对值为 3 。 -// -// -// 示例 3: -// -// 输入:nums = [3,2,1,5,4], k = 2 -//输出:3 -//解释:差的绝对值为 2 的数对为: -//- [3,2,1,5,4] -//- [3,2,1,5,4] -//- [3,2,1,5,4] -// -// -// -// -// 提示: -// -// -// 1 <= nums.length <= 200 -// 1 <= nums[i] <= 100 -// 1 <= k <= 99 -// -// Related Topics 数组 哈希表 计数 👍 26 👎 0 - - -/** - * 2006 差的绝对值为 K 的数对数目 - * - * @author shang.liang - * @date 2022-02-09 10:00:57 - */ -public class CountNumberOfPairsWithAbsoluteDifferenceK { - public static void main(String[] args) { - Solution soution = new CountNumberOfPairsWithAbsoluteDifferenceK().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public int countKDifference(int[] nums, int k) { - - int tmpNum, res = 0; - - for (int i = 0; i < nums.length; i++) { - for (int j = i + 1; j < nums.length; j++) { - - tmpNum = nums[i] - nums[j]; - - if (tmpNum == k || tmpNum + k == 0) { - res++; - } - } - } - - return res; - } - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/CountVowelsPermutation.java b/src/com/leetcodeTwo/editor/cn/CountVowelsPermutation.java deleted file mode 100644 index 0dc8a6a..0000000 --- a/src/com/leetcodeTwo/editor/cn/CountVowelsPermutation.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.leetcode.editor.cn; - -//给你一个整数 n,请你帮忙统计一下我们可以按下述规则形成多少个长度为 n 的字符串: -// -// -// 字符串中的每个字符都应当是小写元音字母('a', 'e', 'i', 'o', 'u') -// 每个元音 'a' 后面都只能跟着 'e' -// 每个元音 'e' 后面只能跟着 'a' 或者是 'i' -// 每个元音 'i' 后面 不能 再跟着另一个 'i' -// 每个元音 'o' 后面只能跟着 'i' 或者是 'u' -// 每个元音 'u' 后面只能跟着 'a' -// -// -// 由于答案可能会很大,所以请你返回 模 10^9 + 7 之后的结果。 -// -// -// -// 示例 1: -// -// 输入:n = 1 -//输出:5 -//解释:所有可能的字符串分别是:"a", "e", "i" , "o" 和 "u"。 -// -// -// 示例 2: -// -// 输入:n = 2 -//输出:10 -//解释:所有可能的字符串分别是:"ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou" 和 "ua"。 -// -// -// 示例 3: -// -// 输入:n = 5 -//输出:68 -// -// -// -// 提示: -// -// -// 1 <= n <= 2 * 10^4 -// -// Related Topics 动态规划 👍 101 👎 0 - - -/** - * 1220 统计元音字母序列的数目 - * - * @author shang.liang - * @date 2022-01-17 16:12:33 - */ -public class CountVowelsPermutation { - public static void main(String[] args) { - Solution soution = new CountVowelsPermutation().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public int countVowelPermutation(int n) { - - long mod = 1000000007; - - long[] dp = new long[5]; - - for (int i = 0; i < 5; i++) { - dp[i] = 1; - } - - long[] ndp = new long[5]; - /* - 每个元音 'a' 后面都只能跟着 'e' - 每个元音 'e' 后面只能跟着 'a' 或者是 'i' - 每个元音 'i' 后面 不能 再跟着另一个 'i' - 每个元音 'o' 后面只能跟着 'i' 或者是 'u' - 每个元音 'u' 后面只能跟着 'a' - */ - /* - 元音字母 ‘a’ 前面只能跟着 ‘e’,‘i’,‘u’; - 元音字母 ‘e’ 前面只能跟着 ‘a’,‘i’; - 每个元音 ‘i’ 前面只能跟着 ‘e’,‘o’; - 每个元音 ‘o’ 前面只能跟着 ‘i’; - 每个元音 ‘u’ 前面只能跟着 ‘o’,‘i’; - */ - for (int i = 2; i <= n; i++) { - ndp[0] = (dp[1] + dp[2] + dp[4]) % mod; - ndp[1] = (dp[0] + dp[2]) % mod; - ndp[2] = (dp[1] + dp[3]) % mod; - ndp[3] = dp[2] % mod; - ndp[4] = (dp[2] + dp[3]) % mod; - System.arraycopy(ndp, 0, dp, 0, 5); - } - - long ans = 0; - for (int i = 0; i < 5; ++i) { - ans = (ans + dp[i]) % mod; - } - return (int)ans; - } - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/CountingWordsWithAGivenPrefix.java b/src/com/leetcodeTwo/editor/cn/CountingWordsWithAGivenPrefix.java deleted file mode 100644 index 9135b58..0000000 --- a/src/com/leetcodeTwo/editor/cn/CountingWordsWithAGivenPrefix.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.leetcode.editor.cn; - -//给你一个字符串数组 words 和一个字符串 pref 。 -// -// 返回 words 中以 pref 作为 前缀 的字符串的数目。 -// -// 字符串 s 的 前缀 就是 s 的任一前导连续字符串。 -// -// -// -// 示例 1: -// -// 输入:words = ["pay","attention","practice","attend"], pref = "at" -//输出:2 -//解释:以 "at" 作为前缀的字符串有两个,分别是:"attention" 和 "attend" 。 -// -// -// 示例 2: -// -// 输入:words = ["leetcode","win","loops","success"], pref = "code" -//输出:0 -//解释:不存在以 "code" 作为前缀的字符串。 -// -// -// -// -// 提示: -// -// -// 1 <= words.length <= 100 -// 1 <= words[i].length, pref.length <= 100 -// words[i] 和 pref 由小写英文字母组成 -// -// -// Related Topics 数组 字符串 👍 13 👎 0 - - -/** - * 2185 统计包含给定前缀的字符串 - * - * @author shang.liang - * @date 2023-01-08 01:21:33 - */ -public class CountingWordsWithAGivenPrefix { - public static void main(String[] args) { - Solution soution = new CountingWordsWithAGivenPrefix().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public int prefixCount(String[] words, String pref) { - int res = 0; - for (String word : words) { - if (word.startsWith(pref)) { - res++; - } - } - return res; - } - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/CustomSortString.java b/src/com/leetcodeTwo/editor/cn/CustomSortString.java deleted file mode 100644 index cf5541e..0000000 --- a/src/com/leetcodeTwo/editor/cn/CustomSortString.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.leetcode.editor.cn; - -//给定两个字符串 order 和 s 。order 的所有单词都是 唯一 的,并且以前按照一些自定义的顺序排序。 -// -// 对 s 的字符进行置换,使其与排序的 order 相匹配。更具体地说,如果在 order 中的字符 x 出现字符 y 之前,那么在排列后的字符串中, x -//也应该出现在 y 之前。 -// -// 返回 满足这个性质的 s 的任意排列 。 -// -// -// -// 示例 1: -// -// -//输入: order = "cba", s = "abcd" -//输出: "cbad" -//解释: -//“a”、“b”、“c”是按顺序出现的,所以“a”、“b”、“c”的顺序应该是“c”、“b”、“a”。 -//因为“d”不是按顺序出现的,所以它可以在返回的字符串中的任何位置。“dcba”、“cdba”、“cbda”也是有效的输出。 -// -// 示例 2: -// -// -//输入: order = "cbafg", s = "abcd" -//输出: "cbad" -// -// -// -// -// 提示: -// -// -// 1 <= order.length <= 26 -// 1 <= s.length <= 200 -// order 和 s 由小写英文字母组成 -// order 中的所有字符都 不同 -// -// -// Related Topics 哈希表 字符串 排序 👍 163 👎 0 - - -import java.util.Arrays; - -/** - * 791 自定义字符串排序 - * - * @author shang.liang - * @date 2022-11-13 20:47:51 - */ -public class CustomSortString { - public static void main(String[] args) { - Solution soution = new CustomSortString().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public String customSortString(String order, String s) { - int[] val = new int[26]; - for (int i = 0; i < order.length(); ++i) { - val[order.charAt(i) - 'a'] = i + 1; - } - Character[] arr = new Character[s.length()]; - for (int i = 0; i < s.length(); ++i) { - arr[i] = s.charAt(i); - } - Arrays.sort(arr, (c0, c1) -> val[c0 - 'a'] - val[c1 - 'a']); - StringBuilder ans = new StringBuilder(); - for (int i = 0; i < s.length(); ++i) { - ans.append(arr[i]); - } - return ans.toString(); - } - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/EvenOddTree.java b/src/com/leetcodeTwo/editor/cn/EvenOddTree.java deleted file mode 100644 index a588006..0000000 --- a/src/com/leetcodeTwo/editor/cn/EvenOddTree.java +++ /dev/null @@ -1,161 +0,0 @@ -package com.leetcode.editor.cn; - -import com.liang.leetcode.bean.TreeNode; - -import java.util.HashMap; -import java.util.Map; - -; - -//如果一棵二叉树满足下述几个条件,则可以称为 奇偶树 : -// -// -// 二叉树根节点所在层下标为 0 ,根的子节点所在层下标为 1 ,根的孙节点所在层下标为 2 ,依此类推。 -// 偶数下标 层上的所有节点的值都是 奇 整数,从左到右按顺序 严格递增 -// 奇数下标 层上的所有节点的值都是 偶 整数,从左到右按顺序 严格递减 -// -// -// 给你二叉树的根节点,如果二叉树为 奇偶树 ,则返回 true ,否则返回 false 。 -// -// -// -// 示例 1: -// -// -// -// -//输入:root = [1,10,4,3,null,7,9,12,8,6,null,null,2] -//输出:true -//解释:每一层的节点值分别是: -//0 层:[1] -//1 层:[10,4] -//2 层:[3,7,9] -//3 层:[12,8,6,2] -//由于 0 层和 2 层上的节点值都是奇数且严格递增,而 1 层和 3 层上的节点值都是偶数且严格递减,因此这是一棵奇偶树。 -// -// -// 示例 2: -// -// -// -// -//输入:root = [5,4,2,3,3,7] -//输出:false -//解释:每一层的节点值分别是: -//0 层:[5] -//1 层:[4,2] -//2 层:[3,3,7] -//2 层上的节点值不满足严格递增的条件,所以这不是一棵奇偶树。 -// -// -// 示例 3: -// -// -// -// -//输入:root = [5,9,1,3,5,7] -//输出:false -//解释:1 层上的节点值应为偶数。 -// -// -// 示例 4: -// -// -//输入:root = [1] -//输出:true -// -// -// 示例 5: -// -// -//输入:root = [11,8,6,1,3,9,11,30,20,18,16,12,10,4,2,17] -//输出:true -// -// -// -// -// 提示: -// -// -// 树中节点数在范围 [1, 105] 内 -// 1 <= Node.val <= 106 -// -// Related Topics 树 广度优先搜索 二叉树 -// 👍 58 👎 0 - - -/** - * 1609 奇偶树 - * @date 2021-12-25 22:24:07 - * @author shang.liang - */ -public class EvenOddTree{ - public static void main(String[] args) { - Solution solution = new EvenOddTree().new Solution(); - - } - -//leetcode submit region begin(Prohibit modification and deletion) -/** - * Definition for a binary tree node. - * public class TreeNode { - * int val; - * TreeNode left; - * TreeNode right; - * TreeNode() {} - * TreeNode(int val) { this.val = val; } - * TreeNode(int val, TreeNode left, TreeNode right) { - * this.val = val; - * this.left = left; - * this.right = right; - * } - * } - */ -class Solution { - - Map map = new HashMap<>(); - - public boolean isEvenOddTree(TreeNode root) { - return dfs(root, 0); - } - - public boolean dfs(TreeNode root, int index) { - - // 奇数层为 false , 偶数层为 true - boolean flag = index % 2 == 0; - - int curValue = root.val; - int preValue = map.getOrDefault(index, flag ? 0 : Integer.MAX_VALUE); - - /** - * 偶数行 - * 值为偶数 或者 值是递减,return false - */ - if (flag && (curValue % 2 == 0 || curValue <= preValue)) { - return false; - } - - /** - * 奇数行 - * 值为奇数 或者 值是递增,return false - */ - if (!flag && (curValue % 2 != 0 || curValue >= preValue)) { - return false; - } - - map.put(index, root.val); - - if (root.left != null && !dfs(root.left, index + 1)) { - return false; - } - - if (root.right != null && !dfs(root.right, index + 1)) { - return false; - } - - return true; - } -} -//leetcode submit region end(Prohibit modification and deletion) - -} \ No newline at end of file diff --git a/src/com/leetcodeTwo/editor/cn/FactorialTrailingZeroes.java b/src/com/leetcodeTwo/editor/cn/FactorialTrailingZeroes.java deleted file mode 100644 index ae323b2..0000000 --- a/src/com/leetcodeTwo/editor/cn/FactorialTrailingZeroes.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.leetcode.editor.cn; - -//给定一个整数 n ,返回 n! 结果中尾随零的数量。 -// -// 提示 n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1 -// -// -// -// 示例 1: -// -// -//输入:n = 3 -//输出:0 -//解释:3! = 6 ,不含尾随 0 -// -// -// 示例 2: -// -// -//输入:n = 5 -//输出:1 -//解释:5! = 120 ,有一个尾随 0 -// -// -// 示例 3: -// -// -//输入:n = 0 -//输出:0 -// -// -// -// -// 提示: -// -// -// 0 <= n <= 10⁴ -// -// -// -// -// 进阶:你可以设计并实现对数时间复杂度的算法来解决此问题吗? -// Related Topics 数学 👍 651 👎 0 - - -/** - * 172 阶乘后的零 - * @date 2022-03-25 23:25:58 - * @author shang.liang - */ - public class FactorialTrailingZeroes{ - public static void main(String[] args){ - Solution soution = new FactorialTrailingZeroes().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public int trailingZeroes(int n) { - - int res = 0; - - for (int i = 5; i <= n; i+=5) { - for (int j = i; j % 5 == 0; j /= 5) { - res++; - } - } - - return res; - } -} -//leetcode submit region end(Prohibit modification and deletion) - - } diff --git a/src/com/leetcodeTwo/editor/cn/FindCenterOfStarGraph.java b/src/com/leetcodeTwo/editor/cn/FindCenterOfStarGraph.java deleted file mode 100644 index b42cd97..0000000 --- a/src/com/leetcodeTwo/editor/cn/FindCenterOfStarGraph.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.leetcode.editor.cn; - -//有一个无向的 星型 图,由 n 个编号从 1 到 n 的节点组成。星型图有一个 中心 节点,并且恰有 n - 1 条边将中心节点与其他每个节点连接起来。 -// -// 给你一个二维整数数组 edges ,其中 edges[i] = [ui, vi] 表示在节点 ui 和 vi 之间存在一条边。请你找出并返回 edges -//所表示星型图的中心节点。 -// -// -// -// 示例 1: -// -// -//输入:edges = [[1,2],[2,3],[4,2]] -//输出:2 -//解释:如上图所示,节点 2 与其他每个节点都相连,所以节点 2 是中心节点。 -// -// -// 示例 2: -// -// -//输入:edges = [[1,2],[5,1],[1,3],[1,4]] -//输出:1 -// -// -// -// -// 提示: -// -// -// 3 <= n <= 10⁵ -// edges.length == n - 1 -// edges[i].length == 2 -// 1 <= ui, vi <= n -// ui != vi -// 题目数据给出的 edges 表示一个有效的星型图 -// -// Related Topics 图 👍 23 👎 0 - - -/** - * 1791 找出星型图的中心节点 - * @date 2022-02-18 09:22:47 - * @author shang.liang - */ - public class FindCenterOfStarGraph{ - public static void main(String[] args){ - Solution soution = new FindCenterOfStarGraph().new Solution(); - int[][] edges = new int[][]{{1, 2}, {2, 3}, {4, 2}}; - System.out.println("soution.findCenter(edges) = " + soution.findCenter(edges)); - } - - //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public int findCenter(int[][] edges) { - - int[] numsLine = new int[edges.length + 2]; - - for (int[] edge : edges) { - numsLine[edge[0]]++; - numsLine[edge[1]]++; - } - - for (int i = 0; i < numsLine.length; i++) { - if (numsLine[i] == edges.length) { - return i; - } - } - return 1; - } -} -//leetcode submit region end(Prohibit modification and deletion) - - } diff --git a/src/com/leetcodeTwo/editor/cn/FindKPairsWithSmallestSums.java b/src/com/leetcodeTwo/editor/cn/FindKPairsWithSmallestSums.java deleted file mode 100644 index 1e2d652..0000000 --- a/src/com/leetcodeTwo/editor/cn/FindKPairsWithSmallestSums.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.leetcode.editor.cn; - -//给定两个以升序排列的整数数组 nums1 和 nums2 , 以及一个整数 k 。 -// -// 定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2 。 -// -// 请找到和最小的 k 个数对 (u1,v1), (u2,v2) ... (uk,vk) 。 -// -// -// -// 示例 1: -// -// -//输入: nums1 = [1,7,11], nums2 = [2,4,6], k = 3 -//输出: [1,2],[1,4],[1,6] -//解释: 返回序列中的前 3 对数: -// [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6] -// -// -// 示例 2: -// -// -//输入: nums1 = [1,1,2], nums2 = [1,2,3], k = 2 -//输出: [1,1],[1,1] -//解释: 返回序列中的前 2 对数: -// [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3] -// -// -// 示例 3: -// -// -//输入: nums1 = [1,2], nums2 = [3], k = 3 -//输出: [1,3],[2,3] -//解释: 也可能序列中所有的数对都被返回:[1,3],[2,3] -// -// -// -// -// 提示: -// -// -// 1 <= nums1.length, nums2.length <= 10⁴ -// -10⁹ <= nums1[i], nums2[i] <= 10⁹ -// nums1, nums2 均为升序排列 -// 1 <= k <= 1000 -// -// Related Topics 数组 堆(优先队列) 👍 268 👎 0 - - -import java.util.ArrayList; -import java.util.List; -import java.util.PriorityQueue; - -/** - * 373 查找和最小的K对数字 - * - * @author shang.liang - * @date 2022-01-14 10:01:19 - */ -public class FindKPairsWithSmallestSums { - public static void main(String[] args) { - Solution soution = new FindKPairsWithSmallestSums().new Solution(); - int[] nums1 = new int[]{1, 7, 11}; - int[] nums2 = new int[]{2, 4, 6}; - List> lists = soution.kSmallestPairs(nums1, nums2, 2); - System.out.println("lists = " + lists); - - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public List> kSmallestPairs(int[] nums1, int[] nums2, int k) { - PriorityQueue pq = new PriorityQueue<>(k, (o1, o2)->{ - return nums1[o1[0]] + nums2[o1[1]] - nums1[o2[0]] - nums2[o2[1]]; - }); - List> ans = new ArrayList<>(); - int m = nums1.length; - int n = nums2.length; - for (int i = 0; i < Math.min(m, k); i++) { - pq.offer(new int[]{i, 0}); - } - while (k-- > 0 && !pq.isEmpty()) { - int[] idxPair = pq.poll(); - List list = new ArrayList<>(); - list.add(nums1[idxPair[0]]); - list.add(nums2[idxPair[1]]); - ans.add(list); - if (idxPair[1] + 1 < n) { - pq.offer(new int[]{idxPair[0], idxPair[1] + 1}); - } - } - - return ans; - } - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/FindMissingObservations.java b/src/com/leetcodeTwo/editor/cn/FindMissingObservations.java deleted file mode 100644 index 44537cc..0000000 --- a/src/com/leetcodeTwo/editor/cn/FindMissingObservations.java +++ /dev/null @@ -1,102 +0,0 @@ -package com.leetcode.editor.cn; - -//现有一份 n + m 次投掷单个 六面 骰子的观测数据,骰子的每个面从 1 到 6 编号。观测数据中缺失了 n 份,你手上只拿到剩余 m 次投掷的数据。幸好 -//你有之前计算过的这 n + m 次投掷数据的 平均值 。 -// -// 给你一个长度为 m 的整数数组 rolls ,其中 rolls[i] 是第 i 次观测的值。同时给你两个整数 mean 和 n 。 -// -// 返回一个长度为 n 的数组,包含所有缺失的观测数据,且满足这 n + m 次投掷的 平均值 是 mean 。如果存在多组符合要求的答案,只需要返回其中任意 -//一组即可。如果不存在答案,返回一个空数组。 -// -// k 个数字的 平均值 为这些数字求和后再除以 k 。 -// -// 注意 mean 是一个整数,所以 n + m 次投掷的总和需要被 n + m 整除。 -// -// -// -// 示例 1: -// -// -//输入:rolls = [3,2,4,3], mean = 4, n = 2 -//输出:[6,6] -//解释:所有 n + m 次投掷的平均值是 (3 + 2 + 4 + 3 + 6 + 6) / 6 = 4 。 -// -// -// 示例 2: -// -// -//输入:rolls = [1,5,6], mean = 3, n = 4 -//输出:[2,3,2,2] -//解释:所有 n + m 次投掷的平均值是 (1 + 5 + 6 + 2 + 3 + 2 + 2) / 7 = 3 。 -// -// -// 示例 3: -// -// -//输入:rolls = [1,2,3,4], mean = 6, n = 4 -//输出:[] -//解释:无论丢失的 4 次数据是什么,平均值都不可能是 6 。 -// -// -// 示例 4: -// -// -//输入:rolls = [1], mean = 3, n = 1 -//输出:[5] -//解释:所有 n + m 次投掷的平均值是 (1 + 5) / 2 = 3 。 -// -// -// -// -// 提示: -// -// -// m == rolls.length -// 1 <= n, m <= 10⁵ -// 1 <= rolls[i], mean <= 6 -// -// Related Topics 数组 数学 模拟 👍 47 👎 0 - - -import java.util.Arrays; - -/** - * 2028 找出缺失的观测数据 - * - * @author shang.liang - * @date 2022-03-27 19:59:17 - */ -public class FindMissingObservations { - public static void main(String[] args) { - Solution soution = new FindMissingObservations().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public int[] missingRolls(int[] rolls, int mean, int n) { - - int[] res = new int[n]; - - int sum = Arrays.stream(rolls).sum(); - int nTotal = mean * rolls.length - sum + n * mean; - - if (nTotal < n || nTotal > 6 * n) { - return new int[0]; - } - - int index = 0; - for (int i = 0; i < nTotal; i++) { - if (index == n) { - index = 0; - } - res[index++]++; - - } - - return res; - } - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/FindTheClosestPalindrome.java b/src/com/leetcodeTwo/editor/cn/FindTheClosestPalindrome.java deleted file mode 100644 index e6dbce3..0000000 --- a/src/com/leetcodeTwo/editor/cn/FindTheClosestPalindrome.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.leetcode.editor.cn; - -//给定一个表示整数的字符串 n ,返回与它最近的回文整数(不包括自身)。如果不止一个,返回较小的那个。 -// -// “最近的”定义为两个整数差的绝对值最小。 -// -// -// -// 示例 1: -// -// -//输入: n = "123" -//输出: "121" -// -// -// 示例 2: -// -// -//输入: n = "1" -//输出: "0" -//解释: 0 和 2是最近的回文,但我们返回最小的,也就是 0。 -// -// -// -// -// 提示: -// -// -// 1 <= n.length <= 18 -// n 只由数字组成 -// n 不含前导 0 -// n 代表在 [1, 10¹⁸ - 1] 范围内的整数 -// -// Related Topics 数学 字符串 👍 163 👎 0 - - -/** - * 564 寻找最近的回文数 - * @date 2022-03-02 12:01:50 - * @author shang.liang - */ - public class FindTheClosestPalindrome{ - public static void main(String[] args){ - Solution soution = new FindTheClosestPalindrome().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public String nearestPalindromic(String n) { - - - return ""; - } -} -//leetcode submit region end(Prohibit modification and deletion) - - } diff --git a/src/com/leetcodeTwo/editor/cn/FindTheHighestAltitude.java b/src/com/leetcodeTwo/editor/cn/FindTheHighestAltitude.java deleted file mode 100644 index 140f15d..0000000 --- a/src/com/leetcodeTwo/editor/cn/FindTheHighestAltitude.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.leetcode.editor.cn; - -//有一个自行车手打算进行一场公路骑行,这条路线总共由 n + 1 个不同海拔的点组成。自行车手从海拔为 0 的点 0 开始骑行。 -// -// 给你一个长度为 n 的整数数组 gain ,其中 gain[i] 是点 i 和点 i + 1 的 净海拔高度差(0 <= i < n)。请你返回 最高点的 -//海拔 。 -// -// -// -// 示例 1: -// -// -//输入:gain = [-5,1,5,0,-7] -//输出:1 -//解释:海拔高度依次为 [0,-5,-4,1,1,-6] 。最高海拔为 1 。 -// -// -// 示例 2: -// -// -//输入:gain = [-4,-3,-2,-1,4,3,2] -//输出:0 -//解释:海拔高度依次为 [0,-4,-7,-9,-10,-6,-3,-1] 。最高海拔为 0 。 -// -// -// -// -// 提示: -// -// -// n == gain.length -// 1 <= n <= 100 -// -100 <= gain[i] <= 100 -// -// -// Related Topics 数组 前缀和 👍 47 👎 0 - - -/** - * 1732 找到最高海拔 - * @date 2022-11-19 15:32:32 - * @author shang.liang - */ - public class FindTheHighestAltitude{ - public static void main(String[] args){ - Solution soution = new FindTheHighestAltitude().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public int largestAltitude(int[] gain) { - - int current = 0; - int max = 0; - - for (int g : gain) { - current += g; - max = Math.max(max, current); - } - - return max; - } -} -//leetcode submit region end(Prohibit modification and deletion) - - } diff --git a/src/com/leetcodeTwo/editor/cn/FriendsOfAppropriateAges.java b/src/com/leetcodeTwo/editor/cn/FriendsOfAppropriateAges.java deleted file mode 100644 index 6afec8c..0000000 --- a/src/com/leetcodeTwo/editor/cn/FriendsOfAppropriateAges.java +++ /dev/null @@ -1,120 +0,0 @@ -package com.leetcode.editor.cn; - -import java.util.Arrays; - -; - -//在社交媒体网站上有 n 个用户。给你一个整数数组 ages ,其中 ages[i] 是第 i 个用户的年龄。 -// -// 如果下述任意一个条件为真,那么用户 x 将不会向用户 y(x != y)发送好友请求: -// -// -// age[y] <= 0.5 * age[x] + 7 -// age[y] > age[x] -// age[y] > 100 && age[x] < 100 -// -// -// 否则,x 将会向 y 发送一条好友请求。 -// -// 注意,如果 x 向 y 发送一条好友请求,y 不必也向 x 发送一条好友请求。另外,用户不会向自己发送好友请求。 -// -// 返回在该社交媒体网站上产生的好友请求总数。 -// -// -// -// 示例 1: -// -// -//输入:ages = [16,16] -//输出:2 -//解释:2 人互发好友请求。 -// -// -// 示例 2: -// -// -//输入:ages = [16,17,18] -//输出:2 -//解释:产生的好友请求为 17 -> 16 ,18 -> 17 。 -// -// -// 示例 3: -// -// -//输入:ages = [20,30,100,110,120] -//输出:3 -//解释:产生的好友请求为 110 -> 100 ,120 -> 110 ,120 -> 100 。 -// -// -// -// -// 提示: -// -// -// n == ages.length -// 1 <= n <= 2 * 104 -// 1 <= ages[i] <= 120 -// -// Related Topics 数组 双指针 二分查找 排序 -// 👍 99 👎 0 - -/** - * 825 适龄的朋友 - * - * @author shang.liang - * @date 2021-12-27 10:17:10 - */ -public class FriendsOfAppropriateAges { - public static void main(String[] args) { - Solution solution = new FriendsOfAppropriateAges().new Solution(); - - int[] ages = new int[]{16, 17, 18}; - System.out.println("solution.numFriendRequests(ages) = " + solution.numFriendRequests(ages)); - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public int numFriendRequests(int[] ages) { - - Arrays.sort(ages); - int n = ages.length, ans = 0; - for (int k = 0, i = 0, j = 0; k < n; k++) { - while (i < k && !check(ages[i], ages[k])) { - i++; - } - if (j < k) { - j = k; - } - while (j < n && check(ages[j], ages[k])) { - j++; - } - if (j > i) { - ans += j - i - 1; - } - } - return ans; - } - - public boolean check(int x, int y) { - - // age[y] <= 0.5 * age[x] + 7 - // age[y] > age[x] - // age[y] > 100 && age[x] < 100 - - if (y <= (0.5 * x + 7)) { - return false; - } - - if (y > x) { - return false; - } - - if ((x < 100) && (y > 100)) { - return false; - } - - return true; - } - } -//leetcode submit region end(Prohibit modification and deletion) -} \ No newline at end of file diff --git a/src/com/leetcodeTwo/editor/cn/IncreasingTripletSubsequence.java b/src/com/leetcodeTwo/editor/cn/IncreasingTripletSubsequence.java deleted file mode 100644 index 2ce3ee0..0000000 --- a/src/com/leetcodeTwo/editor/cn/IncreasingTripletSubsequence.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.leetcode.editor.cn; - -//给你一个整数数组 nums ,判断这个数组中是否存在长度为 3 的递增子序列。 -// -// 如果存在这样的三元组下标 (i, j, k) 且满足 i < j < k ,使得 nums[i] < nums[j] < nums[k] ,返回 -//true ;否则,返回 false 。 -// -// -// -// 示例 1: -// -// -//输入:nums = [1,2,3,4,5] -//输出:true -//解释:任何 i < j < k 的三元组都满足题意 -// -// -// 示例 2: -// -// -//输入:nums = [5,4,3,2,1] -//输出:false -//解释:不存在满足题意的三元组 -// -// 示例 3: -// -// -//输入:nums = [2,1,5,0,4,6] -//输出:true -//解释:三元组 (3, 4, 5) 满足题意,因为 nums[3] == 0 < nums[4] == 4 < nums[5] == 6 -// -// -// -// -// 提示: -// -// -// 1 <= nums.length <= 5 * 10⁵ -// -2³¹ <= nums[i] <= 2³¹ - 1 -// -// -// -// -// 进阶:你能实现时间复杂度为 O(n) ,空间复杂度为 O(1) 的解决方案吗? -// Related Topics 贪心 数组 👍 489 👎 0 - - -/** - * 334 递增的三元子序列 - * - * @author shang.liang - * @date 2022-01-12 21:30:33 - */ -public class IncreasingTripletSubsequence { - public static void main(String[] args) { - Solution soution = new IncreasingTripletSubsequence().new Solution(); - int[] nums = new int[]{1, 5, 0, 6}; - System.out.println("soution.increasingTriplet(nums) = " + soution.increasingTriplet(nums)); - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public boolean increasingTriplet(int[] nums) { - int a = Integer.MAX_VALUE, b = Integer.MAX_VALUE; - - for (int x : nums) - if (x <= a) a = x; - else if (x <= b) b = x; - else return true; - return false; - } - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/KnightProbabilityInChessboard.java b/src/com/leetcodeTwo/editor/cn/KnightProbabilityInChessboard.java deleted file mode 100644 index 2f8adfa..0000000 --- a/src/com/leetcodeTwo/editor/cn/KnightProbabilityInChessboard.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.leetcode.editor.cn; - -//在一个 n x n 的国际象棋棋盘上,一个骑士从单元格 (row, column) 开始,并尝试进行 k 次移动。行和列是 从 0 开始 的,所以左上单元格 -//是 (0,0) ,右下单元格是 (n - 1, n - 1) 。 -// -// 象棋骑士有8种可能的走法,如下图所示。每次移动在基本方向上是两个单元格,然后在正交方向上是一个单元格。 -// -// -// -// 每次骑士要移动时,它都会随机从8种可能的移动中选择一种(即使棋子会离开棋盘),然后移动到那里。 -// -// 骑士继续移动,直到它走了 k 步或离开了棋盘。 -// -// 返回 骑士在棋盘停止移动后仍留在棋盘上的概率 。 -// -// -// -// 示例 1: -// -// -//输入: n = 3, k = 2, row = 0, column = 0 -//输出: 0.0625 -//解释: 有两步(到(1,2),(2,1))可以让骑士留在棋盘上。 -//在每一个位置上,也有两种移动可以让骑士留在棋盘上。 -//骑士留在棋盘上的总概率是0.0625。 -// -// -// 示例 2: -// -// -//输入: n = 1, k = 0, row = 0, column = 0 -//输出: 1.00000 -// -// -// -// -// 提示: -// -// -// 1 <= n <= 25 -// 0 <= k <= 100 -// 0 <= row, column <= n -// -// Related Topics 动态规划 👍 195 👎 0 - - -/** - * 688 骑士在棋盘上的概率 - * - * @author shang.liang - * @date 2022-02-17 11:27:42 - */ -public class KnightProbabilityInChessboard { - public static void main(String[] args) { - Solution soution = new KnightProbabilityInChessboard().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - - int[][] dirs = {{-2, -1}, {-2, 1}, - {2, -1}, {2, 1}, {-1, -2}, - {-1, 2}, {1, -2}, {1, 2}}; - - public double knightProbability(int n, int k, int row, int column) { - - double[][][] dp = new double[n][n][k + 1]; - - for (int i = 0; i <= k; i++) { - for (int j = 0; j < n; j++) { - for (int m = 0; m < n; m++) { - if (i == 0) { - dp[j][m][i] = 1; - continue; - } - for (int[] dir : dirs) { - int jp = j + dir[0]; - int mp = m + dir[1]; - if (jp >= 0 && jp < n && mp >= 0 && mp < n) { - dp[j][m][i] += dp[jp][mp][i - 1] / 8; - } - } - - } - } - } - - return dp[row][column][k]; - } - - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/LargestNumberAtLeastTwiceOfOthers.java b/src/com/leetcodeTwo/editor/cn/LargestNumberAtLeastTwiceOfOthers.java deleted file mode 100644 index 81d3111..0000000 --- a/src/com/leetcodeTwo/editor/cn/LargestNumberAtLeastTwiceOfOthers.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.leetcode.editor.cn; - -//给你一个整数数组 nums ,其中总是存在 唯一的 一个最大整数 。 -// -// 请你找出数组中的最大元素并检查它是否 至少是数组中每个其他数字的两倍 。如果是,则返回 最大元素的下标 ,否则返回 -1 。 -// -// -// -// 示例 1: -// -// -//输入:nums = [3,6,1,0] -//输出:1 -//解释:6 是最大的整数,对于数组中的其他整数,6 大于数组中其他元素的两倍。6 的下标是 1 ,所以返回 1 。 -// -// -// 示例 2: -// -// -//输入:nums = [1,2,3,4] -//输出:-1 -//解释:4 没有超过 3 的两倍大,所以返回 -1 。 -// -// 示例 3: -// -// -//输入:nums = [1] -//输出:0 -//解释:因为不存在其他数字,所以认为现有数字 1 至少是其他数字的两倍。 -// -// -// -// -// 提示: -// -// -// 1 <= nums.length <= 50 -// 0 <= nums[i] <= 100 -// nums 中的最大元素是唯一的 -// -// Related Topics 数组 排序 👍 149 👎 0 - - -/** - * 747 至少是其他数字两倍的最大数 - * - * @author shang.liang - * @date 2022-01-13 21:36:15 - */ -public class LargestNumberAtLeastTwiceOfOthers { - public static void main(String[] args) { - Solution soution = new LargestNumberAtLeastTwiceOfOthers().new Solution(); - int[] nums = new int[]{0, 1, 1, 2}; - System.out.println("soution.dominantIndex(nums) = " + soution.dominantIndex(nums)); - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public int dominantIndex(int[] nums) { - - int a = Integer.MIN_VALUE; - int b = Integer.MIN_VALUE; - int index = -1; - - for (int i = 0; i < nums.length; i++) { - - if (nums[i] < a) { - b = Math.max(b, nums[i]); - continue; - } - b = Math.max(a, b); - a = nums[i]; - index = i; - - } - - if (a < b * 2) { - return -1; - } - - return index; - } - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/LetterCasePermutation.java b/src/com/leetcodeTwo/editor/cn/LetterCasePermutation.java deleted file mode 100644 index 76503a3..0000000 --- a/src/com/leetcodeTwo/editor/cn/LetterCasePermutation.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.leetcode.editor.cn; - -//给定一个字符串 s ,通过将字符串 s 中的每个字母转变大小写,我们可以获得一个新的字符串。 -// -// 返回 所有可能得到的字符串集合 。以 任意顺序 返回输出。 -// -// -// -// 示例 1: -// -// -//输入:s = "a1b2" -//输出:["a1b2", "a1B2", "A1b2", "A1B2"] -// -// -// 示例 2: -// -// -//输入: s = "3z4" -//输出: ["3z4","3Z4"] -// -// -// -// -// 提示: -// -// -// 1 <= s.length <= 12 -// s 由小写英文字母、大写英文字母和数字组成 -// -// -// Related Topics 位运算 字符串 回溯 👍 445 👎 0 - - -import java.util.ArrayList; -import java.util.List; - -/** - * 784 字母大小写全排列 - * - * @author shang.liang - * @date 2022-10-30 11:03:45 - */ -public class LetterCasePermutation { - public static void main(String[] args) { - Solution soution = new LetterCasePermutation().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - char[] cs; - List ans = new ArrayList<>(); - - public List letterCasePermutation(String s) { - cs = s.toCharArray(); - dfs(0, s.length(), new char[s.length()]); - return ans; - } - - void dfs(int idx, int n, char[] cur) { - if (idx == n) { - ans.add(String.valueOf(cur)); - return; - } - cur[idx] = cs[idx]; - dfs(idx + 1, n, cur); - if (Character.isLetter(cs[idx])) { - cur[idx] = (char) (cs[idx] ^ 32); - dfs(idx + 1, n, cur); - } - } - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/LinkedListRandomNode.java b/src/com/leetcodeTwo/editor/cn/LinkedListRandomNode.java deleted file mode 100644 index 06d0832..0000000 --- a/src/com/leetcodeTwo/editor/cn/LinkedListRandomNode.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.leetcode.editor.cn; - -//给你一个单链表,随机选择链表的一个节点,并返回相应的节点值。每个节点 被选中的概率一样 。 -// -// 实现 Solution 类: -// -// -// Solution(ListNode head) 使用整数数组初始化对象。 -// int getRandom() 从链表中随机选择一个节点并返回该节点的值。链表中所有节点被选中的概率相等。 -// -// -// -// -// 示例: -// -// -//输入 -//["Solution", "getRandom", "getRandom", "getRandom", "getRandom", "getRandom"] -//[[[1, 2, 3]], [], [], [], [], []] -//输出 -//[null, 1, 3, 2, 2, 3] -// -//解释 -//Solution solution = new Solution([1, 2, 3]); -//solution.getRandom(); // 返回 1 -//solution.getRandom(); // 返回 3 -//solution.getRandom(); // 返回 2 -//solution.getRandom(); // 返回 2 -//solution.getRandom(); // 返回 3 -//// getRandom() 方法应随机返回 1、2、3中的一个,每个元素被返回的概率相等。 -// -// -// -// 提示: -// -// -// 链表中的节点数在范围 [1, 10⁴] 内 -// -10⁴ <= Node.val <= 10⁴ -// 至多调用 getRandom 方法 10⁴ 次 -// -// -// -// -// 进阶: -// -// -// 如果链表非常大且长度未知,该怎么处理? -// 你能否在不使用额外空间的情况下解决此问题? -// -// Related Topics 水塘抽样 链表 数学 随机化 👍 231 👎 0 - - -import com.liang.leetcode.bean.ListNode; - -import java.util.Random; - -/** - * 382 链表随机节点 - * - * @author shang.liang - * @date 2022-01-16 19:31:06 - */ -public class LinkedListRandomNode { - public static void main(String[] args) { - Solution soution = new LinkedListRandomNode().new Solution(new ListNode()); - - } - - //leetcode submit region begin(Prohibit modification and deletion) - - /** - * Definition for singly-linked list. - * public class ListNode { - * int val; - * ListNode next; - * ListNode() {} - * ListNode(int val) { this.val = val; } - * ListNode(int val, ListNode next) { this.val = val; this.next = next; } - * } - */ - class Solution { - - ListNode head; - Random random; - - public Solution(ListNode head) { - this.head = head; - random = new Random(); - } - - public int getRandom() { - int res = 0; - int i = 1; - - for (ListNode node = head; node != null; node = node.next) { - - if (random.nextInt(i) == 0) { - res = node.val; - } - - i++; - } - - return res; - } - } - -/** - * Your Solution object will be instantiated and called as such: - * Solution obj = new Solution(head); - * int param_1 = obj.getRandom(); - */ -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/LongestHappyString.java b/src/com/leetcodeTwo/editor/cn/LongestHappyString.java deleted file mode 100644 index 1d0e6ce..0000000 --- a/src/com/leetcodeTwo/editor/cn/LongestHappyString.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.leetcode.editor.cn; - -//如果字符串中不含有任何 'aaa','bbb' 或 'ccc' 这样的字符串作为子串,那么该字符串就是一个「快乐字符串」。 -// -// 给你三个整数 a,b ,c,请你返回 任意一个 满足下列全部条件的字符串 s: -// -// -// s 是一个尽可能长的快乐字符串。 -// s 中 最多 有a 个字母 'a'、b 个字母 'b'、c 个字母 'c' 。 -// s 中只含有 'a'、'b' 、'c' 三种字母。 -// -// -// 如果不存在这样的字符串 s ,请返回一个空字符串 ""。 -// -// -// -// 示例 1: -// -// 输入:a = 1, b = 1, c = 7 -//输出:"ccaccbcc" -//解释:"ccbccacc" 也是一种正确答案。 -// -// -// 示例 2: -// -// 输入:a = 2, b = 2, c = 1 -//输出:"aabbc" -// -// -// 示例 3: -// -// 输入:a = 7, b = 1, c = 0 -//输出:"aabaa" -//解释:这是该测试用例的唯一正确答案。 -// -// -// -// 提示: -// -// -// 0 <= a, b, c <= 100 -// a + b + c > 0 -// -// Related Topics 贪心 字符串 堆(优先队列) 👍 168 👎 0 - - -import java.util.PriorityQueue; - -/** - * 1405 最长快乐字符串 - * - * @author shang.liang - * @date 2022-02-07 22:59:44 - */ -public class LongestHappyString { - public static void main(String[] args) { - Solution soution = new LongestHappyString().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public String longestDiverseString(int a, int b, int c) { - - PriorityQueue q = new PriorityQueue<>((x, y) -> y[1] - x[1]); - if (a > 0) q.add(new int[]{0, a}); - if (b > 0) q.add(new int[]{1, b}); - if (c > 0) q.add(new int[]{2, c}); - StringBuilder sb = new StringBuilder(); - while (!q.isEmpty()) { - int[] cur = q.poll(); - int n = sb.length(); - if (n >= 2 && sb.charAt(n - 1) - 'a' == cur[0] && sb.charAt(n - 2) - 'a' == cur[0]) { - if (q.isEmpty()) break; - int[] next = q.poll(); - sb.append((char) (next[0] + 'a')); - if (--next[1] != 0) q.add(next); - q.add(cur); - } else { - sb.append((char) (cur[0] + 'a')); - if (--cur[1] != 0) q.add(cur); - } - } - return sb.toString(); - } - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/LuckyNumbersInAMatrix.java b/src/com/leetcodeTwo/editor/cn/LuckyNumbersInAMatrix.java deleted file mode 100644 index 1a2f8a1..0000000 --- a/src/com/leetcodeTwo/editor/cn/LuckyNumbersInAMatrix.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.leetcode.editor.cn; - -//给你一个 m * n 的矩阵,矩阵中的数字 各不相同 。请你按 任意 顺序返回矩阵中的所有幸运数。 -// -// 幸运数是指矩阵中满足同时下列两个条件的元素: -// -// -// 在同一行的所有元素中最小 -// 在同一列的所有元素中最大 -// -// -// -// -// 示例 1: -// -// 输入:matrix = [[3,7,8],[9,11,13],[15,16,17]] -//输出:[15] -//解释:15 是唯一的幸运数,因为它是其所在行中的最小值,也是所在列中的最大值。 -// -// -// 示例 2: -// -// 输入:matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]] -//输出:[12] -//解释:12 是唯一的幸运数,因为它是其所在行中的最小值,也是所在列中的最大值。 -// -// -// 示例 3: -// -// 输入:matrix = [[7,8],[1,2]] -//输出:[7] -// -// -// -// -// 提示: -// -// -// m == mat.length -// n == mat[i].length -// 1 <= n, m <= 50 -// 1 <= matrix[i][j] <= 10^5 -// 矩阵中的所有元素都是不同的 -// -// Related Topics 数组 矩阵 👍 95 👎 0 - - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * 1380 矩阵中的幸运数 - * @date 2022-02-15 17:36:20 - * @author shang.liang - */ - public class LuckyNumbersInAMatrix{ - public static void main(String[] args){ - Solution soution = new LuckyNumbersInAMatrix().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public List luckyNumbers (int[][] matrix) { - - List res = new ArrayList<>(); - - int[] dpRow = new int[matrix.length]; - Arrays.fill(dpRow, Integer.MAX_VALUE); - int[] dpCol = new int[matrix[0].length]; - - for (int i = 0; i < matrix.length; i++) { - for (int j = 0; j < matrix[i].length; j++) { - dpRow[i] = Math.min(dpRow[i], matrix[i][j]); - dpCol[j] = Math.max(dpCol[j], matrix[i][j]); - } - } - - - for (int i = 0; i < matrix.length; i++) { - for (int j = 0; j < matrix[0].length; j++) { - if (dpRow[i] == matrix[i][j] && dpCol[j] == matrix[i][j]) { - res.add(matrix[i][j]); - } - } - } - - return res; - } -} -//leetcode submit region end(Prohibit modification and deletion) - - } diff --git a/src/com/leetcodeTwo/editor/cn/MaximumDifferenceBetweenIncreasingElements.java b/src/com/leetcodeTwo/editor/cn/MaximumDifferenceBetweenIncreasingElements.java deleted file mode 100644 index 2a131ae..0000000 --- a/src/com/leetcodeTwo/editor/cn/MaximumDifferenceBetweenIncreasingElements.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.leetcode.editor.cn; - -//给你一个下标从 0 开始的整数数组 nums ,该数组的大小为 n ,请你计算 nums[j] - nums[i] 能求得的 最大差值 ,其中 0 <= -//i < j < n 且 nums[i] < nums[j] 。 -// -// 返回 最大差值 。如果不存在满足要求的 i 和 j ,返回 -1 。 -// -// -// -// 示例 1: -// -// 输入:nums = [7,1,5,4] -//输出:4 -//解释: -//最大差值出现在 i = 1 且 j = 2 时,nums[j] - nums[i] = 5 - 1 = 4 。 -//注意,尽管 i = 1 且 j = 0 时 ,nums[j] - nums[i] = 7 - 1 = 6 > 4 ,但 i > j 不满足题面要求,所以 6 -// 不是有效的答案。 -// -// -// 示例 2: -// -// 输入:nums = [9,4,3,2] -//输出:-1 -//解释: -//不存在同时满足 i < j 和 nums[i] < nums[j] 这两个条件的 i, j 组合。 -// -// -// 示例 3: -// -// 输入:nums = [1,5,2,10] -//输出:9 -//解释: -//最大差值出现在 i = 0 且 j = 3 时,nums[j] - nums[i] = 10 - 1 = 9 。 -// -// -// -// -// 提示: -// -// -// n == nums.length -// 2 <= n <= 1000 -// 1 <= nums[i] <= 10⁹ -// -// Related Topics 数组 👍 63 👎 0 - - -/** - * 2016 增量元素之间的最大差值 - * - * @author shang.liang - * @date 2022-02-26 18:06:16 - */ -public class MaximumDifferenceBetweenIncreasingElements { - public static void main(String[] args) { - Solution soution = new MaximumDifferenceBetweenIncreasingElements().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public int maximumDifference(int[] nums) { - - int min = nums[0]; - int ans = -1; - - for (int i = 1; i < nums.length; i++) { - - if (nums[i] > min) { - ans = Math.max(ans, nums[i] - min); - } else { - min = nums[i]; - } - - } - - return ans; - - } - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/MaximumNumberOfEatenApples.java b/src/com/leetcodeTwo/editor/cn/MaximumNumberOfEatenApples.java deleted file mode 100644 index be20308..0000000 --- a/src/com/leetcodeTwo/editor/cn/MaximumNumberOfEatenApples.java +++ /dev/null @@ -1,118 +0,0 @@ -package com.leetcode.editor.cn; - -import java.util.PriorityQueue; - -; - -//有一棵特殊的苹果树,一连 n 天,每天都可以长出若干个苹果。在第 i 天,树上会长出 apples[i] 个苹果,这些苹果将会在 days[i] 天后(也就 -//是说,第 i + days[i] 天时)腐烂,变得无法食用。也可能有那么几天,树上不会长出新的苹果,此时用 apples[i] == 0 且 days[i] = -//= 0 表示。 -// -// 你打算每天 最多 吃一个苹果来保证营养均衡。注意,你可以在这 n 天之后继续吃苹果。 -// -// 给你两个长度为 n 的整数数组 days 和 apples ,返回你可以吃掉的苹果的最大数目。 -// -// -// -// 示例 1: -// -// 输入:apples = [1,2,3,5,2], days = [3,2,1,4,2] -//输出:7 -//解释:你可以吃掉 7 个苹果: -//- 第一天,你吃掉第一天长出来的苹果。 -//- 第二天,你吃掉一个第二天长出来的苹果。 -//- 第三天,你吃掉一个第二天长出来的苹果。过了这一天,第三天长出来的苹果就已经腐烂了。 -//- 第四天到第七天,你吃的都是第四天长出来的苹果。 -// -// -// 示例 2: -// -// 输入:apples = [3,0,0,0,0,2], days = [3,0,0,0,0,2] -//输出:5 -//解释:你可以吃掉 5 个苹果: -//- 第一天到第三天,你吃的都是第一天长出来的苹果。 -//- 第四天和第五天不吃苹果。 -//- 第六天和第七天,你吃的都是第六天长出来的苹果。 -// -// -// -// -// 提示: -// -// -// apples.length == n -// days.length == n -// 1 <= n <= 2 * 104 -// 0 <= apples[i], days[i] <= 2 * 104 -// 只有在 apples[i] = 0 时,days[i] = 0 才成立 -// -// Related Topics 贪心 数组 堆(优先队列) -// 👍 56 👎 0 - -/** - * 1705 吃苹果的最大数目 - * - * @author shang.liang - * @date 2021-12-24 08:33:22 - */ -public class MaximumNumberOfEatenApples { - public static void main(String[] args) { - Solution solution = new MaximumNumberOfEatenApples().new Solution(); - int[] apples = new int[]{1, 10, 17, 10, 12, 6, 20, 8, 8, 22, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 5, 2, 1, 0, 0, 0, 0, 0, 0, 23}; - int[] days = new int[]{19999, 11, 18, 22, 5, 2, 14, 5, 20, 7, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 1, 4, 2, 7, 0, 0, 0, 0, 0, 0, 1}; - System.out.println(solution.eatenApples(apples, days)); - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public int eatenApples(int[] apples, int[] days) { - - int res = 0; - int day = 0; - - PriorityQueue queue = new PriorityQueue<>((o1, o2) -> (o1[0] - o2[0])); - - for (int i = 0; i < apples.length; i++) { - - if (apples[i] > 0) { - queue.add(new Integer[]{days[i] + i, apples[i]}); - } - - boolean eat = false; - while (!queue.isEmpty() && !eat) { - Integer[] poll = queue.peek(); - if (poll[1] > 0 && poll[0] > day) { - eat = true; - res++; - if(poll[1] == 1){ - queue.poll(); - }else{ - queue.peek()[1]--; - } - } else { - queue.poll(); - } - } - day++; - } - - while (!queue.isEmpty()) { - Integer[] poll = queue.peek(); - - if (poll[1] > 0 && poll[0] > day) { - day++; - res++; - if(poll[1] == 1){ - queue.poll(); - }else{ - queue.peek()[1]--; - } - } else { - queue.poll(); - } - } - return res; - } - } -//leetcode submit region end(Prohibit modification and deletion) -} \ No newline at end of file diff --git a/src/com/leetcodeTwo/editor/cn/MergeStringsAlternately.java b/src/com/leetcodeTwo/editor/cn/MergeStringsAlternately.java deleted file mode 100644 index 224a44a..0000000 --- a/src/com/leetcodeTwo/editor/cn/MergeStringsAlternately.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.leetcode.editor.cn; - -//给你两个字符串 word1 和 word2 。请你从 word1 开始,通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长,就将多出来的字母追加到 -//合并后字符串的末尾。 -// -// 返回 合并后的字符串 。 -// -// -// -// 示例 1: -// -// -//输入:word1 = "abc", word2 = "pqr" -//输出:"apbqcr" -//解释:字符串合并情况如下所示: -//word1: a b c -//word2: p q r -//合并后: a p b q c r -// -// -// 示例 2: -// -// -//输入:word1 = "ab", word2 = "pqrs" -//输出:"apbqrs" -//解释:注意,word2 比 word1 长,"rs" 需要追加到合并后字符串的末尾。 -//word1: a b -//word2: p q r s -//合并后: a p b q r s -// -// -// 示例 3: -// -// -//输入:word1 = "abcd", word2 = "pq" -//输出:"apbqcd" -//解释:注意,word1 比 word2 长,"cd" 需要追加到合并后字符串的末尾。 -//word1: a b c d -//word2: p q -//合并后: a p b q c d -// -// -// -// -// 提示: -// -// -// 1 <= word1.length, word2.length <= 100 -// word1 和 word2 由小写英文字母组成 -// -// -// Related Topics 双指针 字符串 👍 54 👎 0 - - -/** - * 1768 交替合并字符串 - * @date 2022-10-23 15:00:28 - * @author shang.liang - */ - public class MergeStringsAlternately{ - public static void main(String[] args){ - Solution soution = new MergeStringsAlternately().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public String mergeAlternately(String word1, String word2) { - - int l1 = word1.length(); - int l2 = word2.length(); - - int i = 0, j = 0; - StringBuilder result = new StringBuilder(); - while (i < l1 || j < l2) { - if (i < l1) { - result.append(word1.charAt(i)); - i++; - } - if (j < l2) { - result.append(word2.charAt(j)); - j++; - } - } - - return result.toString(); - } -} -//leetcode submit region end(Prohibit modification and deletion) - - } diff --git a/src/com/leetcodeTwo/editor/cn/MinimumDifferenceBetweenHighestAndLowestOfKScores.java b/src/com/leetcodeTwo/editor/cn/MinimumDifferenceBetweenHighestAndLowestOfKScores.java deleted file mode 100644 index 2be9e3d..0000000 --- a/src/com/leetcodeTwo/editor/cn/MinimumDifferenceBetweenHighestAndLowestOfKScores.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.leetcode.editor.cn; - -//给你一个 下标从 0 开始 的整数数组 nums ,其中 nums[i] 表示第 i 名学生的分数。另给你一个整数 k 。 -// -// 从数组中选出任意 k 名学生的分数,使这 k 个分数间 最高分 和 最低分 的 差值 达到 最小化 。 -// -// 返回可能的 最小差值 。 -// -// -// -// 示例 1: -// -// 输入:nums = [90], k = 1 -//输出:0 -//解释:选出 1 名学生的分数,仅有 1 种方法: -//- [90] 最高分和最低分之间的差值是 90 - 90 = 0 -//可能的最小差值是 0 -// -// -// 示例 2: -// -// 输入:nums = [9,4,1,7], k = 2 -//输出:2 -//解释:选出 2 名学生的分数,有 6 种方法: -//- [9,4,1,7] 最高分和最低分之间的差值是 9 - 4 = 5 -//- [9,4,1,7] 最高分和最低分之间的差值是 9 - 1 = 8 -//- [9,4,1,7] 最高分和最低分之间的差值是 9 - 7 = 2 -//- [9,4,1,7] 最高分和最低分之间的差值是 4 - 1 = 3 -//- [9,4,1,7] 最高分和最低分之间的差值是 7 - 4 = 3 -//- [9,4,1,7] 最高分和最低分之间的差值是 7 - 1 = 6 -//可能的最小差值是 2 -// -// -// -// 提示: -// -// -// 1 <= k <= nums.length <= 1000 -// 0 <= nums[i] <= 10⁵ -// -// Related Topics 数组 排序 滑动窗口 👍 15 👎 0 - - -import java.util.Arrays; - -/** - * 1984 学生分数的最小差值 - * @date 2022-02-11 08:46:52 - * @author shang.liang - */ - public class MinimumDifferenceBetweenHighestAndLowestOfKScores{ - public static void main(String[] args){ - Solution soution = new MinimumDifferenceBetweenHighestAndLowestOfKScores().new Solution(); - int[] nums = new int[]{9, 4, 1, 7}; - int k = 2; - System.out.println("soution.minimumDifference(nums,k) = " + soution.minimumDifference(nums, k)); - } - - //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public int minimumDifference(int[] nums, int k) { - - int res = Integer.MAX_VALUE; - int index = 0; - - Arrays.sort(nums); - - for (int i = k-1; i < nums.length; i++) { - - int tmp = nums[i] - nums[index++]; - res = Math.min(res, tmp); - - } - - return res; - } -} -//leetcode submit region end(Prohibit modification and deletion) - - } diff --git a/src/com/leetcodeTwo/editor/cn/NAryTreePostorderTraversal.java b/src/com/leetcodeTwo/editor/cn/NAryTreePostorderTraversal.java deleted file mode 100644 index 21f3460..0000000 --- a/src/com/leetcodeTwo/editor/cn/NAryTreePostorderTraversal.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.leetcode.editor.cn; - -//给定一个 n 叉树的根节点 root ,返回 其节点值的 后序遍历 。 -// -// n 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 null 分隔(请参见示例)。 -// -// -// -// 示例 1: -// -// -// -// -//输入:root = [1,null,3,2,4,null,5,6] -//输出:[5,6,3,2,4,1] -// -// -// 示例 2: -// -// -// -// -//输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12, -//null,13,null,null,14] -//输出:[2,6,14,11,7,3,12,8,4,13,9,10,5,1] -// -// -// -// -// 提示: -// -// -// 节点总数在范围 [0, 10⁴] 内 -// 0 <= Node.val <= 10⁴ -// n 叉树的高度小于或等于 1000 -// -// -// -// -// 进阶:递归法很简单,你可以使用迭代法完成此题吗? -// Related Topics 栈 树 深度优先搜索 👍 194 👎 0 - - -import java.util.ArrayList; -import java.util.List; - -/** - * 590 N 叉树的后序遍历 - * - * @author shang.liang - * @date 2022-03-12 09:35:34 - */ -public class NAryTreePostorderTraversal { - public static void main(String[] args) { - Solution soution = new NAryTreePostorderTraversal().new Solution(); - - } - - class Node { - public int val; - public List children; - - public Node() { - } - - public Node(int _val) { - val = _val; - } - - public Node(int _val, List _children) { - val = _val; - children = _children; - } - } - //leetcode submit region begin(Prohibit modification and deletion) - - -// Definition for a Node. - - - class Solution { - public List postorder(Node root) { - - List res = new ArrayList<>(); - - - dfs(root, res); - - - return res; - } - - public void dfs(Node root, List res) { - - if (root == null) { - return; - } - - for (Node child : root.children) { - dfs(child, res); - } - - res.add(root.val); - } - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/NumberOfRectanglesThatCanFormTheLargestSquare.java b/src/com/leetcodeTwo/editor/cn/NumberOfRectanglesThatCanFormTheLargestSquare.java deleted file mode 100644 index 9f2993c..0000000 --- a/src/com/leetcodeTwo/editor/cn/NumberOfRectanglesThatCanFormTheLargestSquare.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.leetcode.editor.cn; - -//给你一个数组 rectangles ,其中 rectangles[i] = [li, wi] 表示第 i 个矩形的长度为 li 、宽度为 wi 。 -// -// 如果存在 k 同时满足 k <= li 和 k <= wi ,就可以将第 i 个矩形切成边长为 k 的正方形。例如,矩形 [4,6] 可以切成边长最大为 -//4 的正方形。 -// -// 设 maxLen 为可以从矩形数组 rectangles 切分得到的 最大正方形 的边长。 -// -// 请你统计有多少个矩形能够切出边长为 maxLen 的正方形,并返回矩形 数目 。 -// -// -// -// 示例 1: -// -// -//输入:rectangles = [[5,8],[3,9],[5,12],[16,5]] -//输出:3 -//解释:能从每个矩形中切出的最大正方形边长分别是 [5,3,5,5] 。 -//最大正方形的边长为 5 ,可以由 3 个矩形切分得到。 -// -// -// 示例 2: -// -// -//输入:rectangles = [[2,3],[3,7],[4,3],[3,7]] -//输出:3 -// -// -// -// -// 提示: -// -// -// 1 <= rectangles.length <= 1000 -// rectangles[i].length == 2 -// 1 <= li, wi <= 10⁹ -// li != wi -// -// Related Topics 数组 👍 47 👎 0 - - -/** - * 1725 可以形成最大正方形的矩形数目 - * @date 2022-02-04 22:08:26 - * @author shang.liang - */ - public class NumberOfRectanglesThatCanFormTheLargestSquare{ - public static void main(String[] args){ - Solution soution = new NumberOfRectanglesThatCanFormTheLargestSquare().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public int countGoodRectangles(int[][] rectangles) { - - int res = 0; - int maxLen = 0; - - for (int[] rectangle : rectangles) { - int currLen = Math.min(rectangle[0], rectangle[1]); - - if (currLen == maxLen) { - res++; - } else if (currLen > maxLen) { - res = 1; - maxLen = currLen; - } - } - - return res; - } -} -//leetcode submit region end(Prohibit modification and deletion) - - } diff --git a/src/com/leetcodeTwo/editor/cn/NumberOfStudentsUnableToEatLunch.java b/src/com/leetcodeTwo/editor/cn/NumberOfStudentsUnableToEatLunch.java deleted file mode 100644 index 737394f..0000000 --- a/src/com/leetcodeTwo/editor/cn/NumberOfStudentsUnableToEatLunch.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.leetcode.editor.cn; - -//学校的自助午餐提供圆形和方形的三明治,分别用数字 0 和 1 表示。所有学生站在一个队列里,每个学生要么喜欢圆形的要么喜欢方形的。 餐厅里三明治的数量与学生 -//的数量相同。所有三明治都放在一个 栈 里,每一轮: -// -// -// 如果队列最前面的学生 喜欢 栈顶的三明治,那么会 拿走它 并离开队列。 -// 否则,这名学生会 放弃这个三明治 并回到队列的尾部。 -// -// -// 这个过程会一直持续到队列里所有学生都不喜欢栈顶的三明治为止。 -// -// 给你两个整数数组 students 和 sandwiches ,其中 sandwiches[i] 是栈里面第 i 个三明治的类型(i = 0 是栈的顶部) -//, students[j] 是初始队列里第 j 名学生对三明治的喜好(j = 0 是队列的最开始位置)。请你返回无法吃午餐的学生数量。 -// -// -// -// 示例 1: -// -// 输入:students = [1,1,0,0], sandwiches = [0,1,0,1] -//输出:0 -//解释: -//- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [1,0,0,1]。 -//- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [0,0,1,1]。 -//- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [0,1,1],三明治栈为 sandwiches = [1,0,1]。 -//- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [1,1,0]。 -//- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [1,0],三明治栈为 sandwiches = [0,1]。 -//- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [0,1]。 -//- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [1],三明治栈为 sandwiches = [1]。 -//- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [],三明治栈为 sandwiches = []。 -//所以所有学生都有三明治吃。 -// -// -// 示例 2: -// -// 输入:students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1] -//输出:3 -// -// -// -// -// 提示: -// -// -// 1 <= students.length, sandwiches.length <= 100 -// students.length == sandwiches.length -// sandwiches[i] 要么是 0 ,要么是 1 。 -// students[i] 要么是 0 ,要么是 1 。 -// -// -// Related Topics 栈 队列 数组 模拟 👍 111 👎 0 - - -import java.util.Arrays; - -/** - * 1700 无法吃午餐的学生数量 - * - * @author shang.liang - * @date 2022-10-19 18:58:24 - */ -public class NumberOfStudentsUnableToEatLunch { - public static void main(String[] args) { - Solution soution = new NumberOfStudentsUnableToEatLunch().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public int countStudents(int[] students, int[] sandwiches) { - - int s1 = Arrays.stream(students).sum(); - int s0 = students.length - s1; - - for (int i = 0; i < sandwiches.length; i++) { - if (sandwiches[i] == 0 && s0 > 0) { - s0--; - } else if (sandwiches[i] == 1 && s1 > 0) { - s1--; - } else { - break; - } - } - return s0 + s1; - - } - } -//leetcode submit region end(Prohibit modification and deletion) - -} diff --git a/src/com/leetcodeTwo/editor/cn/NumberOfSubarraysWithBoundedMaximum.java b/src/com/leetcodeTwo/editor/cn/NumberOfSubarraysWithBoundedMaximum.java deleted file mode 100644 index b47c770..0000000 --- a/src/com/leetcodeTwo/editor/cn/NumberOfSubarraysWithBoundedMaximum.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.leetcode.editor.cn; - -//给你一个整数数组 nums 和两个整数:left 及 right 。找出 nums 中连续、非空且其中最大元素在范围 [left, right] 内的子数组 -//,并返回满足条件的子数组的个数。 -// -// 生成的测试用例保证结果符合 32-bit 整数范围。 -// -// -// -// 示例 1: -// -// -//输入:nums = [2,1,4,3], left = 2, right = 3 -//输出:3 -//解释:满足条件的三个子数组:[2], [2, 1], [3] -// -// -// 示例 2: -// -// -//输入:nums = [2,9,2,5,6], left = 2, right = 8 -//输出:7 -// -// -// -// -// 提示: -// -// -// 1 <= nums.length <= 10⁵ -// 0 <= nums[i] <= 10⁹ -// 0 <= left <= right <= 10⁹ -// -// -// Related Topics 数组 双指针 👍 265 👎 0 - - -/** - * 795 区间子数组个数 - * @date 2022-11-24 13:25:36 - * @author shang.liang - */ - public class NumberOfSubarraysWithBoundedMaximum{ - public static void main(String[] args){ - Solution soution = new NumberOfSubarraysWithBoundedMaximum().new Solution(); - - } - - //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public int numSubarrayBoundedMax(int[] nums, int left, int right) { - int res = 0, last2 = -1, last1 = -1; - for (int i = 0; i < nums.length; i++) { - if (nums[i] >= left && nums[i] <= right) { - last1 = i; - } else if (nums[i] > right) { - last2 = i; - last1 = -1; - } - if (last1 != -1) { - res += last1 - last2; - } - } - return res; - } -} -//leetcode submit region end(Prohibit modification and deletion) - - }