Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
e755910965
@ -0,0 +1,88 @@
|
||||
//给你一个二维矩阵 matrix 和一个整数 k ,矩阵大小为 m x n 由非负整数组成。
|
||||
//
|
||||
// 矩阵中坐标 (a, b) 的 值 可由对所有满足 0 <= i <= a < m 且 0 <= j <= b < n 的元素 matrix[i][j](下
|
||||
//标从 0 开始计数)执行异或运算得到。
|
||||
//
|
||||
// 请你找出 matrix 的所有坐标中第 k 大的值(k 的值从 1 开始计数)。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
// 输入:matrix = [[5,2],[1,6]], k = 1
|
||||
//输出:7
|
||||
//解释:坐标 (0,1) 的值是 5 XOR 2 = 7 ,为最大的值。
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
// 输入:matrix = [[5,2],[1,6]], k = 2
|
||||
//输出:5
|
||||
//解释:坐标 (0,0) 的值是 5 = 5 ,为第 2 大的值。
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
// 输入:matrix = [[5,2],[1,6]], k = 3
|
||||
//输出:4
|
||||
//解释:坐标 (1,0) 的值是 5 XOR 1 = 4 ,为第 3 大的值。
|
||||
//
|
||||
// 示例 4:
|
||||
//
|
||||
// 输入:matrix = [[5,2],[1,6]], k = 4
|
||||
//输出:0
|
||||
//解释:坐标 (1,1) 的值是 5 XOR 2 XOR 1 XOR 6 = 0 ,为第 4 大的值。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// m == matrix.length
|
||||
// n == matrix[i].length
|
||||
// 1 <= m, n <= 1000
|
||||
// 0 <= matrix[i][j] <= 106
|
||||
// 1 <= k <= m * n
|
||||
//
|
||||
// Related Topics 数组
|
||||
// 👍 23 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
//1738:找出第 K 大的异或坐标值
|
||||
public class FindKthLargestXorCoordinateValue {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new FindKthLargestXorCoordinateValue().new Solution();
|
||||
solution.kthLargestValue(new int[][]{{5, 2}, {1, 6}}, 1);
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int kthLargestValue(int[][] matrix, int k) {
|
||||
int xLength = matrix.length;
|
||||
int yLength = matrix[0].length;
|
||||
int[][] result = new int[xLength][yLength];
|
||||
List<Integer> xor = new ArrayList<>();
|
||||
for (int i = 0; i < xLength; i++) {
|
||||
for (int j = 0; j < yLength; j++) {
|
||||
if (i == 0 && j == 0) {
|
||||
result[0][0] = matrix[0][0];
|
||||
} else if (i > 0 && j > 0) {
|
||||
result[i][j] = result[i - 1][j] ^ result[i][j - 1] ^ result[i - 1][j - 1] ^ matrix[i][j];
|
||||
} else if (i == 0) {
|
||||
result[i][j] = result[i][j - 1] ^ matrix[i][j];
|
||||
} else {
|
||||
result[i][j] = result[i - 1][j] ^ matrix[i][j];
|
||||
}
|
||||
xor.add(result[i][j]);
|
||||
}
|
||||
}
|
||||
Collections.sort(xor);
|
||||
return xor.get(xLength * yLength - k);
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
<p>给你一个二维矩阵 <code>matrix</code> 和一个整数 <code>k</code> ,矩阵大小为 <code>m x n</code> 由非负整数组成。</p>
|
||||
|
||||
<p>矩阵中坐标 <code>(a, b)</code> 的 <strong>值</strong> 可由对所有满足 <code>0 <= i <= a < m</code> 且 <code>0 <= j <= b < n</code> 的元素 <code>matrix[i][j]</code>(<strong>下标从 0 开始计数</strong>)执行异或运算得到。</p>
|
||||
|
||||
<p>请你找出 <code>matrix</code> 的所有坐标中第 <code>k</code> 大的值(<strong><code>k</code> 的值从 1 开始计数</strong>)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>matrix = [[5,2],[1,6]], k = 1
|
||||
<strong>输出:</strong>7
|
||||
<strong>解释:</strong>坐标 (0,1) 的值是 5 XOR 2 = 7 ,为最大的值。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>matrix = [[5,2],[1,6]], k = 2
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>坐标 (0,0) 的值是 5 = 5 ,为第 2 大的值。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>matrix = [[5,2],[1,6]], k = 3
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>坐标 (1,0) 的值是 5 XOR 1 = 4 ,为第 3 大的值。</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>matrix = [[5,2],[1,6]], k = 4
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>坐标 (1,1) 的值是 5 XOR 2 XOR 1 XOR 6 = 0 ,为第 4 大的值。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == matrix.length</code></li>
|
||||
<li><code>n == matrix[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 1000</code></li>
|
||||
<li><code>0 <= matrix[i][j] <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= k <= m * n</code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>数组</li></div></div>\n<div><li>👍 23</li><li>👎 0</li></div>
|
@ -0,0 +1,92 @@
|
||||
//给你一个二维矩阵 matrix 和一个整数 k ,矩阵大小为 m x n 由非负整数组成。
|
||||
//
|
||||
// 矩阵中坐标 (a, b) 的 值 可由对所有满足 0 <= i <= a < m 且 0 <= j <= b < n 的元素 matrix[i][j](下
|
||||
//标从 0 开始计数)执行异或运算得到。
|
||||
//
|
||||
// 请你找出 matrix 的所有坐标中第 k 大的值(k 的值从 1 开始计数)。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
// 输入:matrix = [[5,2],[1,6]], k = 1
|
||||
//输出:7
|
||||
//解释:坐标 (0,1) 的值是 5 XOR 2 = 7 ,为最大的值。
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
// 输入:matrix = [[5,2],[1,6]], k = 2
|
||||
//输出:5
|
||||
//解释:坐标 (0,0) 的值是 5 = 5 ,为第 2 大的值。
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
// 输入:matrix = [[5,2],[1,6]], k = 3
|
||||
//输出:4
|
||||
//解释:坐标 (1,0) 的值是 5 XOR 1 = 4 ,为第 3 大的值。
|
||||
//
|
||||
// 示例 4:
|
||||
//
|
||||
// 输入:matrix = [[5,2],[1,6]], k = 4
|
||||
//输出:0
|
||||
//解释:坐标 (1,1) 的值是 5 XOR 2 XOR 1 XOR 6 = 0 ,为第 4 大的值。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// m == matrix.length
|
||||
// n == matrix[i].length
|
||||
// 1 <= m, n <= 1000
|
||||
// 0 <= matrix[i][j] <= 106
|
||||
// 1 <= k <= m * n
|
||||
//
|
||||
// Related Topics 数组
|
||||
// 👍 23 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.PriorityQueue;
|
||||
|
||||
//1738:找出第 K 大的异或坐标值
|
||||
public class FindKthLargestXorCoordinateValue1 {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new FindKthLargestXorCoordinateValue1().new Solution();
|
||||
solution.kthLargestValue(new int[][]{{5, 2}, {1, 6}}, 1);
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int kthLargestValue(int[][] matrix, int k) {
|
||||
int xLength = matrix.length;
|
||||
int yLength = matrix[0].length;
|
||||
int[][] result = new int[xLength][yLength];
|
||||
PriorityQueue<Integer> xor = new PriorityQueue<>((o1, o2) -> o2 - o1);
|
||||
result[0][0] = matrix[0][0];
|
||||
xor.add(result[0][0]);
|
||||
for (int i = 1; i < xLength; i++) {
|
||||
result[i][0] = result[i - 1][0] ^ matrix[i][0];
|
||||
xor.add(result[i][0]);
|
||||
}
|
||||
for (int i = 1; i < yLength; i++) {
|
||||
result[0][i] = result[0][i - 1] ^ matrix[0][i];
|
||||
xor.add(result[0][i]);
|
||||
}
|
||||
for (int i = 1; i < xLength; i++) {
|
||||
for (int j = 1; j < yLength; j++) {
|
||||
result[i][j] = result[i - 1][j] ^ result[i][j - 1] ^ result[i - 1][j - 1] ^ matrix[i][j];
|
||||
xor.add(result[i][j]);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < k - 1; i++) {
|
||||
xor.poll();
|
||||
}
|
||||
return xor.peek();
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
83
src/main/java/leetcode/editor/cn/TopKFrequentWords.java
Normal file
83
src/main/java/leetcode/editor/cn/TopKFrequentWords.java
Normal file
@ -0,0 +1,83 @@
|
||||
//给一非空的单词列表,返回前 k 个出现次数最多的单词。
|
||||
//
|
||||
// 返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
|
||||
//输出: ["i", "love"]
|
||||
//解析: "i" 和 "love" 为出现次数最多的两个单词,均为2次。
|
||||
// 注意,按字母顺序 "i" 在 "love" 之前。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k
|
||||
// = 4
|
||||
//输出: ["the", "is", "sunny", "day"]
|
||||
//解析: "the", "is", "sunny" 和 "day" 是出现次数最多的四个单词,
|
||||
// 出现次数依次为 4, 3, 2 和 1 次。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 注意:
|
||||
//
|
||||
//
|
||||
// 假定 k 总为有效值, 1 ≤ k ≤ 集合元素数。
|
||||
// 输入的单词均由小写字母组成。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 扩展练习:
|
||||
//
|
||||
//
|
||||
// 尝试以 O(n log k) 时间复杂度和 O(n) 空间复杂度解决。
|
||||
//
|
||||
// Related Topics 堆 字典树 哈希表
|
||||
// 👍 265 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.PriorityQueue;
|
||||
|
||||
//692:前K个高频单词
|
||||
public class TopKFrequentWords{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new TopKFrequentWords().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public List<String> topKFrequent(String[] words, int k) {
|
||||
HashMap<String, Integer> map = new HashMap<>();
|
||||
for (String s : words) {
|
||||
map.put(s, map.getOrDefault(s, 0) + 1);
|
||||
}
|
||||
PriorityQueue<String> queue = new PriorityQueue<>((o1, o2) ->{
|
||||
if (map.get(o1).equals(map.get(o2))) {
|
||||
return o1.compareTo(o2);
|
||||
}
|
||||
return map.get(o2) - map.get(o1);
|
||||
});
|
||||
queue.addAll(map.keySet());
|
||||
List<String> result = new ArrayList<>();
|
||||
for (int i = 0; i < k; i++) {
|
||||
result.add(queue.peek());
|
||||
queue.poll();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
41
src/main/java/leetcode/editor/cn/TopKFrequentWords.md
Normal file
41
src/main/java/leetcode/editor/cn/TopKFrequentWords.md
Normal file
@ -0,0 +1,41 @@
|
||||
<p>给一非空的单词列表,返回前 <em>k </em>个出现次数最多的单词。</p>
|
||||
|
||||
<p>返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。</p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> ["i", "love", "leetcode", "i", "love", "coding"], k = 2
|
||||
<strong>输出:</strong> ["i", "love"]
|
||||
<strong>解析:</strong> "i" 和 "love" 为出现次数最多的两个单词,均为2次。
|
||||
注意,按字母顺序 "i" 在 "love" 之前。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
|
||||
<strong>输出:</strong> ["the", "is", "sunny", "day"]
|
||||
<strong>解析:</strong> "the", "is", "sunny" 和 "day" 是出现次数最多的四个单词,
|
||||
出现次数依次为 4, 3, 2 和 1 次。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li>假定 <em>k</em> 总为有效值, 1 ≤ <em>k</em> ≤ 集合元素数。</li>
|
||||
<li>输入的单词均由小写字母组成。</li>
|
||||
</ol>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>扩展练习:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li>尝试以 <em>O</em>(<em>n</em> log <em>k</em>) 时间复杂度和 <em>O</em>(<em>n</em>) 空间复杂度解决。</li>
|
||||
</ol>
|
||||
<div><div>Related Topics</div><div><li>堆</li><li>字典树</li><li>哈希表</li></div></div>\n<div><li>👍 265</li><li>👎 0</li></div>
|
88
src/main/java/leetcode/editor/cn/UncrossedLines.java
Normal file
88
src/main/java/leetcode/editor/cn/UncrossedLines.java
Normal file
@ -0,0 +1,88 @@
|
||||
//在两条独立的水平线上按给定的顺序写下 nums1 和 nums2 中的整数。
|
||||
//
|
||||
// 现在,可以绘制一些连接两个数字 nums1[i] 和 nums2[j] 的直线,这些直线需要同时满足满足:
|
||||
//
|
||||
//
|
||||
// nums1[i] == nums2[j]
|
||||
// 且绘制的直线不与任何其他连线(非水平线)相交。
|
||||
//
|
||||
//
|
||||
// 请注意,连线即使在端点也不能相交:每个数字只能属于一条连线。
|
||||
//
|
||||
// 以这种方法绘制线条,并返回可以绘制的最大连线数。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//
|
||||
//输入:nums1 = [1,4,2], nums2 = [1,2,4]
|
||||
//输出:2
|
||||
//解释:可以画出两条不交叉的线,如上图所示。
|
||||
//但无法画出第三条不相交的直线,因为从 nums1[1]=4 到 nums2[2]=4 的直线将与从 nums1[2]=2 到 nums2[1]=2 的直线相
|
||||
//交。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:nums1 = [2,5,1,2,5], nums2 = [10,5,2,1,5,2]
|
||||
//输出:3
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:nums1 = [1,3,7,1,7,5], nums2 = [1,9,2,5,1]
|
||||
//输出:2
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= nums1.length <= 500
|
||||
// 1 <= nums2.length <= 500
|
||||
// 1 <= nums1[i], nums2[i] <= 2000
|
||||
//
|
||||
//
|
||||
//
|
||||
// Related Topics 数组
|
||||
// 👍 159 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//1035:不相交的线
|
||||
public class UncrossedLines {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new UncrossedLines().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int maxUncrossedLines(int[] nums1, int[] nums2) {
|
||||
int length1 = nums1.length;
|
||||
int length2 = nums2.length;
|
||||
int[][] counts = new int[length1 + 1][length2 + 1];
|
||||
for (int i = 1; i <= length1; i++) {
|
||||
for (int j = 1; j <= length2; j++) {
|
||||
if (nums1[i - 1] == nums2[j - 1]) {
|
||||
counts[i][j] = counts[i - 1][j - 1] + 1;
|
||||
} else {
|
||||
counts[i][j] = Math.max(counts[i - 1][j], counts[i][j - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return counts[length1][length2];
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
54
src/main/java/leetcode/editor/cn/UncrossedLines.md
Normal file
54
src/main/java/leetcode/editor/cn/UncrossedLines.md
Normal file
@ -0,0 +1,54 @@
|
||||
<p>在两条独立的水平线上按给定的顺序写下 <code>nums1</code> 和 <code>nums2</code> 中的整数。</p>
|
||||
|
||||
<p>现在,可以绘制一些连接两个数字 <code>nums1[i]</code> 和 <code>nums2[j]</code> 的直线,这些直线需要同时满足满足:</p>
|
||||
|
||||
<ul>
|
||||
<li> <code>nums1[i] == nums2[j]</code></li>
|
||||
<li>且绘制的直线不与任何其他连线(非水平线)相交。</li>
|
||||
</ul>
|
||||
|
||||
<p>请注意,连线即使在端点也不能相交:每个数字只能属于一条连线。</p>
|
||||
|
||||
<p>以这种方法绘制线条,并返回可以绘制的最大连线数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/04/28/142.png" style="height: 72px; width: 100px;" /></strong>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = <span id="example-input-1-1">[1,4,2]</span>, nums2 = <span id="example-input-1-2">[1,2,4]</span>
|
||||
<strong>输出:</strong><span id="example-output-1">2</span>
|
||||
<strong>解释:</strong>可以画出两条不交叉的线,如上图所示。
|
||||
但无法画出第三条不相交的直线,因为从 nums1[1]=4 到 nums2[2]=4 的直线将与从 nums1[2]=2 到 nums2[1]=2 的直线相交。
|
||||
</pre>
|
||||
|
||||
<div>
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = <span id="example-input-2-1">[2,5,1,2,5]</span>, nums2 = <span id="example-input-2-2">[10,5,2,1,5,2]</span>
|
||||
<strong>输出:</strong><span id="example-output-2">3</span>
|
||||
</pre>
|
||||
|
||||
<div>
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = <span id="example-input-3-1">[1,3,7,1,7,5]</span>, nums2 = <span id="example-input-3-2">[1,9,2,5,1]</span>
|
||||
<strong>输出:</strong><span id="example-output-3">2</span></pre>
|
||||
|
||||
<p> </p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length <= 500</code></li>
|
||||
<li><code>1 <= nums2.length <= 500</code></li>
|
||||
<li><code><font face="monospace">1 <= nums1[i], nums2[i] <= 2000</font></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<div><div>Related Topics</div><div><li>数组</li></div></div>\n<div><li>👍 159</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user