算是第一次认真参加周赛,之前都是觉得自己没啥信心,看到题目头很大,这次题目比较简单,简单记录下。
class Solution {
public int countElements(int[] nums) {
int n = nums.length;
if(n < 3){
return 0;
}
int res = 0;
Arrays.sort(nums);
for(int i = 1;i < n - 1;i++){
if(nums[i] == nums[0]) continue;
if(nums[i] == nums[n - 1]) break;
res++;
}
return res;
}
}
class Solution {
public int[] rearrangeArray(int[] nums) {
int index = 0;
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
for(int i = 0;i < nums.length;i++){
if(nums[i] < 0) list2.add(nums[i]);
if(nums[i] > 0) list1.add(nums[i]);
}
int n = nums.length;
int index1 = 0, index2 = 0;
int[] res = new int[n];
for(int i = 0;i < n;i++){
if(i == 0 || i % 2 == 0){
res[i] = list1.get(index1++);
}else{
res[i] = list2.get(index2++);
}
}
return res;
}
}
class Solution {
public List<Integer> findLonely(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0;i < n;i++){
map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
}
List<Integer> res = new ArrayList<>();
for(int i = 0;i < n;i++){
if(map.get(nums[i]) > 1 || map.containsKey(nums[i] - 1) || map.containsKey(nums[i] + 1)){
continue;
}
res.add(nums[i]);
}
return res;
}
}
还是要多练习啊。。。