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:
commit
71d8add7c6
54
src/main/java/leetcode/editor/cn/ContainsDuplicate.java
Normal file
54
src/main/java/leetcode/editor/cn/ContainsDuplicate.java
Normal file
@ -0,0 +1,54 @@
|
||||
//给定一个整数数组,判断是否存在重复元素。
|
||||
//
|
||||
// 如果存在一值在数组中出现至少两次,函数返回 true 。如果数组中每个元素都不相同,则返回 false 。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入: [1,2,3,1]
|
||||
//输出: true
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入: [1,2,3,4]
|
||||
//输出: false
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入: [1,1,1,3,3,4,3,2,4,2]
|
||||
//输出: true
|
||||
// Related Topics 数组 哈希表 排序
|
||||
// 👍 407 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
//217:存在重复元素
|
||||
class ContainsDuplicate{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
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;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
24
src/main/java/leetcode/editor/cn/ContainsDuplicate.md
Normal file
24
src/main/java/leetcode/editor/cn/ContainsDuplicate.md
Normal file
@ -0,0 +1,24 @@
|
||||
<p>给定一个整数数组,判断是否存在重复元素。</p>
|
||||
|
||||
<p>如果存在一值在数组中出现至少两次,函数返回 <code>true</code> 。如果数组中每个元素都不相同,则返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> [1,2,3,1]
|
||||
<strong>输出:</strong> true</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>[1,2,3,4]
|
||||
<strong>输出:</strong> false</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>[1,1,1,3,3,4,3,2,4,2]
|
||||
<strong>输出:</strong> true</pre>
|
||||
<div><div>Related Topics</div><div><li>数组</li><li>哈希表</li><li>排序</li></div></div>\n<div><li>👍 407</li><li>👎 0</li></div>
|
79
src/main/java/leetcode/editor/cn/MaximumSubarray.java
Normal file
79
src/main/java/leetcode/editor/cn/MaximumSubarray.java
Normal file
@ -0,0 +1,79 @@
|
||||
//给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
|
||||
//输出:6
|
||||
//解释:连续子数组 [4,-1,2,1] 的和最大,为 6 。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:nums = [1]
|
||||
//输出:1
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:nums = [0]
|
||||
//输出:0
|
||||
//
|
||||
//
|
||||
// 示例 4:
|
||||
//
|
||||
//
|
||||
//输入:nums = [-1]
|
||||
//输出:-1
|
||||
//
|
||||
//
|
||||
// 示例 5:
|
||||
//
|
||||
//
|
||||
//输入:nums = [-100000]
|
||||
//输出:-100000
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= nums.length <= 3 * 104
|
||||
// -105 <= nums[i] <= 105
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 进阶:如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的 分治法 求解。
|
||||
// Related Topics 数组 分治 动态规划
|
||||
// 👍 3368 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//53:最大子序和
|
||||
class MaximumSubarray {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new MaximumSubarray().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int maxSubArray(int[] nums) {
|
||||
int pre = 0, maxAns = nums[0];
|
||||
for (int x : nums) {
|
||||
pre = Math.max(pre + x, x);
|
||||
maxAns = Math.max(maxAns, pre);
|
||||
}
|
||||
return maxAns;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
53
src/main/java/leetcode/editor/cn/MaximumSubarray.md
Normal file
53
src/main/java/leetcode/editor/cn/MaximumSubarray.md
Normal file
@ -0,0 +1,53 @@
|
||||
<p>给定一个整数数组 <code>nums</code> ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [-2,1,-3,4,-1,2,1,-5,4]
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>连续子数组 [4,-1,2,1] 的和最大,为 6 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [0]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [-1]
|
||||
<strong>输出:</strong>-1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [-100000]
|
||||
<strong>输出:</strong>-100000
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>如果你已经实现复杂度为 <code>O(n)</code> 的解法,尝试使用更为精妙的 <strong>分治法</strong> 求解。</p>
|
||||
<div><div>Related Topics</div><div><li>数组</li><li>分治</li><li>动态规划</li></div></div>\n<div><li>👍 3368</li><li>👎 0</li></div>
|
114
src/main/java/leetcode/editor/cn/XOfAKindInADeckOfCards.java
Normal file
114
src/main/java/leetcode/editor/cn/XOfAKindInADeckOfCards.java
Normal file
@ -0,0 +1,114 @@
|
||||
//给定一副牌,每张牌上都写着一个整数。
|
||||
//
|
||||
// 此时,你需要选定一个数字 X,使我们可以将整副牌按下述规则分成 1 组或更多组:
|
||||
//
|
||||
//
|
||||
// 每组都有 X 张牌。
|
||||
// 组内所有的牌上都写着相同的整数。
|
||||
//
|
||||
//
|
||||
// 仅当你可选的 X >= 2 时返回 true。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
// 输入:[1,2,3,4,4,3,2,1]
|
||||
//输出:true
|
||||
//解释:可行的分组是 [1,1],[2,2],[3,3],[4,4]
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
// 输入:[1,1,1,2,2,2,3,3]
|
||||
//输出:false
|
||||
//解释:没有满足要求的分组。
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
// 输入:[1]
|
||||
//输出:false
|
||||
//解释:没有满足要求的分组。
|
||||
//
|
||||
//
|
||||
// 示例 4:
|
||||
//
|
||||
// 输入:[1,1]
|
||||
//输出:true
|
||||
//解释:可行的分组是 [1,1]
|
||||
//
|
||||
//
|
||||
// 示例 5:
|
||||
//
|
||||
// 输入:[1,1,2,2,2,2]
|
||||
//输出:true
|
||||
//解释:可行的分组是 [1,1],[2,2],[2,2]
|
||||
//
|
||||
//
|
||||
//
|
||||
//提示:
|
||||
//
|
||||
//
|
||||
// 1 <= deck.length <= 10000
|
||||
// 0 <= deck[i] < 10000
|
||||
//
|
||||
//
|
||||
//
|
||||
// Related Topics 数组 哈希表 数学 计数 数论
|
||||
// 👍 229 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
//914:卡牌分组
|
||||
class XOfAKindInADeckOfCards {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new XOfAKindInADeckOfCards().new Solution();
|
||||
System.out.println(solution.hasGroupsSizeX(new int[]{1, 2, 3, 4, 4, 3, 2, 1}));
|
||||
System.out.println(solution.hasGroupsSizeX(new int[]{1, 1}));
|
||||
System.out.println(solution.hasGroupsSizeX(new int[]{1, 1, 2, 2, 2, 2}));
|
||||
System.out.println(solution.hasGroupsSizeX(new int[]{1,1,1,1,2,2,2,2,2,2}));
|
||||
System.out.println("------------------------");
|
||||
System.out.println(solution.hasGroupsSizeX(new int[]{1, 1, 1, 2, 2, 2, 3, 3}));
|
||||
System.out.println(solution.hasGroupsSizeX(new int[]{}));
|
||||
System.out.println(solution.hasGroupsSizeX(new int[]{}));
|
||||
System.out.println(solution.hasGroupsSizeX(new int[]{}));
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public boolean hasGroupsSizeX(int[] deck) {
|
||||
int size = deck.length;
|
||||
if (size < 2) {
|
||||
return false;
|
||||
}
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
for (int num : deck) {
|
||||
map.put(num, map.getOrDefault(num, 0) + 1);
|
||||
}
|
||||
for (int i = 2; i <= size; i++) {
|
||||
if (size % i == 0) {
|
||||
boolean flag = true;
|
||||
for (int key : map.keySet()) {
|
||||
if (map.get(key) % i != 0) {
|
||||
flag = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
58
src/main/java/leetcode/editor/cn/XOfAKindInADeckOfCards.md
Normal file
58
src/main/java/leetcode/editor/cn/XOfAKindInADeckOfCards.md
Normal file
@ -0,0 +1,58 @@
|
||||
<p>给定一副牌,每张牌上都写着一个整数。</p>
|
||||
|
||||
<p>此时,你需要选定一个数字 <code>X</code>,使我们可以将整副牌按下述规则分成 1 组或更多组:</p>
|
||||
|
||||
<ul>
|
||||
<li>每组都有 <code>X</code> 张牌。</li>
|
||||
<li>组内所有的牌上都写着相同的整数。</li>
|
||||
</ul>
|
||||
|
||||
<p>仅当你可选的 <code>X >= 2</code> 时返回 <code>true</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>[1,2,3,4,4,3,2,1]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>可行的分组是 [1,1],[2,2],[3,3],[4,4]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>[1,1,1,2,2,2,3,3]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>没有满足要求的分组。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>[1]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>没有满足要求的分组。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>[1,1]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>可行的分组是 [1,1]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>[1,1,2,2,2,2]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>可行的分组是 [1,1],[2,2],[2,2]
|
||||
</pre>
|
||||
|
||||
<p><br>
|
||||
<strong>提示:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li><code>1 <= deck.length <= 10000</code></li>
|
||||
<li><code>0 <= deck[i] < 10000</code></li>
|
||||
</ol>
|
||||
|
||||
<p> </p>
|
||||
<div><div>Related Topics</div><div><li>数组</li><li>哈希表</li><li>数学</li><li>计数</li><li>数论</li></div></div>\n<div><li>👍 229</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
Loading…
Reference in New Issue
Block a user