方法一(DFS)
实现思路
注意
(1)在DFS的过程中,记得进行剪枝操作,未进行剪枝的情况下,一共会有
在编写代码的时候,忘记这一点了,所以导致最终的结果超时
(2) 在实现的过程中,要注意搞清楚每一步遍历的情况应该是什么,我原先理解为,所有的桶遍历可以放的元素;最后实现的代码中是每一个元素遍历它可以放置的桶。
总结起来就是遍历所有元素,将其尝试放在不同的桶中,如果所有桶都尝试了一遍没有成功,就返回false;遍历不同桶的过程里,该桶已经满了,或者加上当前元素导致桶总和溢出了就不接着往下遍历别的元素,而是先将该元素放在别的桶中。
实现代码
class Solution {
public:
int get_sum(vector<int> &nums){
int sum=0;
for(int i:nums){
sum+=i;
}
return sum;
}
bool judege_all(int target,vector<int> &bucket){
return bucket[0]==target&&bucket[1]==target&&bucket[2]==target&&bucket[3]==target;
}
bool dfs(int i,vector<int> &nums,int target,vector<int> &bucket)
{
if(i==nums.size()) return judege_all(target,bucket);
for(int j=0;j<4;j++){
if(bucket[j]==target) continue;
bucket[j]+=nums[i];
if(bucket[j]<=target&&dfs(i+1,nums,target,bucket)) return true;
bucket[j]-=nums[i];
}
return false;
}
bool makesquare(vector<int>& nums) {
if(nums.size()<4) return false;//(1)opti
int all_sum=get_sum(nums);
if(all_sum%4) return false;//(2)opti
//从大到小排序
sort(nums.rbegin(),nums.rend());
vector<int> bucket(4,0);
return dfs(0,nums,all_sum/4,bucket);
}
};
提交结果及分析
方法二(位运算)
实现思路
TIP 所有枚举的情况,其实都可以尝试使用位运算来解决
实现代码
class Solution {
public:
int get_num(vector<int> &nums){
int sum=0;
for(auto i:nums){
sum+=i;
}
return sum;
}
bool makesquare(vector<int>& nums) {
if(nums.size()<4) return false;
int all=get_num(nums);
if(all%4) return false;
int target=all/4;
vector<int> ok_single;
vector<int> ok_half;
int n=1<<nums.size();
for(int i=0;i<n;i++){
int t_sum=0;
for(int j=0;j<nums.size();j++){
if(i&(1<<j)){
t_sum+=nums[j];
}
if(t_sum==target)
ok_single.push_back(i);
}
}
if(ok_single.size()>=2)
for(int i=0;i<ok_single.size()-1;i++){
for(int j=i+1;j<ok_single.size();j++){
if((ok_single[i]&ok_single[j])==0){
ok_half.push_back(ok_single[i]|ok_single[j]);
}
}
}
if(ok_half.size()>=2)
for(int i=0;i<ok_half.size()-1;i++){
for(int j=i+1;j<ok_half.size();j++){
// cout<<"a"<<endl;
if((ok_half[i]&ok_half[j])==0){
return true;
}
}
}
return false;
}
};
提交结果及分析
提交结果超时了,估计可能是由于方次的计算吧
总结
需要记住的代码
将vector数组按降序排列
(一)
sort(nums.rbegin(),nums.rend())
(二)
bool cmp(const int &a,const int &b){
return a>b;
}
sort(nums.beign(),nums.end());
待改进代码:
bool cmp(const int a,const int b){
return a>b;
}
class Solution {
public:
int sum_nums(vector<int> &nums){
int sum=0;
for(int i:nums){
sum+=i;
}
return sum;
}
bool solute(int length,int condition,int &satisfy_num,unordered_map<int,int> &cnt,vector<int> &nums){
int cur_s=0;
for(int t:nums)
{
if(satisfy_num==nums.size()||cur_s==condition)
return true;
if(cnt[t]<=0) continue;
// if(cnt.count(length-t)&&length-t==t&&cnt[length-t]<2) continue;
if(t==length){
satisfy_num++;
cur_s++;
cnt[t]--;
}
else{
if(length-t>0)
{
if(cnt.count(length-t)&&cnt[length-t]&&cnt[t]&&(length-t!=t||(length-t==t&&cnt[length-t]>=2))){
cur_s++;
cnt[length-t]--;
cnt[t]--;
satisfy_num+=2;
// cout<<length<<" "<<t<<endl;
}else{
if(solute(length-t,1,satisfy_num,cnt,nums))
{
cur_s++;
cnt[t]--;
cout<<"["<<cnt[t]<<endl;
// cout<<length-t<<"--"<<endl;
}
}
}
}
}
return false;
}
bool makesquare(vector<int>& nums) {
int allsum=sum_nums(nums);
if(allsum%4||nums.size()<4) return false;
cout<<allsum/4<<endl;
sort(nums.begin(),nums.end(),cmp);
unordered_map<int,int> cnt;
for(int t:nums){
if(cnt.count(t)) cnt[t]=1;
else cnt[t]++;
}
int satisfy_num=0;
solute(allsum/4,nums.size(),satisfy_num,cnt,nums);
// cout<<"=========="<<endl;
unordered_map<int,int>::iterator it;
for(it=cnt.begin();it!=cnt.end();it++){
cout<<it->first<<" "<<it->second<<endl;
// if(it->second>0) return false;
}
return true;
}
};