Merge remote-tracking branch 'origin/master'

This commit is contained in:
huangge1199@hotmail.com 2021-05-23 13:51:00 +08:00
commit e755910965
8 changed files with 491 additions and 1 deletions

View File

@ -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)
}

View File

@ -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 &lt;= i &lt;= a &lt; m</code><code>0 &lt;= j &lt;= b &lt; 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 &lt;= m, n &lt;= 1000</code></li>
<li><code>0 &lt;= matrix[i][j] &lt;= 10<sup>6</sup></code></li>
<li><code>1 &lt;= k &lt;= m * n</code></li>
</ul>
<div><div>Related Topics</div><div><li>数组</li></div></div>\n<div><li>👍 23</li><li>👎 0</li></div>

View File

@ -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)
}

View 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)
}

View File

@ -0,0 +1,41 @@
<p>给一非空的单词列表,返回前&nbsp;<em>k&nbsp;</em>个出现次数最多的单词。</p>
<p>返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong> [&quot;i&quot;, &quot;love&quot;, &quot;leetcode&quot;, &quot;i&quot;, &quot;love&quot;, &quot;coding&quot;], k = 2
<strong>输出:</strong> [&quot;i&quot;, &quot;love&quot;]
<strong>解析:</strong> &quot;i&quot;&quot;love&quot; 为出现次数最多的两个单词均为2次。
注意,按字母顺序 &quot;i&quot;&quot;love&quot; 之前。
</pre>
<p>&nbsp;</p>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong> [&quot;the&quot;, &quot;day&quot;, &quot;is&quot;, &quot;sunny&quot;, &quot;the&quot;, &quot;the&quot;, &quot;the&quot;, &quot;sunny&quot;, &quot;is&quot;, &quot;is&quot;], k = 4
<strong>输出:</strong> [&quot;the&quot;, &quot;is&quot;, &quot;sunny&quot;, &quot;day&quot;]
<strong>解析:</strong> &quot;the&quot;, &quot;is&quot;, &quot;sunny&quot;&quot;day&quot; 是出现次数最多的四个单词,
出现次数依次为 4, 3, 2 和 1 次。
</pre>
<p>&nbsp;</p>
<p><strong>注意:</strong></p>
<ol>
<li>假定 <em>k</em> 总为有效值, 1 &le; <em>k</em> &le; 集合元素数。</li>
<li>输入的单词均由小写字母组成。</li>
</ol>
<p>&nbsp;</p>
<p><strong>扩展练习:</strong></p>
<ol>
<li>尝试以&nbsp;<em>O</em>(<em>n</em> log <em>k</em>) 时间复杂度和&nbsp;<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>

View 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)
}

View 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