LeetCode 219周赛

只完成了两道送分题,不提hard题,看来我在dynamicprogramming问题的解决方法上还欠缺经验,思路也不够清晰。
题1解题代码:

class Solution {
public:
    int numberOfMatches(int n) {
        int i=0;
        while(n>1)
        {
            if(n%2==0)i+=(n/=2);else {i+=n/2;n=n/2+1;}
        }
        return i;
    }
};

题2解题代码:

class Solution {
public:
    int minPartitions(string n) {
        int r=0;int an=0;
        while(r<n.size())
        {
            if(n[r]=='0')
            {
                r++;
                continue;
            }
            else
            {
                n[r]--;
                int t=r+1;
                while(t<n.size())
                {
                    if(n[t]!='0')
                        n[t]--;
                    t++;
                }
                an++;
            }
        }
        return an;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_36389986/article/details/111111552