1 Star 1 Fork 0

laodasbch/Leetcode-Complete-Guide

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
954.txt 1.09 KB
Copy Edit Raw Blame History
JunB(66哥) authored 2020-09-19 07:31 +08:00 . Create 954.txt
class Solution {
public boolean canReorderDoubled(int[] A) {
List<Integer>pos=new ArrayList<>();
List<Integer>neg=new ArrayList<>();
int cnt=0;
for(int i:A){
if(i==0){
cnt++;continue;
}
if(i>0){
pos.add(i);
}else{
neg.add(i);
}
}
Collections.sort(pos);
Collections.sort(neg,(a,b)->{
return b-a;
});
return check(pos)&check(neg)&(cnt%2==0);
}
public boolean check(List<Integer>list){
Map<Integer,Integer>map=new HashMap<>();
for(int i:list){
if(!map.containsKey(i))map.put(i,0);
map.put(i,map.get(i)+1);
}
for(int i:list){
if(!map.containsKey(i))continue;
if(!map.containsKey(i*2))return false;
map.put(i,map.get(i)-1);
if(map.get(i)==0)map.remove(i);
map.put(i*2,map.get(i*2)-1);
if(map.get(i*2)==0)map.remove(i*2);
}
return true;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/laodasbch/Leetcode-Complete-Guide.git
git@gitee.com:laodasbch/Leetcode-Complete-Guide.git
laodasbch
Leetcode-Complete-Guide
Leetcode-Complete-Guide
master

Search