LeetCode--179场周赛题解

水题:

 1 class Solution {
 2 public:
 3     string generateTheString(int n) {
 4     string s;
 5     string a="a",b="b";
 6     if( n%2==0){
 7         
 8         for(int i =1; i <= n - 1; i++)
 9             s = s + a;
10         s = s + b;
11     }
12     else{
13         for(int i =1; i <= n ; i++)
14             s = s + a;
15     }
16         return s;
17     }
18 };

难题:1377. T 秒后青蛙的位置

思路:自底向上反推,从结果入手寻找答案

 

 1 #include<string.h>
 2 class Solution {
 3 public:
 4     double frogPosition(int n, vector< vector<int> >& edges, int t, int target) {
 5         
 6         int a[105];
 7         int step = t;
 8         double ans = 1;
 9         memset(a, 0, sizeof(a) );
10         if (edges.size() == 0 ){
11             return 1;
12         }
13         else if ( target == 1) {
14             return 0;
15         }
16         
17          for (int i = 0; i < edges.size(); i++){
18              if (edges[i][0]>edges[i][1]){
19                  swap(edges[i][0],edges[i][1]);
20              }
21              a[edges[i][0]]++;
22              
23         }
24         
25         int flag = a[target];
26         
27         while(1){
28             for (int i = 0; i < edges.size(); i++) {
29 
30                 if ( edges[i][1] == target){
31                       
32                         target = edges[i][0];
33                         ans = ans /a[edges[i][0]];
34                         t--;
35                          
36                         if (target == 1) {
37                             if ( t == 0)  return ans;
38                             else if (t > 0 && flag == 0) return ans;
39                             else return 0;
40                         }
41                     break;
42                                  
43                  }
44 
45                 
46 
47             }
48         }
49     }
50 };

 

  

  

posted @ 2020-03-19 15:01  DengSchoo  阅读(155)  评论(0编辑  收藏  举报