Conflicts:
	src/main/java/leetcode/editor/cn/all.json
This commit is contained in:
huangge1199 2021-07-19 10:48:06 +08:00
commit 2b2f681558
21 changed files with 976 additions and 1 deletions

View File

@ -0,0 +1,20 @@
package com.code.leet.contest2;
import java.util.Arrays;
public class Solution1 {
public static void main(String[] args) {
Solution1 solution = new Solution1();
}
public int minimumWaitingTime(int[] w) {
int[] wait = new int[w.length];
Arrays.sort(w);
int sum = 0;
for (int i = 1; i < w.length; i++) {
wait[i] = wait[i - 1] + w[i - 1];
sum += wait[i];
}
return sum;
}
}

View File

@ -0,0 +1,27 @@
package com.code.leet.contest2;
import java.util.*;
public class Solution2 {
public static void main(String[] args) {
Solution2 solution = new Solution2();
}
public int countOccurrences(int[] nums, int target) {
int start = 0;
int end = nums.length - 1;
while (start <= end) {
if (nums[start] == target && nums[end] == target) {
return end - start + 1;
} else if (nums[start] == target && nums[end] > target) {
end--;
} else if (nums[end] == target && nums[start] < target) {
start++;
} else {
start++;
end--;
}
}
return 0;
}
}

View File

@ -0,0 +1,43 @@
package com.code.leet.contest2;
import com.code.leet.entiy.ListNode;
import java.util.ArrayList;
import java.util.List;
public class Solution3 {
public static void main(String[] args) {
Solution3 solution = new Solution3();
solution.previousPermutation(new int[]{3, 0, 4, 2, 1});
}
public int[] previousPermutation(int[] permutation) {
List<Integer> list = new ArrayList<>();
int i = permutation.length - 2;
list.add(permutation[permutation.length - 1]);
for (; i >= 0; i--) {
list.add(permutation[i]);
if (permutation[i] > permutation[i + 1]) {
for (int j = 0; j < list.size(); j++) {
if (list.get(j) < permutation[i]) {
int temp = list.get(j);
list.set(j, permutation[i]);
permutation[i] = temp;
break;
}
}
break;
}
}
if (i == -1) {
for (int j = 0; j < permutation.length; j++) {
permutation[j] = list.get(j);
}
} else {
for (int j = 0; j < permutation.length - 1 - i; j++) {
permutation[i + j + 1] = list.get(j);
}
}
return permutation;
}
}

View File

@ -0,0 +1,90 @@
package com.code.leet.contest2;
import java.util.*;
public class Solution4 {
public static void main(String[] args) {
Solution4 solution = new Solution4();
System.out.println(solution.maximumValue(new int[]{1, 4, 5, 8, 2}, new int[]{3, 5, 4}));
}
public int maximumValue(int[] nums, int[] arr) {
// Arrays.sort(nums);
// Map<Integer, int[]> map = new HashMap<>();
// for (int i = 0; i < nums.length; i++) {
// int num = nums[i];
// int[] indexs = map.getOrDefault(num, new int[]{Integer.MAX_VALUE, Integer.MAX_VALUE});
// indexs[0] = Math.min(indexs[0], i);
// indexs[1] = Math.min(indexs[1], nums.length - 1 - i);
// map.put(num, indexs);
// }
//
// long result = 0;
// for (int key : map.keySet()) {
// int[] indexs = map.get(key);
// result = Math.max(result, (long) indexs[0] * indexs[1]);
// }
// return (int) (result % 100000007);
Arrays.sort(nums);
int size = nums.length;
List<Integer> list = new ArrayList<>();
if (size % 2 == 0) {
list.add(nums[size / 2 - 1]);
}
list.add(nums[size / 2]);
int min = Integer.MAX_VALUE;
int index = 0;
for (int i = 0; i < arr.length; i++) {
int sub = Integer.MAX_VALUE;
for (int mid : list) {
sub = Math.min(Math.abs(arr[i] - mid), sub);
}
if (sub < min) {
min = sub;
index = i;
}
}
long result;
int mid = size / 2;
if (size % 2 == 1) {
if (arr[index] < nums[mid]) {
mid--;
while (arr[index] <= nums[mid]) {
if(arr[index] == nums[mid]){
size--;
}
mid--;
}
} else if (arr[index] >= nums[mid]) {
if(arr[index] == nums[mid]){
size--;
}
mid++;
while (arr[index] >= nums[mid]) {
if(arr[index] == nums[mid]){
size--;
}
mid++;
}
}
} else {
int left = mid - 1;
int right = mid;
int count = 0;
while (arr[index] <= nums[left] || arr[index] >= nums[right]) {
if(arr[index] == nums[left]){
size--;
}
if(arr[index] == nums[right]){
size--;
}
left--;
right++;
count++;
}
mid = mid - 1 - count;
}
result = (long) mid * (size - mid);
return (int) (result % 100000007);
}
}

