LeetCode 927. Three Equal Parts

Given an array A of 0s and 1s, divide the array into 3 non-empty parts such that all of these parts represent the same binary value.

If it is possible, return any [i, j] with i+1 < j, such that:

  • A[0], A[1], ..., A[i] is the first part;
  • A[i+1], A[i+2], ..., A[j-1] is the second part, and
  • A[j], A[j+1], ..., A[A.length - 1] is the third part.
  • All three parts have equal binary value.

If it is not possible, return [-1, -1].

Note that the entire part is used when considering what binary value it represents.  For example, [1,1,0] represents 6 in decimal, not 3.  Also, leading zeros are allowed, so [0,1,1] and [1,1] represent the same value.

 

Example 1:

Input: [1,0,1,0,1]
Output: [0,3]

Example 2:

Input: [1,1,0,1,1]
Output: [-1,-1]

 

Note:

  1. 3 <= A.length <= 30000
  2. A[i] == 0 or A[i] == 1

 


方法很暴力,但是头一回解决三题,几年一下。


class Solution {
public:
    vector<int> threeEqualParts(vector<int>& A) {
        int n=0;
        vector<int> result;
        int len=A.size();
        for(int i=0;i<len;++i)
        {
            if(A[i])
                ++n;
        }
        if(n==0)
        {
            result.push_back(0);
            result.push_back(len-1);
            return result;
        }
        if(n%3!=0)
        {
            result.push_back(-1);
            result.push_back(-1);
            return result;
        }
        string a="",b="",c="";
        int k=n/3;
        int loc=-1;
        for(int i=0;i<len;++i)
        {
            if(A[i]==0&&a=="")
                continue;
            else if(A[i]==0)
                a+="0";
            else
            {
                a+="1";
                --k;
            }
            if(k==0)
            {
                ++i;
                while(A[i]==0)
                {
                    a+="0";
                    ++i;
                }
                loc=i;
                break;
            }
        }
        int loc2=-1;
        k=n/3;
        for(int i=loc;i<len;++i)
        {
            if(A[i]==0&&b=="")
                continue;
            else if(A[i]==0)
                b+="0";
            else
            {
                b+="1";
                --k;
            }
            if(k==0)
            {
                ++i;
                while(i<len&&A[i]==0)
                {
                    b+="0";
                    ++i;
                }
                loc2=i;
                break;
            }
        }
        k=n/3;
        for(int i=loc2;i<len;++i)
        {
            if(A[i]==0&&c=="")
                continue;
            else if(A[i]==0)
                c+="0";
            else
            {
                c+="1";
                --k;
            }
            if(k==0)
            {
                ++i;
                while(i<len&&A[i]==0)
                {
                    c+="0";
                    ++i;
                }
                break;
            }
        }
        int nc=c.size();
        for(int i=0;i<nc;++i)
        {
            if(a[i]==c[i]&&b[i]==c[i])
                continue;
            else
            {
                result.push_back(-1);
                result.push_back(-1);
                return result;
            }
        }
        int x=loc-(a.size()-c.size())-1;
        int y=loc2-(b.size()-c.size());
        result.push_back(x);
        result.push_back(y);
        return result;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值