diff --git a/src/com/leetcode_new/editor/cn/AddDigits.java b/src/com/leetcode_new/editor/cn/AddDigits.java new file mode 100644 index 0000000..18956c4 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/AddDigits.java @@ -0,0 +1,69 @@ +package com.leetcode_new.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_new/editor/cn/AddTwoNumbers.java b/src/com/leetcode_new/editor/cn/AddTwoNumbers.java new file mode 100644 index 0000000..4ecf2e1 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/AddTwoNumbers.java @@ -0,0 +1,110 @@ +package com.leetcode_new.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/leetcode_new/editor/cn/ArrayOfDoubledPairs.java b/src/com/leetcode_new/editor/cn/ArrayOfDoubledPairs.java new file mode 100644 index 0000000..ce294ad --- /dev/null +++ b/src/com/leetcode_new/editor/cn/ArrayOfDoubledPairs.java @@ -0,0 +1,90 @@ +package com.leetcode_new.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_new/editor/cn/BaseballGame.java b/src/com/leetcode_new/editor/cn/BaseballGame.java new file mode 100644 index 0000000..96cd13b --- /dev/null +++ b/src/com/leetcode_new/editor/cn/BaseballGame.java @@ -0,0 +1,118 @@ +package com.leetcode_new.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_new/editor/cn/BattleshipsInABoard.java b/src/com/leetcode_new/editor/cn/BattleshipsInABoard.java new file mode 100644 index 0000000..a54940d --- /dev/null +++ b/src/com/leetcode_new/editor/cn/BattleshipsInABoard.java @@ -0,0 +1,82 @@ +package com.leetcode_new.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/leetcode_new/editor/cn/BinaryNumberWithAlternatingBits.java b/src/com/leetcode_new/editor/cn/BinaryNumberWithAlternatingBits.java new file mode 100644 index 0000000..98de0d2 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/BinaryNumberWithAlternatingBits.java @@ -0,0 +1,61 @@ +package com.leetcode_new.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_new/editor/cn/ComplexNumberMultiplication.java b/src/com/leetcode_new/editor/cn/ComplexNumberMultiplication.java new file mode 100644 index 0000000..f3a6b1f --- /dev/null +++ b/src/com/leetcode_new/editor/cn/ComplexNumberMultiplication.java @@ -0,0 +1,74 @@ +package com.leetcode_new.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/leetcode_new/editor/cn/ContainsDuplicateIi.java b/src/com/leetcode_new/editor/cn/ContainsDuplicateIi.java new file mode 100644 index 0000000..1448051 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/ContainsDuplicateIi.java @@ -0,0 +1,72 @@ +package com.leetcode_new.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/leetcode_new/editor/cn/CountNumberOfHomogenousSubstrings.java b/src/com/leetcode_new/editor/cn/CountNumberOfHomogenousSubstrings.java new file mode 100644 index 0000000..c94e51b --- /dev/null +++ b/src/com/leetcode_new/editor/cn/CountNumberOfHomogenousSubstrings.java @@ -0,0 +1,85 @@ +package com.leetcode_new.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/leetcode_new/editor/cn/CountNumberOfPairsWithAbsoluteDifferenceK.java b/src/com/leetcode_new/editor/cn/CountNumberOfPairsWithAbsoluteDifferenceK.java new file mode 100644 index 0000000..3eb0409 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/CountNumberOfPairsWithAbsoluteDifferenceK.java @@ -0,0 +1,89 @@ +package com.leetcode_new.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_new/editor/cn/CountVowelsPermutation.java b/src/com/leetcode_new/editor/cn/CountVowelsPermutation.java new file mode 100644 index 0000000..85f4300 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/CountVowelsPermutation.java @@ -0,0 +1,104 @@ +package com.leetcode_new.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/leetcode/editor/cn/CountingWordsWithAGivenPrefix.java b/src/com/leetcode_new/editor/cn/CountingWordsWithAGivenPrefix.java similarity index 97% rename from src/com/leetcode/editor/cn/CountingWordsWithAGivenPrefix.java rename to src/com/leetcode_new/editor/cn/CountingWordsWithAGivenPrefix.java index 9135b58..0106602 100644 --- a/src/com/leetcode/editor/cn/CountingWordsWithAGivenPrefix.java +++ b/src/com/leetcode_new/editor/cn/CountingWordsWithAGivenPrefix.java @@ -1,4 +1,4 @@ -package com.leetcode.editor.cn; +package com.leetcode_new.editor.cn; //给你一个字符串数组 words 和一个字符串 pref 。 // diff --git a/src/com/leetcode_new/editor/cn/CustomSortString.java b/src/com/leetcode_new/editor/cn/CustomSortString.java new file mode 100644 index 0000000..079e06a --- /dev/null +++ b/src/com/leetcode_new/editor/cn/CustomSortString.java @@ -0,0 +1,77 @@ +package com.leetcode_new.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/leetcode_new/editor/cn/EvenOddTree.java b/src/com/leetcode_new/editor/cn/EvenOddTree.java new file mode 100644 index 0000000..9286795 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/EvenOddTree.java @@ -0,0 +1,161 @@ +package com.leetcode_new.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/leetcode_new/editor/cn/FactorialTrailingZeroes.java b/src/com/leetcode_new/editor/cn/FactorialTrailingZeroes.java new file mode 100644 index 0000000..32c5baf --- /dev/null +++ b/src/com/leetcode_new/editor/cn/FactorialTrailingZeroes.java @@ -0,0 +1,74 @@ +package com.leetcode_new.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_new/editor/cn/FindCenterOfStarGraph.java b/src/com/leetcode_new/editor/cn/FindCenterOfStarGraph.java new file mode 100644 index 0000000..b42cd97 --- /dev/null +++ b/src/com/leetcode_new/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_new/editor/cn/FindKPairsWithSmallestSums.java b/src/com/leetcode_new/editor/cn/FindKPairsWithSmallestSums.java new file mode 100644 index 0000000..1e2d652 --- /dev/null +++ b/src/com/leetcode_new/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) + +} diff --git a/src/com/leetcode_new/editor/cn/FindMissingObservations.java b/src/com/leetcode_new/editor/cn/FindMissingObservations.java new file mode 100644 index 0000000..44537cc --- /dev/null +++ b/src/com/leetcode_new/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_new/editor/cn/FindTheClosestPalindrome.java b/src/com/leetcode_new/editor/cn/FindTheClosestPalindrome.java new file mode 100644 index 0000000..76f6653 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/FindTheClosestPalindrome.java @@ -0,0 +1,58 @@ +package com.leetcode_new.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_new/editor/cn/FindTheHighestAltitude.java b/src/com/leetcode_new/editor/cn/FindTheHighestAltitude.java new file mode 100644 index 0000000..acf4853 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/FindTheHighestAltitude.java @@ -0,0 +1,67 @@ +package com.leetcode_new.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/leetcode_new/editor/cn/FriendsOfAppropriateAges.java b/src/com/leetcode_new/editor/cn/FriendsOfAppropriateAges.java new file mode 100644 index 0000000..a407e53 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/FriendsOfAppropriateAges.java @@ -0,0 +1,120 @@ +package com.leetcode_new.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/leetcode_new/editor/cn/IncreasingTripletSubsequence.java b/src/com/leetcode_new/editor/cn/IncreasingTripletSubsequence.java new file mode 100644 index 0000000..2ce3ee0 --- /dev/null +++ b/src/com/leetcode_new/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) + +} diff --git a/src/com/leetcode_new/editor/cn/KnightProbabilityInChessboard.java b/src/com/leetcode_new/editor/cn/KnightProbabilityInChessboard.java new file mode 100644 index 0000000..5ad3d27 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/KnightProbabilityInChessboard.java @@ -0,0 +1,95 @@ +package com.leetcode_new.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/leetcode_new/editor/cn/LargestNumberAtLeastTwiceOfOthers.java b/src/com/leetcode_new/editor/cn/LargestNumberAtLeastTwiceOfOthers.java new file mode 100644 index 0000000..81d3111 --- /dev/null +++ b/src/com/leetcode_new/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) + +} diff --git a/src/com/leetcode_new/editor/cn/LetterCasePermutation.java b/src/com/leetcode_new/editor/cn/LetterCasePermutation.java new file mode 100644 index 0000000..47e7543 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/LetterCasePermutation.java @@ -0,0 +1,76 @@ +package com.leetcode_new.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/leetcode_new/editor/cn/LinkedListRandomNode.java b/src/com/leetcode_new/editor/cn/LinkedListRandomNode.java new file mode 100644 index 0000000..2d40b7d --- /dev/null +++ b/src/com/leetcode_new/editor/cn/LinkedListRandomNode.java @@ -0,0 +1,115 @@ +package com.leetcode_new.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/leetcode_new/editor/cn/LongestHappyString.java b/src/com/leetcode_new/editor/cn/LongestHappyString.java new file mode 100644 index 0000000..afdc4ce --- /dev/null +++ b/src/com/leetcode_new/editor/cn/LongestHappyString.java @@ -0,0 +1,89 @@ +package com.leetcode_new.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_new/editor/cn/LuckyNumbersInAMatrix.java b/src/com/leetcode_new/editor/cn/LuckyNumbersInAMatrix.java new file mode 100644 index 0000000..4dbea85 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/LuckyNumbersInAMatrix.java @@ -0,0 +1,94 @@ +package com.leetcode_new.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_new/editor/cn/MaximumDifferenceBetweenIncreasingElements.java b/src/com/leetcode_new/editor/cn/MaximumDifferenceBetweenIncreasingElements.java new file mode 100644 index 0000000..0585e36 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/MaximumDifferenceBetweenIncreasingElements.java @@ -0,0 +1,83 @@ +package com.leetcode_new.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_new/editor/cn/MaximumNumberOfEatenApples.java b/src/com/leetcode_new/editor/cn/MaximumNumberOfEatenApples.java new file mode 100644 index 0000000..be20308 --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/MergeStringsAlternately.java b/src/com/leetcode_new/editor/cn/MergeStringsAlternately.java new file mode 100644 index 0000000..224a44a --- /dev/null +++ b/src/com/leetcode_new/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) + + } diff --git a/src/com/leetcode_new/editor/cn/MinimumDifferenceBetweenHighestAndLowestOfKScores.java b/src/com/leetcode_new/editor/cn/MinimumDifferenceBetweenHighestAndLowestOfKScores.java new file mode 100644 index 0000000..2be9e3d --- /dev/null +++ b/src/com/leetcode_new/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_new/editor/cn/NAryTreePostorderTraversal.java b/src/com/leetcode_new/editor/cn/NAryTreePostorderTraversal.java new file mode 100644 index 0000000..21f3460 --- /dev/null +++ b/src/com/leetcode_new/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_new/editor/cn/NumberOfRectanglesThatCanFormTheLargestSquare.java b/src/com/leetcode_new/editor/cn/NumberOfRectanglesThatCanFormTheLargestSquare.java new file mode 100644 index 0000000..6ec5e10 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/NumberOfRectanglesThatCanFormTheLargestSquare.java @@ -0,0 +1,77 @@ +package com.leetcode_new.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_new/editor/cn/NumberOfStudentsUnableToEatLunch.java b/src/com/leetcode_new/editor/cn/NumberOfStudentsUnableToEatLunch.java new file mode 100644 index 0000000..5b30791 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/NumberOfStudentsUnableToEatLunch.java @@ -0,0 +1,90 @@ +package com.leetcode_new.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/leetcode_new/editor/cn/NumberOfSubarraysWithBoundedMaximum.java b/src/com/leetcode_new/editor/cn/NumberOfSubarraysWithBoundedMaximum.java new file mode 100644 index 0000000..b47c770 --- /dev/null +++ b/src/com/leetcode_new/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) + + } diff --git a/src/com/leetcode_new/editor/cn/NumberOfWaysToReconstructATree.java b/src/com/leetcode_new/editor/cn/NumberOfWaysToReconstructATree.java new file mode 100644 index 0000000..4df02e7 --- /dev/null +++ b/src/com/leetcode_new/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_new/editor/cn/OccurrencesAfterBigram.java b/src/com/leetcode_new/editor/cn/OccurrencesAfterBigram.java new file mode 100644 index 0000000..1a2697a --- /dev/null +++ b/src/com/leetcode_new/editor/cn/OccurrencesAfterBigram.java @@ -0,0 +1,74 @@ +package com.leetcode.editor.cn; + +import java.util.Arrays; + +; + +//给出第一个词 first 和第二个词 second,考虑在某些文本 text 中可能以 "first second third" 形式出现的情况,其中 se +//cond 紧随 first 出现,third 紧随 second 出现。 +// +// 对于每种这样的情况,将第三个词 "third" 添加到答案中,并返回答案。 +// +// +// +// 示例 1: +// +// +//输入:text = "alice is a good girl she is a good student", first = "a", second = +//"good" +//输出:["girl","student"] +// +// +// 示例 2: +// +// +//输入:text = "we will we will rock you", first = "we", second = "will" +//输出:["we","rock"] +// +// +// +// +// 提示: +// +// +// 1 <= text.length <= 1000 +// text 由小写英文字母和空格组成 +// text 中的所有单词之间都由 单个空格字符 分隔 +// 1 <= first.length, second.length <= 10 +// first 和 second 由小写英文字母组成 +// +// Related Topics 字符串 +// 👍 42 👎 0 + + +/** + * 1078 Bigram 分词 + * @date 2021-12-26 10:50:08 + * @author shang.liang + */ +public class OccurrencesAfterBigram{ + public static void main(String[] args) { + Solution solution = new OccurrencesAfterBigram().new Solution(); + + } + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public String[] findOcurrences(String text, String first, String second) { + + String[] texts = text.split(" "); + String[] res = new String[texts.length]; + int index = 0; + + for (int i = 2; i < texts.length; i++) { + if (texts[i - 2].equals(first) && texts[i - 1].equals(second)) { + res[index++] = texts[i]; + } + } + + return Arrays.copyOfRange(res, 0, index); + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/com/leetcode_new/editor/cn/OptimalDivision.java b/src/com/leetcode_new/editor/cn/OptimalDivision.java new file mode 100644 index 0000000..806040c --- /dev/null +++ b/src/com/leetcode_new/editor/cn/OptimalDivision.java @@ -0,0 +1,76 @@ +package com.leetcode_new.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_new/editor/cn/PancakeSorting.java b/src/com/leetcode_new/editor/cn/PancakeSorting.java new file mode 100644 index 0000000..78cfda8 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/PancakeSorting.java @@ -0,0 +1,116 @@ +package com.leetcode_new.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_new/editor/cn/ParsingABooleanExpression.java b/src/com/leetcode_new/editor/cn/ParsingABooleanExpression.java new file mode 100644 index 0000000..243e35a --- /dev/null +++ b/src/com/leetcode_new/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) + +} diff --git a/src/com/leetcode_new/editor/cn/PartitionArrayIntoDisjointIntervals.java b/src/com/leetcode_new/editor/cn/PartitionArrayIntoDisjointIntervals.java new file mode 100644 index 0000000..81740fa --- /dev/null +++ b/src/com/leetcode_new/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) + +} diff --git a/src/com/leetcode_new/editor/cn/PerfectNumber.java b/src/com/leetcode_new/editor/cn/PerfectNumber.java new file mode 100644 index 0000000..5a1bd26 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/PerfectNumber.java @@ -0,0 +1,89 @@ +package com.leetcode_new.editor.cn;; + +//对于一个 正整数,如果它和除了它自身以外的所有 正因子 之和相等,我们称它为 「完美数」。 +// +// 给定一个 整数 n, 如果是完美数,返回 true,否则返回 false +// +// +// +// 示例 1: +// +// +//输入:num = 28 +//输出:true +//解释:28 = 1 + 2 + 4 + 7 + 14 +//1, 2, 4, 7, 和 14 是 28 的所有正因子。 +// +// 示例 2: +// +// +//输入:num = 6 +//输出:true +// +// +// 示例 3: +// +// +//输入:num = 496 +//输出:true +// +// +// 示例 4: +// +// +//输入:num = 8128 +//输出:true +// +// +// 示例 5: +// +// +//输入:num = 2 +//输出:false +// +// +// +// +// 提示: +// +// +// 1 <= num <= 108 +// +// Related Topics 数学 +// 👍 131 👎 0 + + +/** + * 507 完美数 + * @date 2021-12-31 10:52:30 + * @author shang.liang + */ +public class PerfectNumber{ + public static void main(String[] args) { + Solution solution = new PerfectNumber().new Solution(); + + } + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public boolean checkPerfectNumber(int num) { + + if (num == 1) { + return false; + } + + int sum = 1; + + for (int i = 2; i < Math.sqrt(num); i++) { + if (num % i == 0) { + sum += num / i + i; + } + } + + return sum == num; + } + +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/com/leetcode_new/editor/cn/PlusOne.java b/src/com/leetcode_new/editor/cn/PlusOne.java new file mode 100644 index 0000000..4eb143a --- /dev/null +++ b/src/com/leetcode_new/editor/cn/PlusOne.java @@ -0,0 +1,78 @@ +package com.leetcode_new.editor.cn;; + +//给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。 +// +// 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。 +// +// 你可以假设除了整数 0 之外,这个整数不会以零开头。 +// +// +// +// 示例 1: +// +// +//输入:digits = [1,2,3] +//输出:[1,2,4] +//解释:输入数组表示数字 123。 +// +// +// 示例 2: +// +// +//输入:digits = [4,3,2,1] +//输出:[4,3,2,2] +//解释:输入数组表示数字 4321。 +// +// +// 示例 3: +// +// +//输入:digits = [0] +//输出:[1] +// +// +// +// +// 提示: +// +// +// 1 <= digits.length <= 100 +// 0 <= digits[i] <= 9 +// +// Related Topics 数组 数学 +// 👍 866 👎 0 + +/** + * 66 加一 + * @date 2021-11-30 11:02:36 + * @author shang.liang + */ +public class PlusOne{ + public static void main(String[] args) { + Solution solution = new PlusOne().new Solution(); + + } + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int[] plusOne(int[] digits) { + int n = digits.length; + for (int i = n - 1; i >= 0; --i) { + if (digits[i] != 9) { + ++digits[i]; + for (int j = i + 1; j < n; ++j) { + digits[j] = 0; + } + return digits; + } + } + + // digits 中所有的元素均为 9 + int[] ans = new int[n + 1]; + ans[0] = 1; + return ans; + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/com/leetcode_new/editor/cn/PushDominoes.java b/src/com/leetcode_new/editor/cn/PushDominoes.java new file mode 100644 index 0000000..46a2fd8 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/PushDominoes.java @@ -0,0 +1,111 @@ +package com.leetcode_new.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_new/editor/cn/ReachableNodesInSubdividedGraph.java b/src/com/leetcode_new/editor/cn/ReachableNodesInSubdividedGraph.java new file mode 100644 index 0000000..cd592ad --- /dev/null +++ b/src/com/leetcode_new/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) + + } diff --git a/src/com/leetcode_new/editor/cn/ReverseOnlyLetters.java b/src/com/leetcode_new/editor/cn/ReverseOnlyLetters.java new file mode 100644 index 0000000..c1fc298 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/ReverseOnlyLetters.java @@ -0,0 +1,103 @@ +package com.leetcode_new.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_new/editor/cn/ShortestSubarrayWithSumAtLeastK.java b/src/com/leetcode_new/editor/cn/ShortestSubarrayWithSumAtLeastK.java new file mode 100644 index 0000000..db919a5 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/ShortestSubarrayWithSumAtLeastK.java @@ -0,0 +1,84 @@ +/* +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) + +} +*/ diff --git a/src/com/leetcode_new/editor/cn/SimplifiedFractions.java b/src/com/leetcode_new/editor/cn/SimplifiedFractions.java new file mode 100644 index 0000000..3c43406 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/SimplifiedFractions.java @@ -0,0 +1,80 @@ +package com.leetcode_new.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_new/editor/cn/SingleElementInASortedArray.java b/src/com/leetcode_new/editor/cn/SingleElementInASortedArray.java new file mode 100644 index 0000000..1f96fd4 --- /dev/null +++ b/src/com/leetcode_new/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_new/editor/cn/StoneGameIx.java b/src/com/leetcode_new/editor/cn/StoneGameIx.java new file mode 100644 index 0000000..da97db6 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/StoneGameIx.java @@ -0,0 +1,92 @@ +package com.leetcode_new.editor.cn; + +//Alice 和 Bob 再次设计了一款新的石子游戏。现有一行 n 个石子,每个石子都有一个关联的数字表示它的价值。给你一个整数数组 stones ,其中 +//stones[i] 是第 i 个石子的价值。 +// +// Alice 和 Bob 轮流进行自己的回合,Alice 先手。每一回合,玩家需要从 stones 中移除任一石子。 +// +// +// 如果玩家移除石子后,导致 所有已移除石子 的价值 总和 可以被 3 整除,那么该玩家就 输掉游戏 。 +// 如果不满足上一条,且移除后没有任何剩余的石子,那么 Bob 将会直接获胜(即便是在 Alice 的回合)。 +// +// +// 假设两位玩家均采用 最佳 决策。如果 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 获胜。 +// +// +// +// +// 提示: +// +// +// 1 <= stones.length <= 10⁵ +// 1 <= stones[i] <= 10⁴ +// +// Related Topics 贪心 数组 数学 计数 博弈 👍 70 👎 0 + + +/** + * 2029 石子游戏 IX + * @date 2022-01-20 13:21:27 + * @author shang.liang + */ + public class StoneGameIx{ + public static void main(String[] args){ + Solution soution = new StoneGameIx().new Solution(); + + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public boolean stoneGameIX(int[] stones) { + + int[] nums = new int[3]; + + for (int stone : stones) { + nums[stone % 3]++; + } + + if (nums[0] % 2 == 0) { + return nums[1] >= 1 && nums[2] >= 1; + } + + return Math.abs(nums[1] - nums[2]) > 2; + } +} +//leetcode submit region end(Prohibit modification and deletion) + + } diff --git a/src/com/leetcode_new/editor/cn/TwoSumIvInputIsABst.java b/src/com/leetcode_new/editor/cn/TwoSumIvInputIsABst.java new file mode 100644 index 0000000..7d26370 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/TwoSumIvInputIsABst.java @@ -0,0 +1,101 @@ +package com.leetcode_new.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_new/editor/cn/ZigzagConversion.java b/src/com/leetcode_new/editor/cn/ZigzagConversion.java new file mode 100644 index 0000000..131fc4f --- /dev/null +++ b/src/com/leetcode_new/editor/cn/ZigzagConversion.java @@ -0,0 +1,119 @@ +package com.leetcode_new.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_new/editor/cn/doc/content/AddDigits.md b/src/com/leetcode_new/editor/cn/doc/content/AddDigits.md new file mode 100644 index 0000000..f22dca7 --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/AddTwoNumbers.md b/src/com/leetcode_new/editor/cn/doc/content/AddTwoNumbers.md new file mode 100644 index 0000000..4842831 --- /dev/null +++ b/src/com/leetcode_new/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]
    +
    + +

     

    + +

    提示:

    + +
      +
    • 每个链表中的节点数在范围 [1, 100]
    • +
    • 0 <= Node.val <= 9
    • +
    • 题目数据保证列表表示的数字不含前导零
    • +
    +
    Related Topics
  • 递归
  • 链表
  • 数学

  • 👍 7310
  • 👎 0
  • \ No newline at end of file diff --git a/src/com/leetcode_new/editor/cn/doc/content/ArrayOfDoubledPairs.md b/src/com/leetcode_new/editor/cn/doc/content/ArrayOfDoubledPairs.md new file mode 100644 index 0000000..a55ca86 --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/BaseballGame.md b/src/com/leetcode_new/editor/cn/doc/content/BaseballGame.md new file mode 100644 index 0000000..e98f29f --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/BinaryNumberWithAlternatingBits.md b/src/com/leetcode_new/editor/cn/doc/content/BinaryNumberWithAlternatingBits.md new file mode 100644 index 0000000..dcaea8f --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/ComplexNumberMultiplication.md b/src/com/leetcode_new/editor/cn/doc/content/ComplexNumberMultiplication.md new file mode 100644 index 0000000..7eb58d3 --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/ContainsDuplicateIi.md b/src/com/leetcode_new/editor/cn/doc/content/ContainsDuplicateIi.md new file mode 100644 index 0000000..d38407e --- /dev/null +++ b/src/com/leetcode_new/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
    + +

     

    + +

     

    + +

    提示:

    + +
      +
    • 1 <= nums.length <= 105
    • +
    • -109 <= nums[i] <= 109
    • +
    • 0 <= k <= 105
    • +
    +
    Related Topics
  • 数组
  • 哈希表
  • 滑动窗口

  • 👍 420
  • 👎 0
  • \ No newline at end of file diff --git a/src/com/leetcode_new/editor/cn/doc/content/CountNumberOfPairsWithAbsoluteDifferenceK.md b/src/com/leetcode_new/editor/cn/doc/content/CountNumberOfPairsWithAbsoluteDifferenceK.md new file mode 100644 index 0000000..5cbcb02 --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/CountVowelsPermutation.md b/src/com/leetcode_new/editor/cn/doc/content/CountVowelsPermutation.md new file mode 100644 index 0000000..c014c30 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/doc/content/CountVowelsPermutation.md @@ -0,0 +1,42 @@ +

    给你一个整数 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
  • \ No newline at end of file diff --git a/src/com/leetcode_new/editor/cn/doc/content/FactorialTrailingZeroes.md b/src/com/leetcode_new/editor/cn/doc/content/FactorialTrailingZeroes.md new file mode 100644 index 0000000..621ff4b --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/FindCenterOfStarGraph.md b/src/com/leetcode_new/editor/cn/doc/content/FindCenterOfStarGraph.md new file mode 100644 index 0000000..8df8558 --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/FindKPairsWithSmallestSums.md b/src/com/leetcode_new/editor/cn/doc/content/FindKPairsWithSmallestSums.md new file mode 100644 index 0000000..20ac5f5 --- /dev/null +++ b/src/com/leetcode_new/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]
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= nums1.length, nums2.length <= 104
    • +
    • -109 <= nums1[i], nums2[i] <= 109
    • +
    • nums1, nums2 均为升序排列
    • +
    • 1 <= k <= 1000
    • +
    +
    Related Topics
  • 数组
  • 堆(优先队列)

  • 👍 268
  • 👎 0
  • \ No newline at end of file diff --git a/src/com/leetcode_new/editor/cn/doc/content/FindMissingObservations.md b/src/com/leetcode_new/editor/cn/doc/content/FindMissingObservations.md new file mode 100644 index 0000000..5bd1e1e --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/FindTheClosestPalindrome.md b/src/com/leetcode_new/editor/cn/doc/content/FindTheClosestPalindrome.md new file mode 100644 index 0000000..7428035 --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/IncreasingTripletSubsequence.md b/src/com/leetcode_new/editor/cn/doc/content/IncreasingTripletSubsequence.md new file mode 100644 index 0000000..c281143 --- /dev/null +++ b/src/com/leetcode_new/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
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= nums.length <= 5 * 105
    • +
    • -231 <= nums[i] <= 231 - 1
    • +
    + +

     

    + +

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

    +
    Related Topics
  • 贪心
  • 数组

  • 👍 489
  • 👎 0
  • \ No newline at end of file diff --git a/src/com/leetcode_new/editor/cn/doc/content/KnightProbabilityInChessboard.md b/src/com/leetcode_new/editor/cn/doc/content/KnightProbabilityInChessboard.md new file mode 100644 index 0000000..20164dc --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/LargestNumberAtLeastTwiceOfOthers.md b/src/com/leetcode_new/editor/cn/doc/content/LargestNumberAtLeastTwiceOfOthers.md new file mode 100644 index 0000000..7a75454 --- /dev/null +++ b/src/com/leetcode_new/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 至少是其他数字的两倍。
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= nums.length <= 50
    • +
    • 0 <= nums[i] <= 100
    • +
    • nums 中的最大元素是唯一的
    • +
    +
    Related Topics
  • 数组
  • 排序

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

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

    + +

    实现 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, 104]
    • +
    • -104 <= Node.val <= 104
    • +
    • 至多调用 getRandom 方法 104
    • +
    + +

     

    + +

    进阶:

    + +
      +
    • 如果链表非常大且长度未知,该怎么处理?
    • +
    • 你能否在不使用额外空间的情况下解决此问题?
    • +
    +
    Related Topics
  • 水塘抽样
  • 链表
  • 数学
  • 随机化

  • 👍 231
  • 👎 0
  • \ No newline at end of file diff --git a/src/com/leetcode_new/editor/cn/doc/content/LongestHappyString.md b/src/com/leetcode_new/editor/cn/doc/content/LongestHappyString.md new file mode 100644 index 0000000..2728ad5 --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/LuckyNumbersInAMatrix.md b/src/com/leetcode_new/editor/cn/doc/content/LuckyNumbersInAMatrix.md new file mode 100644 index 0000000..e11aac5 --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/MaximumDifferenceBetweenIncreasingElements.md b/src/com/leetcode_new/editor/cn/doc/content/MaximumDifferenceBetweenIncreasingElements.md new file mode 100644 index 0000000..df1dfcf --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/MinimumDifferenceBetweenHighestAndLowestOfKScores.md b/src/com/leetcode_new/editor/cn/doc/content/MinimumDifferenceBetweenHighestAndLowestOfKScores.md new file mode 100644 index 0000000..ee9f944 --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/NAryTreePostorderTraversal.md b/src/com/leetcode_new/editor/cn/doc/content/NAryTreePostorderTraversal.md new file mode 100644 index 0000000..c1f5cf9 --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/NumberOfRectanglesThatCanFormTheLargestSquare.md b/src/com/leetcode_new/editor/cn/doc/content/NumberOfRectanglesThatCanFormTheLargestSquare.md new file mode 100644 index 0000000..0bf230c --- /dev/null +++ b/src/com/leetcode_new/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
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= rectangles.length <= 1000
    • +
    • rectangles[i].length == 2
    • +
    • 1 <= li, wi <= 109
    • +
    • li != wi
    • +
    +
    Related Topics
  • 数组

  • 👍 47
  • 👎 0
  • \ No newline at end of file diff --git a/src/com/leetcode_new/editor/cn/doc/content/NumberOfWaysToReconstructATree.md b/src/com/leetcode_new/editor/cn/doc/content/NumberOfWaysToReconstructATree.md new file mode 100644 index 0000000..5b40c40 --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/OptimalDivision.md b/src/com/leetcode_new/editor/cn/doc/content/OptimalDivision.md new file mode 100644 index 0000000..2532c0c --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/PancakeSorting.md b/src/com/leetcode_new/editor/cn/doc/content/PancakeSorting.md new file mode 100644 index 0000000..d2b6b31 --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/PushDominoes.md b/src/com/leetcode_new/editor/cn/doc/content/PushDominoes.md new file mode 100644 index 0000000..0ca4c77 --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/ReverseOnlyLetters.md b/src/com/leetcode_new/editor/cn/doc/content/ReverseOnlyLetters.md new file mode 100644 index 0000000..746480b --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/SimplifiedFractions.md b/src/com/leetcode_new/editor/cn/doc/content/SimplifiedFractions.md new file mode 100644 index 0000000..1c0a9b3 --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/SingleElementInASortedArray.md b/src/com/leetcode_new/editor/cn/doc/content/SingleElementInASortedArray.md new file mode 100644 index 0000000..bbadc43 --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/StockPriceFluctuation.md b/src/com/leetcode_new/editor/cn/doc/content/StockPriceFluctuation.md new file mode 100644 index 0000000..a087980 --- /dev/null +++ b/src/com/leetcode_new/editor/cn/doc/content/StockPriceFluctuation.md @@ -0,0 +1,56 @@ +

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

    + +

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

    + +

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

    + +
      +
    • 更新 股票在某一时间戳的股票价格,如果有之前同一时间戳的价格,这一操作将 更正 之前的错误价格。
    • +
    • 找到当前记录里 最新股票价格 。最新股票价格 定义为时间戳最晚的股票价格。
    • +
    • 找到当前记录里股票的 最高价格 。
    • +
    • 找到当前记录里股票的 最低价格 。
    • +
    + +

    请你实现 StockPrice 类:

    + +
      +
    • StockPrice() 初始化对象,当前无股票价格记录。
    • +
    • void update(int timestamp, int price) 在时间点 timestamp 更新股票价格为 price 。
    • +
    • int current() 返回股票 最新价格 。
    • +
    • int maximum() 返回股票 最高价格 。
    • +
    • int minimum() 返回股票 最低价格 。
    • +
    + +

     

    + +

    示例 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 。
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= timestamp, price <= 109
    • +
    • updatecurrentmaximum 和 minimum  调用次数不超过 105 。
    • +
    • currentmaximum 和 minimum 被调用时,update 操作 至少 已经被调用过 一次 。
    • +
    +
    Related Topics
  • 设计
  • 哈希表
  • 数据流
  • 有序集合
  • 堆(优先队列)

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

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

    + +

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

    + +
      +
    • 如果玩家移除石子后,导致 所有已移除石子 的价值 总和 可以被 3 整除,那么该玩家就 输掉游戏
    • +
    • 如果不满足上一条,且移除后没有任何剩余的石子,那么 Bob 将会直接获胜(即便是在 Alice 的回合)。
    • +
    + +

    假设两位玩家均采用 最佳 决策。如果 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 获胜。
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= stones.length <= 105
    • +
    • 1 <= stones[i] <= 104
    • +
    +
    Related Topics
  • 贪心
  • 数组
  • 数学
  • 计数
  • 博弈

  • 👍 70
  • 👎 0
  • \ No newline at end of file diff --git a/src/com/leetcode_new/editor/cn/doc/content/TheNumberOfWeakCharactersInTheGame.md b/src/com/leetcode_new/editor/cn/doc/content/TheNumberOfWeakCharactersInTheGame.md new file mode 100644 index 0000000..ab47ec1 --- /dev/null +++ b/src/com/leetcode_new/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
    +解释:第三个角色是弱角色,因为第二个角色的攻击和防御严格大于该角色。
    +
    + +

     

    + +

    提示:

    + +
      +
    • 2 <= properties.length <= 105
    • +
    • properties[i].length == 2
    • +
    • 1 <= attacki, defensei <= 105
    • +
    +
    Related Topics
  • 贪心
  • 数组
  • 排序
  • 单调栈

  • 👍 108
  • 👎 0
  • \ No newline at end of file diff --git a/src/com/leetcode_new/editor/cn/doc/content/TwoSumIvInputIsABst.md b/src/com/leetcode_new/editor/cn/doc/content/TwoSumIvInputIsABst.md new file mode 100644 index 0000000..a47d0fd --- /dev/null +++ b/src/com/leetcode_new/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 diff --git a/src/com/leetcode_new/editor/cn/doc/content/ZigzagConversion.md b/src/com/leetcode_new/editor/cn/doc/content/ZigzagConversion.md new file mode 100644 index 0000000..8955775 --- /dev/null +++ b/src/com/leetcode_new/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