diff --git a/src/main/java/leetcode/editor/cn/ContainsDuplicate.java b/src/main/java/leetcode/editor/cn/ContainsDuplicate.java index bef9ce7..654e9e1 100644 --- a/src/main/java/leetcode/editor/cn/ContainsDuplicate.java +++ b/src/main/java/leetcode/editor/cn/ContainsDuplicate.java @@ -26,29 +26,41 @@ package leetcode.editor.cn; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.Set; //217:存在重复元素 -class ContainsDuplicate{ +class ContainsDuplicate { public static void main(String[] args) { //测试代码 Solution solution = new ContainsDuplicate().new Solution(); } + //力扣代码 //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public boolean containsDuplicate(int[] nums) { - Set set = new HashSet<>(); - for (int num : nums) { - if (set.contains(num)) { - return true; + class Solution { + // public boolean containsDuplicate(int[] nums) { +// Set set = new HashSet<>(); +// for (int num : nums) { +// if (set.contains(num)) { +// return true; +// } +// set.add(num); +// } +// return false; +// } + public boolean containsDuplicate(int[] nums) { + Arrays.sort(nums); + for (int i = 1; i < nums.length; i++) { + if(nums[i]==nums[i-1]){ + return true; + } } - set.add(num); + return false; } - return false; } -} //leetcode submit region end(Prohibit modification and deletion) } \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/DivideTwoIntegers.java b/src/main/java/leetcode/editor/cn/DivideTwoIntegers.java new file mode 100644 index 0000000..0aba82a --- /dev/null +++ b/src/main/java/leetcode/editor/cn/DivideTwoIntegers.java @@ -0,0 +1,87 @@ +//给定两个整数,被除数 dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。 +// +// 返回被除数 dividend 除以除数 divisor 得到的商。 +// +// 整数除法的结果应当截去(truncate)其小数部分,例如:truncate(8.345) = 8 以及 truncate(-2.7335) = -2 +// +// +// +// 示例 1: +// +// 输入: dividend = 10, divisor = 3 +//输出: 3 +//解释: 10/3 = truncate(3.33333..) = truncate(3) = 3 +// +// 示例 2: +// +// 输入: dividend = 7, divisor = -3 +//输出: -2 +//解释: 7/-3 = truncate(-2.33333..) = -2 +// +// +// +// 提示: +// +// +// 被除数和除数均为 32 位有符号整数。 +// 除数不为 0。 +// 假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−2³¹, 231 − 1]。本题中,如果除法结果溢出,则返回 231 − 1。 +// +// Related Topics 位运算 数学 👍 700 👎 0 + +package leetcode.editor.cn; + +//29:两数相除 +class DivideTwoIntegers { + public static void main(String[] args) { + //测试代码 + Solution solution = new DivideTwoIntegers().new Solution(); + solution.divide(-2147483648, 2); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int divide(int dividend, int divisor) { + if (divisor == 0 || dividend == 0) { + return 0; + } + if (divisor == 1) { + return dividend; + } + if (divisor == -1) { + return dividend == -2147483648 ? 2147483647 : -dividend; + } + if (divisor == -2147483648) { + return dividend == -2147483648 ? 1 : 0; + } + int count = 0; + int bl1 = 0; + int bl2 = 0; + int temp = dividend; + if (dividend == -2147483648) { + dividend = 2147483647; + bl1 = 1; + } else if (dividend < 0) { + dividend = -dividend; + bl1 = 1; + } + if (divisor < 0) { + divisor = -divisor; + bl2 = 1; + } + while (dividend >= divisor) { + count++; + dividend -= divisor; + } + if (temp == -2147483648) { + if (dividend + 1 >= divisor) { + count++; + } + } + return (bl1 ^ bl2) == 0 ? count : -count; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/IntegerToEnglishWords.java b/src/main/java/leetcode/editor/cn/IntegerToEnglishWords.java new file mode 100644 index 0000000..c87f0ce --- /dev/null +++ b/src/main/java/leetcode/editor/cn/IntegerToEnglishWords.java @@ -0,0 +1,96 @@ +//将非负整数 num 转换为其对应的英文表示。 +// +// +// +// 示例 1: +// +// +//输入:num = 123 +//输出:"One Hundred Twenty Three" +// +// +// 示例 2: +// +// +//输入:num = 12345 +//输出:"Twelve Thousand Three Hundred Forty Five" +// +// +// 示例 3: +// +// +//输入:num = 1234567 +//输出:"One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" +// +// +// 示例 4: +// +// +//输入:num = 1234567891 +//输出:"One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven +//Thousand Eight Hundred Ninety One" +// +// +// +// +// 提示: +// +// +// 0 <= num <= 2³¹ - 1 +// +// Related Topics 递归 数学 字符串 👍 198 👎 0 + +package leetcode.editor.cn; + +//273:整数转换英文表示 +class IntegerToEnglishWords { + public static void main(String[] args) { + //测试代码 + Solution solution = new IntegerToEnglishWords().new Solution(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + String[] singles = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; + String[] teens = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; + String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; + String[] thousands = {"", "Thousand", "Million", "Billion"}; + + public String numberToWords(int num) { + if (num == 0) { + return "Zero"; + } + StringBuffer sb = new StringBuffer(); + for (int i = 3, unit = 1000000000; i >= 0; i--, unit /= 1000) { + int curNum = num / unit; + if (curNum != 0) { + num -= curNum * unit; + StringBuffer curr = new StringBuffer(); + recursion(curr, curNum); + curr.append(thousands[i]).append(" "); + sb.append(curr); + } + } + return sb.toString().trim(); + } + + public void recursion(StringBuffer curr, int num) { + if (num == 0) { + return; + } else if (num < 10) { + curr.append(singles[num]).append(" "); + } else if (num < 20) { + curr.append(teens[num - 10]).append(" "); + } else if (num < 100) { + curr.append(tens[num / 10]).append(" "); + recursion(curr, num % 10); + } else { + curr.append(singles[num / 100]).append(" Hundred "); + recursion(curr, num % 100); + } + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/LargestPalindromeProduct.java b/src/main/java/leetcode/editor/cn/LargestPalindromeProduct.java new file mode 100644 index 0000000..6f721ca --- /dev/null +++ b/src/main/java/leetcode/editor/cn/LargestPalindromeProduct.java @@ -0,0 +1,48 @@ +//你需要找到由两个 n 位数的乘积组成的最大回文数。 +// +// 由于结果会很大,你只需返回最大回文数 mod 1337得到的结果。 +// +// 示例: +// +// 输入: 2 +// +// 输出: 987 +// +// 解释: 99 x 91 = 9009, 9009 % 1337 = 987 +// +// 说明: +// +// n 的取值范围为 [1,8]。 +// Related Topics 数学 👍 41 👎 0 + +package leetcode.editor.cn; + +//479:最大回文数乘积 +class LargestPalindromeProduct { + public static void main(String[] args) { + //测试代码 + Solution solution = new LargestPalindromeProduct().new Solution(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int largestPalindrome(int n) { + if (n == 1) { + return 9; + } + long max = (long) Math.pow(10, n) - 1; + for (long i = max; i > max / 10; i--) { + long mul = Long.parseLong(i + new StringBuilder("" + i).reverse().toString()); + for (long j = max; j * j >= mul; j--) { + if (mul % j == 0) { + return (int) (mul % 1337); + } + } + } + return 0; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/MinimumOperationsToMakeAUniValueGrid.java b/src/main/java/leetcode/editor/cn/MinimumOperationsToMakeAUniValueGrid.java new file mode 100644 index 0000000..b5624b8 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/MinimumOperationsToMakeAUniValueGrid.java @@ -0,0 +1,98 @@ +//给你一个大小为 m x n 的二维整数网格 grid 和一个整数 x 。每一次操作,你可以对 grid 中的任一元素 加 x 或 减 x 。 +// +// 单值网格 是全部元素都相等的网格。 +// +// 返回使网格化为单值网格所需的 最小 操作数。如果不能,返回 -1 。 +// +// +// +// 示例 1: +// +// +// +// +//输入:grid = [[2,4],[6,8]], x = 2 +//输出:4 +//解释:可以执行下述操作使所有元素都等于 4 : +//- 2 加 x 一次。 +//- 6 减 x 一次。 +//- 8 减 x 两次。 +//共计 4 次操作。 +// +// +// 示例 2: +// +// +// +// +//输入:grid = [[1,5],[2,3]], x = 1 +//输出:5 +//解释:可以使所有元素都等于 3 。 +// +// +// 示例 3: +// +// +// +// +//输入:grid = [[1,2],[3,4]], x = 2 +//输出:-1 +//解释:无法使所有元素相等。 +// +// +// +// +// 提示: +// +// +// m == grid.length +// n == grid[i].length +// 1 <= m, n <= 10⁵ +// 1 <= m * n <= 10⁵ +// 1 <= x, grid[i][j] <= 10⁴ +// +// 👍 8 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.TwoArray; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +//2033:获取单值网格的最小操作数 +class MinimumOperationsToMakeAUniValueGrid { + public static void main(String[] args) { + //测试代码 + Solution solution = new MinimumOperationsToMakeAUniValueGrid().new Solution(); + TwoArray twoArray = new TwoArray("[[146]]", true); + solution.minOperations(twoArray.getArr(), 86); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int minOperations(int[][] grid, int x) { + List list = new ArrayList<>(); + int temp = grid[0][0]; + for (int[] ints : grid) { + for (int j = 0; j < grid[0].length; j++) { + if (Math.abs(ints[j] - temp) % x > 0) { + return -1; + } + list.add(ints[j]); + } + } + Collections.sort(list); + int num = list.get(list.size() / 2); + int sum = 0; + for (Integer integer : list) { + sum += Math.abs(integer - num) / x; + } + return sum; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/StockPriceFluctuation.java b/src/main/java/leetcode/editor/cn/StockPriceFluctuation.java new file mode 100644 index 0000000..9cd930b --- /dev/null +++ b/src/main/java/leetcode/editor/cn/StockPriceFluctuation.java @@ -0,0 +1,160 @@ +//给你一支股票价格的数据流。数据流中每一条记录包含一个 时间戳 和该时间点股票对应的 价格 。 +// +// 不巧的是,由于股票市场内在的波动性,股票价格记录可能不是按时间顺序到来的。某些情况下,有的记录可能是错的。如果两个有相同时间戳的记录出现在数据流中,前一条 +//记录视为错误记录,后出现的记录 更正 前一条错误的记录。 +// +// 请你设计一个算法,实现: +// +// +// 更新 股票在某一时间戳的股票价格,如果有之前同一时间戳的价格,这一操作将 更正 之前的错误价格。 +// 找到当前记录里 最新股票价格 。最新股票价格 定义为时间戳最晚的股票价格。 +// 找到当前记录里股票的 最高价格 。 +// 找到当前记录里股票的 最低价格 。 +// +// +// 请你实现 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 <= 10⁹ +// update,current,maximum 和 minimum 总 调用次数不超过 10⁵ 。 +// current,maximum 和 minimum 被调用时,update 操作 至少 已经被调用过 一次 。 +// +// 👍 6 👎 0 + +package leetcode.editor.cn; + +import java.util.TreeMap; + +//2034:股票价格波动 +class StockPriceFluctuation { + public static void main(String[] args) { + //测试代码 +// Solution solution = new StockPriceFluctuation().new Solution(); + StockPrice stockPrice = new StockPriceFluctuation().new StockPrice(); + stockPrice.update(38, 2308); + stockPrice.maximum(); + stockPrice.current(); + stockPrice.minimum(); + stockPrice.maximum(); + stockPrice.maximum(); + stockPrice.maximum(); + stockPrice.minimum(); + stockPrice.minimum(); + stockPrice.maximum(); + stockPrice.update(47, 7876); + stockPrice.maximum(); + stockPrice.minimum(); + stockPrice.update(58, 1866); + stockPrice.maximum(); + stockPrice.minimum(); + stockPrice.current(); + stockPrice.maximum(); + stockPrice.update(43, 121); + stockPrice.minimum(); + stockPrice.maximum(); + stockPrice.update(40, 5339); + stockPrice.maximum(); + stockPrice.maximum(); + stockPrice.current(); + stockPrice.update(32, 5339); + stockPrice.current();// + stockPrice.minimum(); + stockPrice.update(43, 6414); + stockPrice.update(49, 9369); + stockPrice.minimum(); + stockPrice.minimum(); + stockPrice.update(36, 3192); + stockPrice.current();// + stockPrice.update(48, 1006); + stockPrice.maximum(); + stockPrice.update(53, 8013); + stockPrice.minimum(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class StockPrice { + + TreeMap map; + TreeMap priceMap; + int cur; + + public StockPrice() { + map = new TreeMap<>(); + priceMap = new TreeMap<>(); + cur = 0; + } + + public void update(int timestamp, int price) { + if (map.containsKey(timestamp)) { + int oldPrice = map.get(timestamp); + priceMap.put(oldPrice, priceMap.get(oldPrice) - 1); + if (priceMap.get(oldPrice) == 0) { + priceMap.remove(oldPrice); + } + } + priceMap.put(price, priceMap.getOrDefault(price, 0) + 1); + map.put(timestamp, price); + cur = Math.max(cur, timestamp); + } + + public int current() { + return map.get(cur); + } + + public int maximum() { + return priceMap.lastKey(); + } + + public int minimum() { + return priceMap.firstKey(); + } + } + +/** + * Your StockPrice object will be instantiated and called as such: + * StockPrice obj = new StockPrice(); + * obj.update(timestamp,price); + * int param_2 = obj.current(); + * int param_3 = obj.maximum(); + * int param_4 = obj.minimum(); + */ +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/TwoSum.java b/src/main/java/leetcode/editor/cn/TwoSum.java index d80b9b0..8911277 100644 --- a/src/main/java/leetcode/editor/cn/TwoSum.java +++ b/src/main/java/leetcode/editor/cn/TwoSum.java @@ -45,54 +45,79 @@ package leetcode.editor.cn; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.*; //1:两数之和 -public class TwoSum{ +public class TwoSum { public static void main(String[] args) { //测试代码 Solution solution = new TwoSum().new Solution(); } + //力扣代码 //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public int[] twoSum(int[] nums, int target) { - int start = 0; - int end = nums.length - 1; - int[] result = new int[2]; - if (nums.length == 2) { - result[0] = start; - result[1] = end; - } else { - List list = new ArrayList(); - for (int i = 0; i < nums.length; i++) { - list.add(nums[i]); + class Solution { + // public int[] twoSum(int[] nums, int target) { +// int start = 0; +// int end = nums.length - 1; +// int[] result = new int[2]; +// if (nums.length == 2) { +// result[0] = start; +// result[1] = end; +// } else { +// List list = new ArrayList(); +// for (int i = 0; i < nums.length; i++) { +// list.add(nums[i]); +// } +// Arrays.sort(nums); +// while (nums[start] + nums[end] != target) { +// if (nums[start] + nums[end] > target) { +// end--; +// } else { +// start++; +// } +// } +// result[0] = list.indexOf(nums[start]); +// list.remove(result[0]); +// result[1] = list.indexOf(nums[end]); +// if (result[1] >= result[0]) { +// result[1]++; +// } +// if (result[0] > result[1]) { +// int temp = result[0]; +// result[0] = result[1]; +// result[1] = temp; +// } +// } +// return result; +// } + public int[] twoSum(int[] nums, int target) { + int[] arrs = new int[2]; + int start = 0; + int end = nums.length - 1; + List list = new ArrayList<>(); + for (int num : nums) { + list.add(num); } Arrays.sort(nums); - while (nums[start] + nums[end] != target) { - if (nums[start] + nums[end] > target) { + while (start < end) { + if (nums[start] + nums[end] < target) { + start++; + } else if (nums[start] + nums[end] > target) { end--; } else { - start++; + break; } } - result[0] = list.indexOf(nums[start]); - list.remove(result[0]); - result[1] = list.indexOf(nums[end]); - if (result[1] >= result[0]) { - result[1]++; - } - if (result[0] > result[1]) { - int temp = result[0]; - result[0] = result[1]; - result[1] = temp; + arrs[0] = list.indexOf(nums[start]); + list.remove(arrs[0]); + arrs[1] = list.indexOf(nums[end]); + if (arrs[1] >= arrs[0]) { + arrs[1]++; } + return arrs; } - return result; } -} //leetcode submit region end(Prohibit modification and deletion) } \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/TwoSumIiInputArrayIsSorted.java b/src/main/java/leetcode/editor/cn/TwoSumIiInputArrayIsSorted.java index 2bd0cde..54acee4 100644 --- a/src/main/java/leetcode/editor/cn/TwoSumIiInputArrayIsSorted.java +++ b/src/main/java/leetcode/editor/cn/TwoSumIiInputArrayIsSorted.java @@ -43,30 +43,32 @@ // 👍 533 👎 0 package leetcode.editor.cn; + //167:两数之和 II - 输入有序数组 -class TwoSumIiInputArrayIsSorted{ +class TwoSumIiInputArrayIsSorted { public static void main(String[] args) { //测试代码 Solution solution = new TwoSumIiInputArrayIsSorted().new Solution(); } + //力扣代码 //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public int[] twoSum(int[] numbers, int target) { - int start = 0; - int end = numbers.length-1; - while(starttarget){ - end--; - }else{ - start++; + class Solution { + public int[] twoSum(int[] numbers, int target) { + int start = 0; + int end = numbers.length - 1; + while (start < end) { + if (numbers[start] + numbers[end] == target) { + return new int[]{start + 1, end + 1}; + } else if (numbers[start] + numbers[end] > target) { + end--; + } else { + start++; + } } + return null; } - return null; } -} //leetcode submit region end(Prohibit modification and deletion) } \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/DivideTwoIntegers.md b/src/main/java/leetcode/editor/cn/doc/content/DivideTwoIntegers.md new file mode 100644 index 0000000..6aaf60f --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/DivideTwoIntegers.md @@ -0,0 +1,30 @@ +

