Merge remote-tracking branch 'origin/master'

# Conflicts:
#	src/main/java/leetcode/editor/cn/all.json
#	src/main/java/leetcode/editor/cn/translation.json
This commit is contained in:
huangge1199 2021-07-09 14:43:01 +08:00
commit c032fc9acd
15 changed files with 626 additions and 25 deletions

View File

@ -0,0 +1,72 @@
//给定两个数组编写一个函数来计算它们的交集
//
//
//
// 示例 1
//
// 输入nums1 = [1,2,2,1], nums2 = [2,2]
//输出[2,2]
//
//
// 示例 2:
//
// 输入nums1 = [4,9,5], nums2 = [9,4,9,8,4]
//输出[4,9]
//
//
//
// 说明
//
//
// 输出结果中每个元素出现的次数应与元素在两个数组中出现次数的最小值一致
// 我们可以不考虑输出结果的顺序
//
//
// 进阶
//
//
// 如果给定的数组已经排好序呢你将如何优化你的算法
// 如果 nums1 的大小比 nums2 小很多哪种方法更优
// 如果 nums2 的元素存储在磁盘上内存是有限的并且你不能一次加载所有的元素到内存中你该怎么办
//
// Related Topics 数组 哈希表 双指针 二分查找 排序
// 👍 512 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
//350:两个数组的交集 II
class IntersectionOfTwoArraysIi{
public static void main(String[] args) {
//测试代码
Solution solution = new IntersectionOfTwoArraysIi().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
List<Integer> list = new ArrayList<>();
int index1 = 0;
int index2 = 0;
while(index1<nums1.length&&index2<nums2.length){
if(nums1[index1]==nums2[index2]){
list.add(nums1[index1]);
index1++;
index2++;
}else if (nums1[index1]>nums2[index2]){
index2++;
}else{
index1++;
}
}
return list.stream().mapToInt(Integer::intValue).toArray();
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,32 @@
<p>给定两个数组,编写一个函数来计算它们的交集。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>nums1 = [1,2,2,1], nums2 = [2,2]
<strong>输出:</strong>[2,2]
</pre>
<p><strong>示例 2:</strong></p>
<pre><strong>输入:</strong>nums1 = [4,9,5], nums2 = [9,4,9,8,4]
<strong>输出:</strong>[4,9]</pre>
<p>&nbsp;</p>
<p><strong>说明:</strong></p>
<ul>
<li>输出结果中每个元素出现的次数,应与元素在两个数组中出现次数的最小值一致。</li>
<li>我们可以不考虑输出结果的顺序。</li>
</ul>
<p><strong><strong>进阶</strong></strong></p>
<ul>
<li>如果给定的数组已经排好序呢?你将如何优化你的算法?</li>
<li>如果&nbsp;<em>nums1&nbsp;</em>的大小比&nbsp;<em>nums2&nbsp;</em>小很多,哪种方法更优?</li>
<li>如果&nbsp;<em>nums2&nbsp;</em>的元素存储在磁盘上,内存是有限的,并且你不能一次加载所有的元素到内存中,你该怎么办?</li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>哈希表</li><li>双指针</li><li>二分查找</li><li>排序</li></div></div>\n<div><li>👍 512</li><li>👎 0</li></div>

View File

@ -35,6 +35,8 @@
package leetcode.editor.cn; package leetcode.editor.cn;
import java.util.Arrays;
//88:合并两个有序数组 //88:合并两个有序数组
public class MergeSortedArray { public class MergeSortedArray {
public static void main(String[] args) { public static void main(String[] args) {
@ -49,29 +51,8 @@ public class MergeSortedArray {
//leetcode submit region begin(Prohibit modification and deletion) //leetcode submit region begin(Prohibit modification and deletion)
class Solution { class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) { public void merge(int[] nums1, int m, int[] nums2, int n) {
int i = 0, j = 0; if (m + n - m >= 0) System.arraycopy(nums2, 0, nums1, m, m + n - m);
if (m == 0) { Arrays.sort(nums1);
nums1 = nums2;
return;
}
if (n == 0) {
return;
}
while (j < n) {
if (i >= m) {
nums1[i] = nums2[j];
j++;
} else if (nums1[i] > nums2[j]) {
if (m - i - i >= 0) {
if (m + n - 1 - i - i >= 0) {
System.arraycopy(nums1, i, nums1, i + 1, m + n - 1 - i - i);
}
}
nums1[i] = nums2[j];
j++;
}
i++;
}
} }
} }
//leetcode submit region end(Prohibit modification and deletion) //leetcode submit region end(Prohibit modification and deletion)

View File

@ -0,0 +1,48 @@
//给定一个数组 nums编写一个函数将所有 0 移动到数组的末尾同时保持非零元素的相对顺序
//
// 示例:
//
// 输入: [0,1,0,3,12]
//输出: [1,3,12,0,0]
//
// 说明:
//
//
// 必须在原数组上操作不能拷贝额外的数组
// 尽量减少操作次数
//
// Related Topics 数组 双指针
// 👍 1110 👎 0
package leetcode.editor.cn;
//283:移动零
class MoveZeroes {
public static void main(String[] args) {
//测试代码
Solution solution = new MoveZeroes().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public void moveZeroes(int[] nums) {
int low = 0, fast = 1;
for (; fast < nums.length; fast++) {
if (nums[low] == 0) {
if (nums[fast] == 0) continue;
else {
int tmp = nums[low];
nums[low] = nums[fast];
nums[fast] = tmp;
low++;
}
continue;
}
low++;
}
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,14 @@
<p>给定一个数组 <code>nums</code>,编写一个函数将所有 <code>0</code> 移动到数组的末尾,同时保持非零元素的相对顺序。</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong> <code>[0,1,0,3,12]</code>
<strong>输出:</strong> <code>[1,3,12,0,0]</code></pre>
<p><strong>说明</strong>:</p>
<ol>
<li>必须在原数组上操作,不能拷贝额外的数组。</li>
<li>尽量减少操作次数。</li>
</ol>
<div><div>Related Topics</div><div><li>数组</li><li>双指针</li></div></div>\n<div><li>👍 1110</li><li>👎 0</li></div>

View File

@ -0,0 +1,72 @@
//给定一个数组将数组中的元素向右移动 k 个位置其中 k 是非负数
//
//
//
// 进阶
//
//
// 尽可能想出更多的解决方案至少有三种不同的方法可以解决这个问题
// 你可以使用空间复杂度为 O(1) 原地 算法解决这个问题吗
//
//
//
//
// 示例 1:
//
//
//输入: nums = [1,2,3,4,5,6,7], k = 3
//输出: [5,6,7,1,2,3,4]
//解释:
//向右旋转 1 : [7,1,2,3,4,5,6]
//向右旋转 2 : [6,7,1,2,3,4,5]
//向右旋转 3 : [5,6,7,1,2,3,4]
//
//
// 示例 2:
//
//
//输入nums = [-1,-100,3,99], k = 2
//输出[3,99,-1,-100]
//解释:
//向右旋转 1 : [99,-1,-100,3]
//向右旋转 2 : [3,99,-1,-100]
//
//
//
// 提示
//
//
// 1 <= nums.length <= 2 * 104
// -231 <= nums[i] <= 231 - 1
// 0 <= k <= 105
//
//
//
//
// Related Topics 数组 数学 双指针
// 👍 1003 👎 0
package leetcode.editor.cn;
//189:旋转数组
class RotateArray{
public static void main(String[] args) {
//测试代码
Solution solution = new RotateArray().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public void rotate(int[] nums, int k) {
int length = nums.length;
int temp[] = new int[length];
for (int i = 0; i < length; i++) {
temp[i] = nums[i];
}
for (int i = 0; i < length; i++) {
nums[(i + k) % length] = temp[i];
}
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,46 @@
<p>给定一个数组,将数组中的元素向右移动 <code>k</code><em> </em>个位置,其中 <code>k</code><em> </em>是非负数。</p>
<p> </p>
<p><strong>进阶:</strong></p>
<ul>
<li>尽可能想出更多的解决方案,至少有三种不同的方法可以解决这个问题。</li>
<li>你可以使用空间复杂度为 O(1) 的 <strong>原地 </strong>算法解决这个问题吗?</li>
</ul>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> nums = [1,2,3,4,5,6,7], k = 3
<strong>输出:</strong> <code>[5,6,7,1,2,3,4]</code>
<strong>解释:</strong>
向右旋转 1 步: <code>[7,1,2,3,4,5,6]</code>
向右旋转 2 步: <code>[6,7,1,2,3,4,5]
</code>向右旋转 3 步: <code>[5,6,7,1,2,3,4]</code>
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>nums = [-1,-100,3,99], k = 2
<strong>输出:</strong>[3,99,-1,-100]
<strong>解释:</strong>
向右旋转 1 步: [99,-1,-100,3]
向右旋转 2 步: [3,99,-1,-100]</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li><code>0 <= k <= 10<sup>5</sup></code></li>
</ul>
<ul>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>数学</li><li>双指针</li></div></div>\n<div><li>👍 1003</li><li>👎 0</li></div>

View File

@ -0,0 +1,66 @@
//给你一个按 非递减顺序 排序的整数数组 nums返回 每个数字的平方 组成的新数组要求也按 非递减顺序 排序
//
//
//
//
//
//
// 示例 1
//
//
//输入nums = [-4,-1,0,3,10]
//输出[0,1,9,16,100]
//解释平方后数组变为 [16,1,0,9,100]
//排序后数组变为 [0,1,9,16,100]
//
// 示例 2
//
//
//输入nums = [-7,-3,2,3,11]
//输出[4,9,9,49,121]
//
//
//
//
// 提示
//
//
// 1 <= nums.length <= 104
// -104 <= nums[i] <= 104
// nums 已按 非递减顺序 排序
//
//
//
//
// 进阶
//
//
// 请你设计时间复杂度为 O(n) 的算法解决本问题
//
// Related Topics 数组 双指针 排序
// 👍 247 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
//977:有序数组的平方
class SquaresOfASortedArray{
public static void main(String[] args) {
//测试代码
Solution solution = new SquaresOfASortedArray().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] sortedSquares(int[] nums) {
for (int i = 0; i < nums.length; i++) {
nums[i]*=nums[i];
}
Arrays.sort(nums);
return nums;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,40 @@
<p>给你一个按 <strong>非递减顺序</strong> 排序的整数数组 <code>nums</code>,返回 <strong>每个数字的平方</strong> 组成的新数组,要求也按 <strong>非递减顺序</strong> 排序。</p>
<ul>
</ul>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [-4,-1,0,3,10]
<strong>输出:</strong>[0,1,9,16,100]
<strong>解释:</strong>平方后,数组变为 [16,1,0,9,100]
排序后,数组变为 [0,1,9,16,100]</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [-7,-3,2,3,11]
<strong>输出:</strong>[4,9,9,49,121]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code><span>1 <= nums.length <= </span>10<sup>4</sup></code></li>
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>nums</code> 已按 <strong>非递减顺序</strong> 排序</li>
</ul>
<p> </p>
<p><strong>进阶:</strong></p>
<ul>
<li>请你<span style="color: rgb(36, 41, 46); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">设计时间复杂度为 <code>O(n)</code> 的算法解决本问题</span></li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>双指针</li><li>排序</li></div></div>\n<div><li>👍 247</li><li>👎 0</li></div>

View File

@ -0,0 +1,75 @@
//给你一个非空数组返回此数组中 第三大的数 如果不存在则返回数组中最大的数
//
//
//
// 示例 1
//
//
//输入[3, 2, 1]
//输出1
//解释第三大的数是 1
//
// 示例 2
//
//
//输入[1, 2]
//输出2
//解释第三大的数不存在, 所以返回最大的数 2
//
//
// 示例 3
//
//
//输入[2, 2, 3, 1]
//输出1
//解释注意要求返回第三大的数是指在所有不同数字中排第三大的数
//此例中存在两个值为 2 的数它们都排第二在所有不同数字中排第三大的数为 1
//
//
//
// 提示
//
//
// 1 <= nums.length <= 104
// -231 <= nums[i] <= 231 - 1
//
//
//
//
// 进阶你能设计一个时间复杂度 O(n) 的解决方案吗
// Related Topics 数组 排序
// 👍 230 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
import java.util.TreeSet;
//414:第三大的数
class ThirdMaximumNumber {
public static void main(String[] args) {
//测试代码
Solution solution = new ThirdMaximumNumber().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int thirdMax(int[] nums) {
TreeSet<Integer> set = new TreeSet<>();
for (int i : nums) {
set.add(i);
if (set.size() > 3) {
set.pollFirst();
}
}
if (set.size() < 3) {
return set.pollLast();
} else {
return set.pollFirst();
}
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,40 @@
<p>给你一个非空数组,返回此数组中 <strong>第三大的数</strong> 。如果不存在,则返回数组中最大的数。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>[3, 2, 1]
<strong>输出:</strong>1
<strong>解释:</strong>第三大的数是 1 。</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>[1, 2]
<strong>输出:</strong>2
<strong>解释:</strong>第三大的数不存在, 所以返回最大的数 2 。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>[2, 2, 3, 1]
<strong>输出:</strong>1
<strong>解释:</strong>注意,要求返回第三大的数,是指在所有不同数字中排第三大的数。
此例中存在两个值为 2 的数,它们都排第二。在所有不同数字中排第三大的数为 1 。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong>你能设计一个时间复杂度 <code>O(n)</code> 的解决方案吗?</p>
<div><div>Related Topics</div><div><li>数组</li><li>排序</li></div></div>\n<div><li>👍 230</li><li>👎 0</li></div>

View File

@ -0,0 +1,72 @@
//给定一个已按照 升序排列 的整数数组 numbers 请你从数组中找出两个数满足相加之和等于目标数 target
//
// 函数应该以长度为 2 的整数数组的形式返回这两个数的下标值numbers 的下标 1 开始计数 所以答案数组应当满足 1 <= answer[0]
// < answer[1] <= numbers.length
//
// 你可以假设每个输入只对应唯一的答案而且你不可以重复使用相同的元素
//
//
// 示例 1
//
//
//输入numbers = [2,7,11,15], target = 9
//输出[1,2]
//解释2 7 之和等于目标数 9 因此 index1 = 1, index2 = 2
//
//
// 示例 2
//
//
//输入numbers = [2,3,4], target = 6
//输出[1,3]
//
//
// 示例 3
//
//
//输入numbers = [-1,0], target = -1
//输出[1,2]
//
//
//
//
// 提示
//
//
// 2 <= numbers.length <= 3 * 104
// -1000 <= numbers[i] <= 1000
// numbers 递增顺序 排列
// -1000 <= target <= 1000
// 仅存在一个有效答案
//
// Related Topics 数组 双指针 二分查找
// 👍 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 {
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;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,41 @@
<p>给定一个已按照<strong><em> </em>升序排列  </strong>的整数数组 <code>numbers</code> ,请你从数组中找出两个数满足相加之和等于目标数 <code>target</code></p>
<p>函数应该以长度为 <code>2</code> 的整数数组的形式返回这两个数的下标值<em></em><code>numbers</code> 的下标 <strong>从 1 开始计数</strong> ,所以答案数组应当满足 <code>1 <= answer[0] < answer[1] <= numbers.length</code></p>
<p>你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。</p>
 
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>numbers = [2,7,11,15], target = 9
<strong>输出:</strong>[1,2]
<strong>解释:</strong>2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>numbers = [2,3,4], target = 6
<strong>输出:</strong>[1,3]
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>numbers = [-1,0], target = -1
<strong>输出:</strong>[1,2]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 <= numbers.length <= 3 * 10<sup>4</sup></code></li>
<li><code>-1000 <= numbers[i] <= 1000</code></li>
<li><code>numbers</code><strong>递增顺序</strong> 排列</li>
<li><code>-1000 <= target <= 1000</code></li>
<li>仅存在一个有效答案</li>
</ul>
<div><div>Related Topics</div><div><li>数组</li><li>双指针</li><li>二分查找</li></div></div>\n<div><li>👍 533</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long