View File

@ -0,0 +1,34 @@
package com.code.leet.contest2;
import java.util.ArrayList;
import java.util.List;
public class Solution5 {
public static void main(String[] args) {
Solution5 solution = new Solution5();
solution.maximumScore(2, new int[][]{{4, 3}, {2, 1}, {0, 5}, {4, 1}});
}
private int max = -1;
public int maximumScore(int atk, int[][] monsters) {
attack(atk, monsters, 0, new ArrayList<>());
return max;
}
private void attack(int atk, int[][] monsters, int score, List<Integer> use) {
if (use.size() == monsters.length) {
max = Math.max(max, score);
}
for (int i = 0; i < monsters.length; i++) {
if (use.contains(i)) {
continue;
}
if (monsters[i][0] <= atk) {
use.add(i);
attack(atk + monsters[i][1], monsters, score + atk - monsters[i][0], use);
use.remove(use.size() - 1);
}
}
}
}

View File

@ -0,0 +1,36 @@
package com.code.leet.contest2;
import java.util.*;
import java.util.stream.Collectors;
public class Solution6 {
public static void main(String[] args) {
Solution6 solution = new Solution6();
solution.solve(new int[]{-3, -1, 2, -3, -2, -5}, new int[][]{{2, 5}, {1, 1}, {2, 3}, {4, 5}, {0, 5}});
}
public int[] solve(int[] nums, int[][] queries) {
// int[][] dp = new int[nums.length][nums.length];
// for (int i = 0; i < nums.length; i++) {
// for (int j = i; j < nums.length; j++) {
// if(i==j){
// dp[i][j] = nums[j];
// }else{
// dp[i][j] = Math.max(dp[i][j-1],nums[j]);
// }
// }
// }
// int[] result = new int[queries.length];
// for (int i = 0; i < queries.length; i++) {
// result[i] = dp[queries[i][0]][queries[i][1]];
// }
// return result;
int[] result = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
int[] arrays = Arrays.copyOfRange(nums, queries[i][0], queries[i][1] + 1);
Arrays.sort(arrays);
result[i] = arrays[arrays.length - 1];
}
return result;
}
}

View File

