LeetCode Contest 179

这次终于四题全过了。
这次比赛也确实比较简单

第一题

class Solution {
public:
    string generateTheString(int n) {

        string str="";
        char x='a';

            if(n%2==0)
            {
                for(int i=0;i<n-1;i++)
                {
                    str+=x;
                }
                str+=(x+1);
            }
            else
            {
                for(int i=0;i<n;i++)
                {
                    str+=x;
                }
            }
        return str;
    }
};

第二题

点灯泡。

题解:遍历数组,维护一个最大值,当最大值等于当前的下标+1的时候,就是blue的时候

class Solution {
public:
   int l;
   int numTimesAllBlue(vector<int>& light) {
       int ans=0;
       int m=0;
       for(int i=0;i<light.size();i++)
       {
           m=max(m,light[i]);
           if(m==i+1)
           {
               ans++;
           }
       }

       return ans;
   }
};

第三题

题解:DFS 找到花时间最长的那个叶子节点,就是答案


class Solution {
public:
    vector<vector<int>> edge;
    int ans;
    vector<int> time;

    int numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) {

        for(int i=0;i<n;i++)
        {
            vector<int> res;
            edge.push_back(res);
        }
        for(int i=0;i<manager.size();i++)
        {
            if(manager[i]==-1)
                continue;
            edge[manager[i]].push_back(i);
        }

        time = informTime;
        ans=0;
        fun(headID,0);

        return ans;

    }

    void fun(int root,int res)
    {
        if(edge[root].size()==0)
            ans=max(ans,res);
        for(int i=0;i<edge[root].size();i++)
        {
            fun(edge[root][i],res+time[root]);
        }
    }

};

第四题

题解:首先从1开始DFS找到一条路径,到target
然后在这条路径上计算概率,此外要注意t和路径的长度大小,已经最后一个点是否是叶子节点

class Solution {
public:
    vector<vector<int>> v;
    int vis[1005];
    vector<int> road;
    double frogPosition(int n, vector<vector<int>>& edges, int t, int target) {

        for(int i=0;i<=n;i++)
        {
            vector<int> ans;
            v.push_back(ans);
        }
        for(int i=0;i<edges.size();i++)
        {
            v[edges[i][0]].push_back(edges[i][1]);
            v[edges[i][1]].push_back(edges[i][0]);
        }

        vis[1]=1;
        road.push_back(1);
        fun(1,target);

        double res=1.0;

        if(t<road.size()-1)
            return 0.00000;
        memset(vis,0,sizeof(vis));

        for(int i=0;i<road.size();i++)
        {
            double sum=0;
            if(i!=0)
                sum=v[road[i]].size()-1;
            else
                sum=v[road[i]].size();
            if(i==road.size()-1)
            {
                if(t>road.size()-1&&sum!=0)
                    return 0.00000;
            }
            if(sum!=0)
                res*= 1.0 / sum;
        }

        return res;
    }

    int fun(int root,int target)
    {
        if(root==target)
            return 1;
        for(int i=0;i<v[root].size();i++)
        {
            if(vis[v[root][i]]==1)
            {
                continue;
            }

            road.push_back(v[root][i]);
            vis[v[root][i]]=1;
            if(fun(v[root][i],target))
                return 1;
            road.pop_back();
            vis[v[root][i]]=0;
        }

        return 0;
    }

};

原文地址:https://www.cnblogs.com/dacc123/p/12442071.html

时间: 2024-08-01 07:20:31

LeetCode Contest 179的相关文章

(Easy) Diet Plan Performance LeetCode Contest

Description: 5174. Diet Plan Performance My SubmissionsBack to Contest User Accepted:0 User Tried:0 Total Accepted:0 Total Submissions:0 Difficulty:Easy A dieter consumes calories[i] calories on the i-th day.  For every consecutive sequence of k days

[leetcode sort]179. Largest Number

Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string instead of an i

【LeetCode】179. Largest Number

Description: For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Analysis: The problem can be solved by sorting. It's reallllllllllllllllllllllllllllllllllllllllllly a tallent and brilliant idea to give a compare function like

(Easy) Can Make Palindrome - LeetCode Contest (in progress)

Description: Given a string s, we make queries on substrings of s. For each query queries[i] = [left, right, k], we may rearrange the substring s[left], ..., s[right], and then choose up to k of them to replace with any lowercase English letter. If t

LeetCode Contest 177

Number of Days Between Two Dates 计算两个日期的相差天数 public class Solution { public int DaysBetweenDates(string date1, string date2) { DateTime time1 = DateTime.Parse(date1); DateTime time2 = DateTime.Parse(date2); return Math.Abs((time2-time1).Days); } } Va

LeetCode 557. Reverse Words in a String III

晚饭后写了一题.当然,我这种菜鸟是从easy开始写的...发现leetcode也是有bug的 Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode contest" O

LeetCode Reverse Words in a String III

原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Inpu

LeetCode解题思路:557. Reverse Words in a String III

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"

[LeetCode] Reverse Words in a String III 翻转字符串中的单词之三

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"