任务描述 本关任务:在类 Solution 中,含有名为 deDuplicationAndSort 的方法,传入 Integer 类型的数组的元素个数与大小不超过 10000,包含10000,且数组不能为空,要求最终返回的 Integer 数组中不包含重复的元素并按升序排列。 请在类名为 Solution 中的 deDuplicationAndSort 方法的 // write your code here 下面编写你的代码。 编程要求 根据提示,在右侧编辑器补充代码,要求最终返回的 Intege
时间: 2024-02-22 09:55:26 浏览: 9
```java
import java.util.Arrays;
public class Solution {
public Integer[] deDuplicationAndSort(int[] nums) {
// 将 int 数组转换为 Integer 数组
Integer[] integers = Arrays.stream(nums).boxed().toArray(Integer[]::new);
// 对 Integer 数组去重并排序
Integer[] result = Arrays.stream(integers).distinct().sorted().toArray(Integer[]::new);
return result;
}
}
```
相关问题
java: 无法将类 Vers.Solution中的方法 isPalindrome应用到给定类型;
这个错误通常表示您在调用方法时给的参数类型不正确。请确保您调用 `isPalindrome` 方法时传入的参数类型与方法定义的参数类型匹配。
例如,如果 `isPalindrome` 方法定义为接受一个字符串作为参数:
```java
public boolean isPalindrome(String s) {
// 方法体
}
```
那么在调用这个方法时,您需要传入一个字符串类型的参数:
```java
String str = "abcba";
boolean result = isPalindrome(str);
```
如果您传递了一个不匹配的参数类型,如一个整数或一个对象,就会出现此错误。
给定一个数组 nums 和一个目标值 target,在该数组中找出和为目标值的两个数
示例:
输入: nums = [2, 7, 11, 15], target = 9
输出: [0, 1]
解释: nums[0] + nums[1] = 2 + 7 = 9,因此返回 [0, 1]。
思路:
可以使用哈希表来解决此问题。遍历数组中的每个元素,在哈希表中查找是否存在与当前元素相加等于 target 的另一个元素。如果存在则返回这两个元素的下标,否则将当前元素加入哈希表中,以便后续查找。
代码实现:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> hash;
for (int i = 0; i < nums.size(); i++) {
int complement = target - nums[i];
if (hash.count(complement)) {
return {hash[complement], i};
}
hash[nums[i]] = i;
}
return {};
}
};
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)