Merge remote-tracking branch 'origin/master'
# Conflicts: # src/main/java/leetcode/editor/cn/doc/all.json # src/main/java/leetcode/editor/cn/doc/translation.json
This commit is contained in:
commit
4c422cce5a
@ -26,6 +26,8 @@
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@ -35,16 +37,26 @@ class ContainsDuplicate{
|
||||
//测试代码
|
||||
Solution solution = new ContainsDuplicate().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
// public boolean containsDuplicate(int[] nums) {
|
||||
// Set<Integer> set = new HashSet<>();
|
||||
// for (int num : nums) {
|
||||
// if (set.contains(num)) {
|
||||
// return true;
|
||||
// }
|
||||
// set.add(num);
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
public boolean containsDuplicate(int[] nums) {
|
||||
Set<Integer> set = new HashSet<>();
|
||||
for (int num : nums) {
|
||||
if (set.contains(num)) {
|
||||
Arrays.sort(nums);
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
if(nums[i]==nums[i-1]){
|
||||
return true;
|
||||
}
|
||||
set.add(num);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
87
src/main/java/leetcode/editor/cn/DivideTwoIntegers.java
Normal file
87
src/main/java/leetcode/editor/cn/DivideTwoIntegers.java
Normal file
@ -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)
|
||||
|
||||
}
|
96
src/main/java/leetcode/editor/cn/IntegerToEnglishWords.java
Normal file
96
src/main/java/leetcode/editor/cn/IntegerToEnglishWords.java
Normal file
@ -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)
|
||||
|
||||
}
|
@ -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)
|
||||
|
||||
}
|
@ -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<Integer> 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)
|
||||
|
||||
}
|
160
src/main/java/leetcode/editor/cn/StockPriceFluctuation.java
Normal file
160
src/main/java/leetcode/editor/cn/StockPriceFluctuation.java
Normal file
@ -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<Integer, Integer> map;
|
||||
TreeMap<Integer, Integer> 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)
|
||||
|
||||
}
|
@ -45,9 +45,7 @@
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
//1:两数之和
|
||||
public class TwoSum {
|
||||
@ -55,42 +53,69 @@ public class TwoSum{
|
||||
//测试代码
|
||||
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<Integer> 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;
|
||||
int[] result = new int[2];
|
||||
if (nums.length == 2) {
|
||||
result[0] = start;
|
||||
result[1] = end;
|
||||
} else {
|
||||
List<Integer> list = new ArrayList();
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
list.add(nums[i]);
|
||||
List<Integer> 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]++;
|
||||
arrs[0] = list.indexOf(nums[start]);
|
||||
list.remove(arrs[0]);
|
||||
arrs[1] = list.indexOf(nums[end]);
|
||||
if (arrs[1] >= arrs[0]) {
|
||||
arrs[1]++;
|
||||
}
|
||||
if (result[0] > result[1]) {
|
||||
int temp = result[0];
|
||||
result[0] = result[1];
|
||||
result[1] = temp;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return arrs;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
@ -43,12 +43,14 @@
|
||||
// 👍 533 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//167:两数之和 II - 输入有序数组
|
||||
class TwoSumIiInputArrayIsSorted {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new TwoSumIiInputArrayIsSorted().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
|
@ -0,0 +1,30 @@
|
||||
<p>给定两个整数,被除数 <code>dividend</code> 和除数 <code>divisor</code>。将两数相除,要求不使用乘法、除法和 mod 运算符。</p>
|
||||
|
||||
<p>返回被除数 <code>dividend</code> 除以除数 <code>divisor</code> 得到的商。</p>
|
||||
|
||||
<p>整数除法的结果应当截去(<code>truncate</code>)其小数部分,例如:<code>truncate(8.345) = 8</code> 以及 <code>truncate(-2.7335) = -2</code></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong> dividend = 10, divisor = 3
|
||||
<strong>输出:</strong> 3
|
||||
<strong>解释: </strong>10/3 = truncate(3.33333..) = truncate(3) = 3</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong> dividend = 7, divisor = -3
|
||||
<strong>输出:</strong> -2
|
||||
<strong>解释:</strong> 7/-3 = truncate(-2.33333..) = -2</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>被除数和除数均为 32 位有符号整数。</li>
|
||||
<li>除数不为 0。</li>
|
||||
<li>假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−2<sup>31</sup>, 2<sup>31 </sup>− 1]。本题中,如果除法结果溢出,则返回 2<sup>31 </sup>− 1。</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>位运算</li><li>数学</li></div></div><br><div><li>👍 700</li><li>👎 0</li></div>
|
@ -0,0 +1,40 @@
|
||||
<p>将非负整数 <code>num</code> 转换为其对应的英文表示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 123
|
||||
<strong>输出:</strong>"One Hundred Twenty Three"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 12345
|
||||
<strong>输出:</strong>"Twelve Thousand Three Hundred Forty Five"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 1234567
|
||||
<strong>输出:</strong>"One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 1234567891
|
||||
<strong>输出:</strong>"One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= num <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>递归</li><li>数学</li><li>字符串</li></div></div><br><div><li>👍 198</li><li>👎 0</li></div>
|
@ -0,0 +1,16 @@
|
||||
<p>你需要找到由两个 n 位数的乘积组成的最大回文数。</p>
|
||||
|
||||
<p>由于结果会很大,你只需返回最大回文数 mod 1337得到的结果。</p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<p>输入: 2</p>
|
||||
|
||||
<p>输出: 987</p>
|
||||
|
||||
<p>解释: 99 x 91 = 9009, 9009 % 1337 = 987</p>
|
||||
|
||||
<p><strong>说明:</strong></p>
|
||||
|
||||
<p>n 的取值范围为 [1,8]。</p>
|
||||
<div><div>Related Topics</div><div><li>数学</li></div></div><br><div><li>👍 41</li><li>👎 0</li></div>
|
@ -0,0 +1,54 @@
|
||||
<p>给你一个大小为 <code>m x n</code> 的二维整数网格 <code>grid</code> 和一个整数 <code>x</code> 。每一次操作,你可以对 <code>grid</code> 中的任一元素 <strong>加</strong> <code>x</code> 或 <strong>减</strong> <code>x</code> 。</p>
|
||||
|
||||
<p><strong>单值网格</strong> 是全部元素都相等的网格。</p>
|
||||
|
||||
<p>返回使网格化为单值网格所需的 <strong>最小</strong> 操作数。如果不能,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/09/21/gridtxt.png" style="width: 164px; height: 165px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[2,4],[6,8]], x = 2
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>可以执行下述操作使所有元素都等于 4 :
|
||||
- 2 加 x 一次。
|
||||
- 6 减 x 一次。
|
||||
- 8 减 x 两次。
|
||||
共计 4 次操作。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/09/21/gridtxt-1.png" style="width: 164px; height: 165px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[1,5],[2,3]], x = 1
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>可以使所有元素都等于 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/09/21/gridtxt-2.png" style="width: 164px; height: 165px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[1,2],[3,4]], x = 2
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>无法使所有元素相等。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= x, grid[i][j] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
||||
<div><li>👍 8</li><li>👎 0</li></div>
|
@ -0,0 +1,56 @@
|
||||
<p>给你一支股票价格的数据流。数据流中每一条记录包含一个 <strong>时间戳</strong> 和该时间点股票对应的 <strong>价格</strong> 。</p>
|
||||
|
||||
<p>不巧的是,由于股票市场内在的波动性,股票价格记录可能不是按时间顺序到来的。某些情况下,有的记录可能是错的。如果两个有相同时间戳的记录出现在数据流中,前一条记录视为错误记录,后出现的记录 <b>更正</b> 前一条错误的记录。</p>
|
||||
|
||||
<p>请你设计一个算法,实现:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>更新 </strong>股票在某一时间戳的股票价格,如果有之前同一时间戳的价格,这一操作将 <strong>更正</strong> 之前的错误价格。</li>
|
||||
<li>找到当前记录里 <b>最新股票价格</b> 。<strong>最新股票价格</strong> 定义为时间戳最晚的股票价格。</li>
|
||||
<li>找到当前记录里股票的 <strong>最高价格</strong> 。</li>
|
||||
<li>找到当前记录里股票的 <strong>最低价格</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你实现 <code>StockPrice</code> 类:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>StockPrice()</code> 初始化对象,当前无股票价格记录。</li>
|
||||
<li><code>void update(int timestamp, int price)</code> 在时间点 <code>timestamp</code> 更新股票价格为 <code>price</code> 。</li>
|
||||
<li><code>int current()</code> 返回股票 <strong>最新价格</strong> 。</li>
|
||||
<li><code>int maximum()</code> 返回股票 <strong>最高价格</strong> 。</li>
|
||||
<li><code>int minimum()</code> 返回股票 <strong>最低价格</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>
|
||||
["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"]
|
||||
[[], [1, 10], [2, 5], [], [], [1, 3], [], [4, 2], []]
|
||||
<strong>输出:</strong>
|
||||
[null, null, null, 5, 10, null, 5, null, 2]
|
||||
|
||||
<strong>解释:</strong>
|
||||
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 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= timestamp, price <= 10<sup>9</sup></code></li>
|
||||
<li><code>update</code>,<code>current</code>,<code>maximum</code> 和 <code>minimum</code> <strong>总</strong> 调用次数不超过 <code>10<sup>5</sup></code> 。</li>
|
||||
<li><code>current</code>,<code>maximum</code> 和 <code>minimum</code> 被调用时,<code>update</code> 操作 <strong>至少</strong> 已经被调用过 <strong>一次</strong> 。</li>
|
||||
</ul>
|
||||
<div><li>👍 6</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user