LeetCode Weekly Contest 281


第一题

class Solution {
public:
    int countEven(int num) {
        
        int ans = 0;
        for(int i=1;i<=num;i++)
        {
             int res = 0;
             int x = i;
             while(x)
             {
                 res+= (x%10);
                 x/=10;
             }
            
            if((res&1)==0)
            {
                ans++;
            }
        }
        
        return ans;
        
    }
};

第二题

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeNodes(ListNode* head) {
        
        ListNode* node = head->next;
        
        
        ListNode* pre = head;
        ListNode* res = head;
        int ans=0;
      
        while(node!=NULL)
        {
            if(node->val == 0)
            {
                    pre->next = new ListNode(ans);
                    pre->next->next = node;
                    pre = node;
 
                    ans=0;
            }
            else
            {
                ans+=node->val;
            }
            
            node = node->next;
        }
        
        ListNode* node2 = res->next;
        
        while(node2!=NULL&&node2->next!=NULL)
        {
            if(node2->next->val == 0)
            {
                node2->next = node2->next->next;
            }
            
            node2 = node2->next;
        }
        
        return res->next;
        
    }
};

第三题

贪心

class Solution {
public:
    int a[30];
    string repeatLimitedString(string s, int repeatLimit) {
        
        memset(a,0,sizeof(a));
        for(int i=0;i=0)
        {
            int i=0;
            while(a[pos]>0 && i< repeatLimit)
            {
                res+='a'+pos;
                a[pos]--;
                i++;
                
                if(i==repeatLimit && a[pos]>0)
                {
                    int j = pos-1;
                    while(j>=0 && a[j]<=0)
                    {
                        j--;
                    }
                    
                    if(j!=-1)
                    {
                        a[j]--;
                        res+='a'+j;
                        i=0;
                    }
                }
            }
            
            pos--;
        }
        
        return res;
        
    }
};

第四题

让你算出一个数组有多少对数字的乘积能被K整除。我们可以对数组中的每个数a[i], 来算a[i+1] - a[n]中有多少个数字和它的乘积是能被k整除的。
如果我们可以知道 最小的x,是的 x*a[i]能被k整除,那么a[i+1]-a[n]中的每个能整除x的数都是我们要找的数。
那么这个最小的x怎么算呢,其实是k/gcd(k, a[i]),
所以我们事先要对k分解因数,然后把数组中能整除这些因数的元素的个数事先存好。
然后针对每个元素a[i],计算x,再计算a[i+1]-a[n]中能整除x的元素个数

class Solution {
public:
    int a[405];
    map m;
    int pos;
    long long countPairs(vector& nums, int k) {
        
        pos=0;
        for(int i=1;i*i<=k;i++)
        {
            if(k%i==0)
            {
                a[pos++] = i;
                
                m[i] = pos-1;
                a[pos++] = k/i;
                m[k/i] = pos-1;
            }
        }
        
        int sum[pos][100005];
        for(int i=0;i

相关