@ -0,0 +1,105 @@
//给定一个整数数组 nums按要求返回一个新数组 counts数组 counts 有该性质 counts[i] 的值是 nums[i] 右侧小于 num
//s[i] 的元素的数量
//
//
//
// 示例
//
// 输入nums = [5,2,6,1]
//输出[2,1,1,0]
//解释
//5 的右侧有 2 个更小的元素 (2 1)
//2 的右侧仅有 1 个更小的元素 (1)
//6 的右侧有 1 个更小的元素 (1)
//1 的右侧有 0 个更小的元素
//
//
//
//
// 提示
//
//
// 0 <= nums.length <= 10^5
// -10^4 <= nums[i] <= 10^4
//
// Related Topics 树状数组 线段树 数组 二分查找 分治 有序集合 归并排序
// 👍 615 👎 0
package leetcode.editor.cn;
import java.util.*;
//315:计算右侧小于当前元素的个数
class CountOfSmallerNumbersAfterSelf {
public static void main(String[] args) {
//测试代码
Solution solution = new CountOfSmallerNumbersAfterSelf().new Solution();
System.out.println(solution.countSmaller(new int[]{1,-3,-2}));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
private int[] c;
private int[] a;
public List<Integer> countSmaller(int[] nums) {
List<Integer> resultList = new ArrayList<Integer>();
discretization(nums);
init(nums.length + 5);
for (int i = nums.length - 1; i >= 0; --i) {
int id = getId(nums[i]);
resultList.add(query(id - 1));
update(id);
}
Collections.reverse(resultList);
return resultList;
}
private void init(int length) {
c = new int[length];
Arrays.fill(c, 0);
}
private int lowBit(int x) {
return x & (-x);
}
private void update(int pos) {
while (pos < c.length) {
c[pos] += 1;
pos += lowBit(pos);
}
}
private int query(int pos) {
int ret = 0;
while (pos > 0) {
ret += c[pos];
pos -= lowBit(pos);
}
return ret;
}
private void discretization(int[] nums) {
Set<Integer> set = new HashSet<Integer>();
for (int num : nums) {
set.add(num);
}
int size = set.size();
a = new int[size];
int index = 0;
for (int num : set) {
a[index++] = num;
}
Arrays.sort(a);
}
private int getId(int x) {
return Arrays.binarySearch(a, x) + 1;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,24 @@
<p>给定一个整数数组 <em>nums</em>,按要求返回一个新数组&nbsp;<em>counts</em>。数组 <em>counts</em> 有该性质: <code>counts[i]</code> 的值是&nbsp; <code>nums[i]</code> 右侧小于&nbsp;<code>nums[i]</code> 的元素的数量。</p>
<p>&nbsp;</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong>nums = [5,2,6,1]
<strong>输出:</strong><code>[2,1,1,0]
<strong>解释:</strong></code>
5 的右侧有 <strong>2 </strong>个更小的元素 (2 和 1)
2 的右侧仅有 <strong>1 </strong>个更小的元素 (1)
6 的右侧有 <strong>1 </strong>个更小的元素 (1)
1 的右侧有 <strong>0 </strong>个更小的元素
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 &lt;= nums.length &lt;= 10^5</code></li>
<li><code>-10^4&nbsp;&lt;= nums[i] &lt;= 10^4</code></li>
</ul>
<div><div>Related Topics</div><div><li>树状数组</li><li>线段树</li><li>数组</li><li>二分查找</li><li>分治</li><li>有序集合</li><li>归并排序</li></div></div>\n<div><li>👍 615</li><li>👎 0</li></div>

View File

@ -0,0 +1,71 @@
//峰值元素是指其值大于左右相邻值的元素
//
// 给你一个输入数组 nums找到峰值元素并返回其索引数组可能包含多个峰值在这种情况下返回 任何一个峰值 所在位置即可
//
// 你可以假设 nums[-1] = nums[n] = -
//
//
//
// 示例 1
//
//
//输入nums = [1,2,3,1]
//输出2
//解释3 是峰值元素你的函数应该返回其索引 2
//
// 示例 2
//
//
//输入nums = [1,2,1,3,5,6,4]
//输出1 5
//解释你的函数可以返回索引 1其峰值元素为 2
//  或者返回索引 5 其峰值元素为 6
//
//
//
//
// 提示
//
//
// 1 <= nums.length <= 1000
// -231 <= nums[i] <= 231 - 1
// 对于所有有效的 i 都有 nums[i] != nums[i + 1]
//
//
//
//
// 进阶你可以实现时间复杂度为 O(logN) 的解决方案吗
// Related Topics 数组 二分查找
// 👍 466 👎 0
package leetcode.editor.cn;
//162:寻找峰值
class FindPeakElement {
public static void main(String[] args) {
//测试代码
Solution solution = new FindPeakElement().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int findPeakElement(int[] nums) {
int start = 0;
int end = nums.length - 1;
int mid = (start + end) / 2;
while (mid < nums.length - 1 && mid > 0) {
if (nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1]) {
return mid;
} else if (nums[mid] > nums[mid - 1]) {
mid++;
} else {
mid--;
}
}
return nums.length == 1 || nums[0] > nums[1] ? 0 : nums.length - 1;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,38 @@
<p>峰值元素是指其值大于左右相邻值的元素。</p>
<p>给你一个输入数组 <code>nums</code>,找到峰值元素并返回其索引。数组可能包含多个峰值,在这种情况下,返回 <strong>任何一个峰值</strong> 所在位置即可。</p>
<p>你可以假设 <code>nums[-1] = nums[n] = -∞</code></p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = <code>[1,2,3,1]</code>
<strong>输出:</strong>2
<strong>解释:</strong>3 是峰值元素,你的函数应该返回其索引 2。</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = <code>[</code>1,2,1,3,5,6,4]
<strong>输出:</strong>1 或 5
<strong>解释:</strong>你的函数可以返回索引 1其峰值元素为 2
  或者返回索引 5 其峰值元素为 6。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li>对于所有有效的 <code>i</code> 都有 <code>nums[i] != nums[i + 1]</code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong>你可以实现时间复杂度为 <code>O(logN)</code><em> </em>的解决方案吗?</p>
<div><div>Related Topics</div><div><li>数组</li><li>二分查找</li></div></div>\n<div><li>👍 466</li><li>👎 0</li></div>

View File

@ -0,0 +1,55 @@
//编写一种方法对字符串数组进行排序将所有变位词组合在一起变位词是指字母相同但排列不同的字符串
//
// 注意本题相对原题稍作修改
//
// 示例:
//
// 输入: ["eat", "tea", "tan", "ate", "nat", "bat"],
//输出:
//[
// ["ate","eat","tea"],
// ["nat","tan"],
// ["bat"]
//]
//
// 说明
//
//
// 所有输入均为小写字母
// 不考虑答案输出的顺序
//
// Related Topics 哈希表 字符串 排序
// 👍 66 👎 0
package leetcode.editor.cn;
import java.util.*;
//面试题 10.02:变位词组
class GroupAnagramsLcci{
public static void main(String[] args) {
//测试代码
Solution solution = new GroupAnagramsLcci().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String,List<String>> map = new HashMap<>();
for (String str:strs){
char[] chars = str.toCharArray();
Arrays.sort(chars);
List<String> list = map.getOrDefault(new String(chars),new ArrayList<>());
list.add(str);
map.put(new String(chars),list);
}
List<List<String>> result = new ArrayList<>();
for (String key: map.keySet()){
result.add(map.get(key));
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,21 @@
<p>编写一种方法,对字符串数组进行排序,将所有变位词组合在一起。变位词是指字母相同,但排列不同的字符串。</p>
<p><strong>注意:</strong>本题相对原题稍作修改</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong> <code>[&quot;eat&quot;, &quot;tea&quot;, &quot;tan&quot;, &quot;ate&quot;, &quot;nat&quot;, &quot;bat&quot;]</code>,
<strong>输出:</strong>
[
[&quot;ate&quot;,&quot;eat&quot;,&quot;tea&quot;],
[&quot;nat&quot;,&quot;tan&quot;],
[&quot;bat&quot;]
]</pre>
<p><strong>说明:</strong></p>
<ul>
<li>所有输入均为小写字母。</li>
<li>不考虑答案输出的顺序。</li>
</ul>
<div><div>Related Topics</div><div><li>哈希表</li><li>字符串</li><li>排序</li></div></div>\n<div><li>👍 66</li><li>👎 0</li></div>

View File

@ -0,0 +1,72 @@
//给定一个字符串S通过将字符串S中的每个字母转变大小写我们可以获得一个新的字符串返回所有可能得到的字符串集合
//
//
//
// 示例
//输入S = "a1b2"
//输出["a1b2", "a1B2", "A1b2", "A1B2"]
//
//输入S = "3z4"
//输出["3z4", "3Z4"]
//
//输入S = "12345"
//输出["12345"]
//
//
//
//
// 提示
//
//
// S 的长度不超过12
// S 仅由数字和字母组成
//
// Related Topics 位运算 字符串 回溯
// 👍 282 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.List;
//784:字母大小写全排列
class LetterCasePermutation{
public static void main(String[] args) {
//测试代码
Solution solution = new LetterCasePermutation().new Solution();
solution.letterCasePermutation("a1b2");
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<String> letterCasePermutation(String s) {
char[] chs = s.toCharArray();
List<char[]> list = new ArrayList<>();
list.add(chs);
for (int i = 0; i < chs.length; i++) {
if(Character.isDigit(chs[i])){
continue;
}
List<char[]> temp = new ArrayList<>();
for (char[] chList:list) {
temp.add(chList);
if(chList[i]<'a'){
chList[i] += 32;
temp.add(chList);
}else {
chList[i] -= 32;
temp.add(chList);
}
}
list = temp;
}
List<String> result = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
result.add(new String(list.get(i)));
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,24 @@
<p>给定一个字符串<code>S</code>,通过将字符串<code>S</code>中的每个字母转变大小写,我们可以获得一个新的字符串。返回所有可能得到的字符串集合。</p>
<p>&nbsp;</p>
<pre><strong>示例:</strong>
<strong>输入:</strong>S = &quot;a1b2&quot;
<strong>输出:</strong>[&quot;a1b2&quot;, &quot;a1B2&quot;, &quot;A1b2&quot;, &quot;A1B2&quot;]
<strong>输入:</strong>S = &quot;3z4&quot;
<strong>输出:</strong>[&quot;3z4&quot;, &quot;3Z4&quot;]
<strong>输入:</strong>S = &quot;12345&quot;
<strong>输出:</strong>[&quot;12345&quot;]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>S</code>&nbsp;的长度不超过<code>12</code></li>
<li><code>S</code>&nbsp;仅由数字和字母组成。</li>
</ul>
<div><div>Related Topics</div><div><li>位运算</li><li>字符串</li><li>回溯</li></div></div>\n<div><li>👍 282</li><li>👎 0</li></div>

View File

@ -0,0 +1,79 @@
//给定一个包含 [0, n] n 个数的数组 nums 找出 [0, n] 这个范围内没有出现在数组中的那个数
//
//
//
// 进阶
//
//
// 你能否实现线性时间复杂度仅使用额外常数空间的算法解决此问题?
//
//
//
//
// 示例 1
//
//
//输入nums = [3,0,1]
//输出2
//解释n = 3因为有 3 个数字所以所有的数字都在范围 [0,3] 2 是丢失的数字因为它没有出现在 nums
//
// 示例 2
//
//
//输入nums = [0,1]
//输出2
//解释n = 2因为有 2 个数字所以所有的数字都在范围 [0,2] 2 是丢失的数字因为它没有出现在 nums
//
// 示例 3
//
//
//输入nums = [9,6,4,2,3,5,7,0,1]
//输出8
//解释n = 9因为有 9 个数字所以所有的数字都在范围 [0,9] 8 是丢失的数字因为它没有出现在 nums
//
// 示例 4
//
//
//输入nums = [0]
//输出1
//解释n = 1因为有 1 个数字所以所有的数字都在范围 [0,1] 1 是丢失的数字因为它没有出现在 nums
//
//
//
// 提示
//
//
// n == nums.length
// 1 <= n <= 104
// 0 <= nums[i] <= n
// nums 中的所有数字都 独一无二
//
// Related Topics 位运算 数组 哈希表 数学 排序
// 👍 415 👎 0
package leetcode.editor.cn;
//268:丢失的数字
class MissingNumber {
public static void main(String[] args) {
//测试代码
Solution solution = new MissingNumber().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int missingNumber(int[] nums) {
int xor = 0;
for (int i = 0; i <= nums.length; i++) {
xor^=i;
}
for (int num : nums) {
xor^=num;
}
return xor;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,51 @@
<p>给定一个包含 <code>[0, n]</code> 中 <code>n</code> 个数的数组 <code>nums</code> ,找出 <code>[0, n]</code> 这个范围内没有出现在数组中的那个数。</p>
<p> </p>
<p><strong>进阶:</strong></p>
<ul>
<li>你能否实现线性时间复杂度、仅使用额外常数空间的算法解决此问题?</li>
</ul>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [3,0,1]
<strong>输出:</strong>2
<b>解释:</b>n = 3因为有 3 个数字,所以所有的数字都在范围 [0,3] 内。2 是丢失的数字,因为它没有出现在 nums 中。</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [0,1]
<strong>输出:</strong>2
<b>解释:</b>n = 2因为有 2 个数字,所以所有的数字都在范围 [0,2] 内。2 是丢失的数字,因为它没有出现在 nums 中。</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>nums = [9,6,4,2,3,5,7,0,1]
<strong>输出:</strong>8
<b>解释:</b>n = 9因为有 9 个数字,所以所有的数字都在范围 [0,9] 内。8 是丢失的数字,因为它没有出现在 nums 中。</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>nums = [0]
<strong>输出:</strong>1
<b>解释:</b>n = 1因为有 1 个数字,所以所有的数字都在范围 [0,1] 内。1 是丢失的数字,因为它没有出现在 nums 中。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= nums[i] <= n</code></li>
<li><code>nums</code> 中的所有数字都 <strong>独一无二</strong></li>
</ul>
<div><div>Related Topics</div><div><li>位运算</li><li>数组</li><li>哈希表</li><li>数学</li><li>排序</li></div></div>\n<div><li>👍 415</li><li>👎 0</li></div>

View File

@ -0,0 +1,49 @@
//数组中有一个数字出现的次数超过数组长度的一半请找出这个数字
//
//
//
// 你可以假设数组是非空的并且给定的数组总是存在多数元素
//
//
//
// 示例 1:
//
// 输入: [1, 2, 3, 2, 2, 2, 5, 4, 2]
//输出: 2
//
//
//
// 限制
//
// 1 <= 数组长度 <= 50000
//
//
//
// 注意本题与主站 169 题相同https://leetcode-cn.com/problems/majority-element/
//
//
// Related Topics 数组 哈希表 分治 计数 排序
// 👍 174 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
//剑指 Offer 39:数组中出现次数超过一半的数字
class ShuZuZhongChuXianCiShuChaoGuoYiBanDeShuZiLcof {
public static void main(String[] args) {
//测试代码
Solution solution = new ShuZuZhongChuXianCiShuChaoGuoYiBanDeShuZiLcof().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length / 2];
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,25 @@
<p>数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。</p>
<p>&nbsp;</p>
<p>你可以假设数组是非空的,并且给定的数组总是存在多数元素。</p>
<p>&nbsp;</p>
<p><strong>示例&nbsp;1:</strong></p>
<pre><strong>输入:</strong> [1, 2, 3, 2, 2, 2, 5, 4, 2]
<strong>输出:</strong> 2</pre>
<p>&nbsp;</p>
<p><strong>限制:</strong></p>
<p><code>1 &lt;= 数组长度 &lt;= 50000</code></p>
<p>&nbsp;</p>
<p>注意:本题与主站 169 题相同:<a href="https://leetcode-cn.com/problems/majority-element/">https://leetcode-cn.com/problems/majority-element/</a></p>
<p>&nbsp;</p>
<div><div>Related Topics</div><div><li>数组</li><li>哈希表</li><li>分治</li><li>计数</li><li>排序</li></div></div>\n<div><li>👍 174</li><li>👎 0</li></div>

View File

@ -0,0 +1,72 @@
//给定一个整数数组 nums其中恰好有两个元素只出现一次其余所有元素均出现两次 找出只出现一次的那两个元素你可以按 任意顺序 返回答案
//
//
//
// 进阶你的算法应该具有线性时间复杂度你能否仅使用常数空间复杂度来实现
//
//
//
// 示例 1
//
//
//输入nums = [1,2,1,3,2,5]
//输出[3,5]
//解释[5, 3] 也是有效的答案
//
//
// 示例 2
//
//
//输入nums = [-1,0]
//输出[-1,0]
//
//
// 示例 3
//
//
//输入nums = [0,1]
//输出[1,0]
//
//
// 提示
//
//
// 2 <= nums.length <= 3 * 104
// -231 <= nums[i] <= 231 - 1
// 除两个只出现一次的整数外nums 中的其他数字都出现两次
//
// Related Topics 位运算 数组
// 👍 416 👎 0
package leetcode.editor.cn;
//260:只出现一次的数字 III
class SingleNumberIii {
public static void main(String[] args) {
//测试代码
Solution solution = new SingleNumberIii().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] singleNumber(int[] nums) {
int xor = 0;
for (int i = 0; i < nums.length; i++) {
xor ^= nums[i];
}
xor &= -xor;
int[] result = new int[2];
for (int num : nums) {
if ((num & xor) == 0) {
result[0] ^= num;
} else {
result[1] ^= num;
}
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,38 @@
<p>给定一个整数数组 <code>nums</code>,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。你可以按 <strong>任意顺序</strong> 返回答案。</p>
<p> </p>
<p><strong>进阶:</strong>你的算法应该具有线性时间复杂度。你能否仅使用常数空间复杂度来实现?</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,1,3,2,5]
<strong>输出:</strong>[3,5]
<strong>解释:</strong>[5, 3] 也是有效的答案。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [-1,0]
<strong>输出:</strong>[-1,0]
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>nums = [0,1]
<strong>输出:</strong>[1,0]
</pre>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 <= nums.length <= 3 * 10<sup>4</sup></code></li>
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
<li>除两个只出现一次的整数外,<code>nums</code> 中的其他数字都出现两次</li>
</ul>
<div><div>Related Topics</div><div><li>位运算</li><li>数组</li></div></div>\n<div><li>👍 416</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long