Conflicts:
	src/main/java/leetcode/editor/cn/doc/all.json
	src/main/java/leetcode/editor/cn/doc/translation.json
This commit is contained in:
huangge1199@hotmail.com 2021-10-10 10:28:50 +08:00
commit 0158df232a
11 changed files with 495 additions and 3 deletions

View File

@ -0,0 +1,121 @@
//给你一个由非负整数 a1, a2, ..., an 组成的数据流输入请你将到目前为止看到的数字总结为不相交的区间列表
//
// 实现 SummaryRanges
//
//
//
//
// SummaryRanges() 使用一个空数据流初始化对象
// void addNum(int val) 向数据流中加入整数 val
// int[][] getIntervals() 以不相交区间 [starti, endi] 的列表形式返回对数据流中整数的总结
//
//
//
//
// 示例
//
//
//输入
//["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals",
//"addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
//[[], [1], [], [3], [], [7], [], [2], [], [6], []]
//输出
//[null, null, [[1, 1]], null, [[1, 1], [3, 3]], null, [[1, 1], [3, 3], [7, 7]],
// null, [[1, 3], [7, 7]], null, [[1, 3], [6, 7]]]
//
//解释
//SummaryRanges summaryRanges = new SummaryRanges();
//summaryRanges.addNum(1); // arr = [1]
//summaryRanges.getIntervals(); // 返回 [[1, 1]]
//summaryRanges.addNum(3); // arr = [1, 3]
//summaryRanges.getIntervals(); // 返回 [[1, 1], [3, 3]]
//summaryRanges.addNum(7); // arr = [1, 3, 7]
//summaryRanges.getIntervals(); // 返回 [[1, 1], [3, 3], [7, 7]]
//summaryRanges.addNum(2); // arr = [1, 2, 3, 7]
//summaryRanges.getIntervals(); // 返回 [[1, 3], [7, 7]]
//summaryRanges.addNum(6); // arr = [1, 2, 3, 6, 7]
//summaryRanges.getIntervals(); // 返回 [[1, 3], [6, 7]]
//
//
//
//
// 提示
//
//
// 0 <= val <= 10
// 最多调用 addNum getIntervals 方法 3 * 10
//
//
//
//
//
//
// 进阶如果存在大量合并并且与数据流的大小相比不相交区间的数量很小该怎么办?
// Related Topics 设计 二分查找 有序集合 👍 100 👎 0
package leetcode.editor.cn;
import lombok.val;
import java.util.Map;
import java.util.TreeMap;
//352:将数据流变为多个不相交区间
class DataStreamAsDisjointIntervals {
public static void main(String[] args) {
//测试代码
// Solution solution = new DataStreamAsDisjointIntervals().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class SummaryRanges {
TreeMap<Integer, Integer> treeMap;
public SummaryRanges() {
treeMap = new TreeMap<>();
}
public void addNum(int val) {
Map.Entry<Integer, Integer> larger = treeMap.ceilingEntry(val + 1);
Map.Entry<Integer, Integer> smaller = treeMap.floorEntry(val);
if (smaller != null && val <= smaller.getValue()) {
} else if (smaller != null && smaller.getValue() + 1 == val && larger != null && larger.getKey() - 1 == val) {
int key = smaller.getKey();
int value = larger.getValue();
treeMap.remove(larger.getKey());
treeMap.remove(smaller.getKey());
treeMap.put(key, value);
} else if (smaller != null && smaller.getValue() + 1 == val) {
treeMap.put(smaller.getKey(), val);
} else if (larger != null && larger.getKey() - 1 == val) {
treeMap.put(val, larger.getValue());
treeMap.remove(larger.getKey());
} else {
treeMap.put(val, val);
}
}
public int[][] getIntervals() {
int[][] result = new int[treeMap.size()][2];
int index = 0;
for (Map.Entry<Integer, Integer> map : treeMap.entrySet()) {
result[index][0] = map.getKey();
result[index][1] = map.getValue();
index++;
}
return result;
}
}
/**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* int[][] param_2 = obj.getIntervals();
*/
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,58 @@
//给你 二维 平面上两个 由直线构成的 矩形请你计算并返回两个矩形覆盖的总面积
//
// 每个矩形由其 左下 顶点和 右上 顶点坐标表示
//
//
//
// 第一个矩形由其左下顶点 (ax1, ay1) 和右上顶点 (ax2, ay2) 定义
// 第二个矩形由其左下顶点 (bx1, by1) 和右上顶点 (bx2, by2) 定义
//
//
//
//
//
// 示例 1
//
//
//输入ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2
//输出45
//
//
// 示例 2
//
//
//输入ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2
//输出16
//
//
//
//
// 提示
//
//
// -10 <= ax1, ay1, ax2, ay2, bx1, by1, bx2, by2 <= 10
//
// Related Topics 几何 数学 👍 128 👎 0
package leetcode.editor.cn;
//223:矩形面积
class RectangleArea {
public static void main(String[] args) {
//测试代码
Solution solution = new RectangleArea().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
return (ax2 - ax1) * (ay2 - ay1)
+ (bx2 - bx1) * (by2 - by1)
- Math.max(Math.min(ax2, bx2) - Math.max(ax1, bx1),0)
* Math.max(Math.min(ay2, by2) - Math.max(ay1, by1),0);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,64 @@
//所有 DNA 都由一系列缩写为 'A''C''G' 'T' 的核苷酸组成例如"ACGAATTCCG"在研究 DNA 识别 DNA 中的重复
//序列有时会对研究非常有帮助
//
// 编写一个函数来找出所有目标子串目标子串的长度为 10且在 DNA 字符串 s 中出现次数超过一次
//
//
//
// 示例 1
//
//
//输入s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
//输出["AAAAACCCCC","CCCCCAAAAA"]
//
//
// 示例 2
//
//
//输入s = "AAAAAAAAAAAAA"
//输出["AAAAAAAAAA"]
//
//
//
//
// 提示
//
//
// 0 <= s.length <= 10
// s[i] 'A''C''G' 'T'
//
// Related Topics 位运算 哈希表 字符串 滑动窗口 哈希函数 滚动哈希 👍 255 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//187:重复的DNA序列
class RepeatedDnaSequences {
public static void main(String[] args) {
//测试代码
Solution solution = new RepeatedDnaSequences().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<String> findRepeatedDnaSequences(String s) {
List<String> result = new ArrayList<>();
Map<String, Integer> cnt = new HashMap<>();
for (int i = 0; i <= s.length() - 10; ++i) {
String sub = s.substring(i, i + 10);
cnt.put(sub, cnt.getOrDefault(sub, 0) + 1);
if (cnt.get(sub) == 2) {
result.add(sub);
}
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,83 @@
//假设有 n 台超级洗衣机放在同一排上开始的时候每台洗衣机内可能有一定量的衣服也可能是空的
//
// 在每一步操作中你可以选择任意 m (1 <= m <= n) 台洗衣机与此同时将每台洗衣机的一件衣服送到相邻的一台洗衣机
//
// 给定一个整数数组 machines 代表从左至右每台洗衣机中的衣物数量请给出能让所有洗衣机中剩下的衣物的数量相等的 最少的操作步数 如果不能使每台洗衣
//机中衣物的数量相等则返回 -1
//
//
//
// 示例 1
//
//
//输入machines = [1,0,5]
//输出3
//解释
//第一步: 1 0 <-- 5 => 1 1 4
//第二步: 1 <-- 1 <-- 4 => 2 1 3
//第三步: 2 1 <-- 3 => 2 2 2
//
//
// 示例 2
//
//
//输入machines = [0,3,0]
//输出2
//解释
//第一步: 0 <-- 3 0 => 1 2 0
//第二步: 1 2 --> 0 => 1 1 1
//
//
// 示例 3
//
//
//输入machines = [0,2,0]
//输出-1
//解释
//不可能让所有三个洗衣机同时剩下相同数量的衣物
//
//
//
//
// 提示
//
//
// n == machines.length
// 1 <= n <= 10
// 0 <= machines[i] <= 10
//
// Related Topics 贪心 数组 👍 174 👎 0
package leetcode.editor.cn;
//517:超级洗衣机
class SuperWashingMachines {
public static void main(String[] args) {
//测试代码
Solution solution = new SuperWashingMachines().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int findMinMoves(int[] machines) {
int sum = 0;
for (int num : machines) {
sum += num;
}
if (sum % machines.length > 0) {
return -1;
}
int avg = sum / machines.length;
int bef = 0;
int result = 0;
for (int machine : machines) {
bef += machine - avg;
result = Math.max(result, Math.max(machine - avg, Math.abs(bef)));
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,52 @@
<p>&nbsp;给你一个由非负整数&nbsp;<code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code> 组成的数据流输入,请你将到目前为止看到的数字总结为不相交的区间列表。</p>
<p>实现 <code>SummaryRanges</code> 类:</p>
<div class="original__bRMd">
<div>
<ul>
<li><code>SummaryRanges()</code> 使用一个空数据流初始化对象。</li>
<li><code>void addNum(int val)</code> 向数据流中加入整数 <code>val</code></li>
<li><code>int[][] getIntervals()</code> 以不相交区间&nbsp;<code>[start<sub>i</sub>, end<sub>i</sub>]</code> 的列表形式返回对数据流中整数的总结。</li>
</ul>
<p>&nbsp;</p>
<p><strong>示例:</strong></p>
<pre>
<strong>输入:</strong>
["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
[[], [1], [], [3], [], [7], [], [2], [], [6], []]
<strong>输出:</strong>
[null, null, [[1, 1]], null, [[1, 1], [3, 3]], null, [[1, 1], [3, 3], [7, 7]], null, [[1, 3], [7, 7]], null, [[1, 3], [6, 7]]]
<strong>解释:</strong>
SummaryRanges summaryRanges = new SummaryRanges();
summaryRanges.addNum(1); // arr = [1]
summaryRanges.getIntervals(); // 返回 [[1, 1]]
summaryRanges.addNum(3); // arr = [1, 3]
summaryRanges.getIntervals(); // 返回 [[1, 1], [3, 3]]
summaryRanges.addNum(7); // arr = [1, 3, 7]
summaryRanges.getIntervals(); // 返回 [[1, 1], [3, 3], [7, 7]]
summaryRanges.addNum(2); // arr = [1, 2, 3, 7]
summaryRanges.getIntervals(); // 返回 [[1, 3], [7, 7]]
summaryRanges.addNum(6); // arr = [1, 2, 3, 6, 7]
summaryRanges.getIntervals(); // 返回 [[1, 3], [6, 7]]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 &lt;= val &lt;= 10<sup>4</sup></code></li>
<li>最多调用&nbsp;<code>addNum</code><code>getIntervals</code> 方法 <code>3 * 10<sup>4</sup></code></li>
</ul>
</div>
</div>
<p>&nbsp;</p>
<p><strong>进阶:</strong>如果存在大量合并,并且与数据流的大小相比,不相交区间的数量很小,该怎么办?</p>
<div><div>Related Topics</div><div><li>设计</li><li>二分查找</li><li>有序集合</li></div></div><br><div><li>👍 100</li><li>👎 0</li></div>

View File

@ -30,4 +30,4 @@
<li><meta charset="UTF-8" /><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code> </li>
<li><code>-1000 <= targetSum <= 1000</code> </li>
</ul>
<div><div>Related Topics</div><div><li></li><li>深度优先搜索</li><li>二叉树</li></div></div><br><div><li>👍 1091</li><li>👎 0</li></div>
<div><div>Related Topics</div><div><li></li><li>深度优先搜索</li><li>二叉树</li></div></div><br><div><li>👍 1054</li><li>👎 0</li></div>

View File

@ -0,0 +1,35 @@
<p>给你 <strong>二维</strong> 平面上两个 <strong>由直线构成的</strong> 矩形,请你计算并返回两个矩形覆盖的总面积。</p>
<p>每个矩形由其 <strong>左下</strong> 顶点和 <strong>右上</strong> 顶点坐标表示:</p>
<div class="MachineTrans-Lines">
<ul>
<li class="MachineTrans-lang-zh-CN">第一个矩形由其左下顶点 <code>(ax1, ay1)</code> 和右上顶点 <code>(ax2, ay2)</code> 定义。</li>
<li class="MachineTrans-lang-zh-CN">第二个矩形由其左下顶点 <code>(bx1, by1)</code> 和右上顶点 <code>(bx2, by2)</code> 定义。</li>
</ul>
</div>
<p> </p>
<p><strong>示例 1</strong></p>
<img alt="Rectangle Area" src="https://assets.leetcode.com/uploads/2021/05/08/rectangle-plane.png" style="width: 700px; height: 365px;" />
<pre>
<strong>输入:</strong>ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2
<strong>输出:</strong>45
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2
<strong>输出:</strong>16
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>-10<sup>4</sup> <= ax1, ay1, ax2, ay2, bx1, by1, bx2, by2 <= 10<sup>4</sup></code></li>
</ul>
<div><div>Related Topics</div><div><li>几何</li><li>数学</li></div></div><br><div><li>👍 128</li><li>👎 0</li></div>

View File

@ -0,0 +1,29 @@
<p>所有 DNA 都由一系列缩写为 <code>'A'</code><code>'C'</code><code>'G'</code><code>'T'</code> 的核苷酸组成,例如:<code>"ACGAATTCCG"</code>。在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助。</p>
<p>编写一个函数来找出所有目标子串,目标子串的长度为 10且在 DNA 字符串 <code>s</code> 中出现次数超过一次。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>输出:</strong>["AAAAACCCCC","CCCCCAAAAA"]
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>s = "AAAAAAAAAAAAA"
<strong>输出:</strong>["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code><code>'A'</code><code>'C'</code><code>'G'</code><code>'T'</code></li>
</ul>
<div><div>Related Topics</div><div><li>位运算</li><li>哈希表</li><li>字符串</li><li>滑动窗口</li><li>哈希函数</li><li>滚动哈希</li></div></div><br><div><li>👍 255</li><li>👎 0</li></div>

View File

@ -0,0 +1,48 @@
<p>假设有 <code>n</code><strong>&nbsp;</strong>台超级洗衣机放在同一排上。开始的时候,每台洗衣机内可能有一定量的衣服,也可能是空的。</p>
<p>在每一步操作中,你可以选择任意 <code>m</code> (<code>1 &lt;= m &lt;= n</code>) 台洗衣机,与此同时将每台洗衣机的一件衣服送到相邻的一台洗衣机。</p>
<p>给定一个整数数组&nbsp;<code>machines</code> 代表从左至右每台洗衣机中的衣物数量,请给出能让所有洗衣机中剩下的衣物的数量相等的 <strong>最少的操作步数 </strong>。如果不能使每台洗衣机中衣物的数量相等,则返回 <code>-1</code></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>machines = [1,0,5]
<strong>输出:</strong>3
<strong>解释:</strong>
第一步: 1 0 &lt;-- 5 =&gt; 1 1 4
第二步: 1 &lt;-- 1 &lt;-- 4 =&gt; 2 1 3
第三步: 2 1 &lt;-- 3 =&gt; 2 2 2
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>machines = [0,3,0]
<strong>输出:</strong>2
<strong>解释:</strong>
第一步: 0 &lt;-- 3 0 =&gt; 1 2 0
第二步: 1 2 --&gt; 0 =&gt; 1 1 1
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>machines = [0,2,0]
<strong>输出:</strong>-1
<strong>解释:</strong>
不可能让所有三个洗衣机同时剩下相同数量的衣物。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == machines.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
<li><code>0 &lt;= machines[i] &lt;= 10<sup>5</sup></code></li>
</ul>
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li></div></div><br><div><li>👍 174</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long