给定两个整数,被除数 dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。

+ +

返回被除数 dividend 除以除数 divisor 得到的商。

+ +

整数除法的结果应当截去(truncate)其小数部分,例如:truncate(8.345) = 8 以及 truncate(-2.7335) = -2

+ +

 

+ +

示例 1:

+ +
输入: dividend = 10, divisor = 3
+输出: 3
+解释: 10/3 = truncate(3.33333..) = truncate(3) = 3
+ +

示例 2:

+ +
输入: dividend = 7, divisor = -3
+输出: -2
+解释: 7/-3 = truncate(-2.33333..) = -2
+ +

 

+ +

提示:

+ +
    +
  • 被除数和除数均为 32 位有符号整数。
  • +
  • 除数不为 0。
  • +
  • 假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−231,  231 − 1]。本题中,如果除法结果溢出,则返回 231 − 1。
  • +
+
Related Topics
  • 位运算
  • 数学

  • 👍 700
  • 👎 0
  • \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/IntegerToEnglishWords.md b/src/main/java/leetcode/editor/cn/doc/content/IntegerToEnglishWords.md new file mode 100644 index 0000000..f993cc9 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/IntegerToEnglishWords.md @@ -0,0 +1,40 @@ +

    将非负整数 num 转换为其对应的英文表示。

    + +

     

    + +

    示例 1:

    + +
    +输入:num = 123
    +输出:"One Hundred Twenty Three"
    +
    + +

    示例 2:

    + +
    +输入:num = 12345
    +输出:"Twelve Thousand Three Hundred Forty Five"
    +
    + +

    示例 3:

    + +
    +输入:num = 1234567
    +输出:"One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
    +
    + +

    示例 4:

    + +
    +输入:num = 1234567891
    +输出:"One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
    +
    + +

     

    + +

    提示:

    + +
      +
    • 0 <= num <= 231 - 1
    • +
    +
    Related Topics
  • 递归
  • 数学
  • 字符串

  • 👍 198
  • 👎 0
  • \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/LargestPalindromeProduct.md b/src/main/java/leetcode/editor/cn/doc/content/LargestPalindromeProduct.md new file mode 100644 index 0000000..4bef774 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/LargestPalindromeProduct.md @@ -0,0 +1,16 @@ +

    你需要找到由两个 n 位数的乘积组成的最大回文数。

    + +

    由于结果会很大,你只需返回最大回文数 mod 1337得到的结果。

    + +

    示例:

    + +

    输入: 2

    + +

    输出: 987

    + +

    解释: 99 x 91 = 9009, 9009 % 1337 = 987

    + +

    说明:

    + +

    n 的取值范围为 [1,8]。

    +
    Related Topics
  • 数学

  • 👍 41
  • 👎 0
  • \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/MinimumOperationsToMakeAUniValueGrid.md b/src/main/java/leetcode/editor/cn/doc/content/MinimumOperationsToMakeAUniValueGrid.md new file mode 100644 index 0000000..8b34640 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/MinimumOperationsToMakeAUniValueGrid.md @@ -0,0 +1,54 @@ +

    给你一个大小为 m x n 的二维整数网格 grid 和一个整数 x 。每一次操作,你可以对 grid 中的任一元素 x x

    + +

    单值网格 是全部元素都相等的网格。

    + +

    返回使网格化为单值网格所需的 最小 操作数。如果不能,返回 -1

    + +

     

    + +

    示例 1:

    + +

    + +
    +输入:grid = [[2,4],[6,8]], x = 2
    +输出:4
    +解释:可以执行下述操作使所有元素都等于 4 : 
    +- 2 加 x 一次。
    +- 6 减 x 一次。
    +- 8 减 x 两次。
    +共计 4 次操作。
    +
    + +

    示例 2:

    + +

    + +
    +输入:grid = [[1,5],[2,3]], x = 1
    +输出:5
    +解释:可以使所有元素都等于 3 。
    +
    + +

    示例 3:

    + +

    + +
    +输入:grid = [[1,2],[3,4]], x = 2
    +输出:-1
    +解释:无法使所有元素相等。
    +
    + +

     

    + +

    提示:

    + +
      +
    • m == grid.length
    • +
    • n == grid[i].length
    • +
    • 1 <= m, n <= 105
    • +
    • 1 <= m * n <= 105
    • +
    • 1 <= x, grid[i][j] <= 104
    • +
    +
  • 👍 8
  • 👎 0
  • \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/StockPriceFluctuation.md b/src/main/java/leetcode/editor/cn/doc/content/StockPriceFluctuation.md new file mode 100644 index 0000000..83cc75f --- /dev/null +++ b/src/main/java/leetcode/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 操作 至少 已经被调用过 一次 。
    • +
    +
  • 👍 6
  • 👎 0
  • \ No newline at end of file