前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode Weekly Contest 281

LeetCode Weekly Contest 281

作者头像
ShenduCC
发布2022-03-10 16:59:56
2450
发布2022-03-10 16:59:56
举报
文章被收录于专栏:算法修养算法修养

第一题

代码语言:javascript
复制
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;
        
    }
};

第二题

代码语言:javascript
复制
/**
 * 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;
        
    }
};

第三题

贪心

代码语言:javascript
复制
class Solution {
public:
    int a[30];
    string repeatLimitedString(string s, int repeatLimit) {
        
        memset(a,0,sizeof(a));
        for(int i=0;i<s.length();i++)
        {
            a[s[i]-'a']++;
        }
        
        int pos=25;
        string res="";
        while(pos>=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的元素个数

代码语言:javascript
复制
class Solution {
public:
    int a[405];
    map<int,int> m;
    int pos;
    long long countPairs(vector<int>& 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<pos;i++)
        {
            if(nums[0] % a[i] == 0)
            {
                sum[i][0] =1;
            }
            else
            {
                sum[i][0] = 0;
            }
            
            for(int j=1;j<nums.size();j++)
            {
                if(nums[j] % a[i] == 0)
                {
                    sum[i][j] = sum[i][j-1]+1;
                }
                else
                {
                    sum[i][j] = sum[i][j-1];
                }
            }
        }
        
        long long int ans=0;
        for(int i=0;i<nums.size();i++)
        {
            int x = k / gcd(nums[i], k);
          
            int j = m[x];
           
            ans+= sum[j][nums.size()-1] - sum[j][i];
          
        }
        
        return ans;
        
    }
    
    int gcd(int x, int y)
    {
        if(x % y == 0)
            return y;
        else
            return gcd(y, x%y);
    }
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-03-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档