zoukankan      html  css  js  c++  java
  • LeetCode Weekly Contest 32 解题报告

    第一次参加leetcode的比赛,刚开始全得用class写十分不适应,这场表现的不是很好,以后继续努力吧……

    A.Shortest Unsorted Continuous Subarray

     1 class Solution {
     2 public:
     3     int findUnsortedSubarray(vector<int>& nums) {
     4         priority_queue<int>que;
     5         que.push(nums[0]);
     6         int lo=-1,an=0;
     7         for(int i=1;i<nums.size();++i)
     8         {
     9             if(nums[i]<que.top())
    10             {
    11                     for(int j=0;j<i;++j)
    12                     {
    13                         if(nums[j]>nums[i])
    14                         {
    15                             if(lo==-1)
    16                             lo=j;
    17                             else
    18                                 lo=min(lo,j);
    19                             an=i-lo+1;
    20                             break;
    21                         }
    22                     }
    23             }
    24             que.push(nums[i]);
    25         }
    26         return an;
    27     }
    28 
    29 };
    View Code

    最开始想法有点问题,一直在想着用单调栈写,卡了很久……改成直接暴力居然就直接过了,用时也很短,失误。

    B.Kill Process

     1 class Solution {
     2 public:
     3     vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
     4         const int MAX=1e5+5;
     5         vector <int> an,son[MAX];
     6         for(int i=0;i<pid.size();++i)
     7         {
     8             son[ppid[i]].push_back(pid[i]);
     9         }
    10         queue<int> que;
    11         que.push(kill);
    12         while(!que.empty())
    13         {
    14             int tem=que.front();que.pop();
    15             an.push_back(tem);
    16             for(int i=0;i<son[tem].size();++i)
    17             {
    18                 que.push(son[tem][i]);
    19             }
    20         }
    21         return an;
    22     }
    23 };
    View Code

    非常非常水的题,按要求搜索即可。

    C.Delete Operation for Two Strings

     1 class Solution {
     2 public:
     3     int minDistance(string word1, string word2) {
     4         int len1=word1.length(),len2=word2.length();
     5         int a[len1+5][len2+5];
     6         memset(a,0,sizeof(a));
     7         for(int i=0;i<=len1;i++)
     8             a[i][0]=0;
     9         for(int j=0;j<=len2;j++)
    10             a[0][j]=0;
    11         for(int i=1;i<=len1;i++)
    12         {
    13             for(int j=1;j<=len2;j++)
    14             {
    15                 if(word1[i-1]==word2[j-1])
    16                     a[i][j]=a[i-1][j-1]+1;
    17                 else
    18                     a[i][j]=max(a[i][j-1],a[i-1][j]);
    19             }
    20         }
    21         return len1+len2-2*a[len1][len2];
    22     }
    23 };
    View Code

    LCS问题,数组下标从1开始比较好,刚开始从0开始一直wa一些数据……

    D.Erect the Fence

     1 class Solution {
     2 public:
     3     /*计算两点之间距离*/
     4     static int dis(Point p1,Point p2)
     5     {
     6         return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
     7     }
     8     /*利用×乘 计算三点之间关系 0:共线 1:依次成顺时针 2:依次成逆时针*/
     9     static int orientation(Point p1,Point p2,Point p3)
    10     {
    11         int val=(p3.y-p1.y)*(p2.x-p1.x)-(p3.x-p1.x)*(p2.y-p1.y);
    12         if(val==0)
    13             return 0;
    14         else
    15             return val>0?2:1;
    16     }
    17     /*将点排序 采用结构体进行排序 第一次见*/
    18     struct pointsComparator
    19     {
    20         Point p0;/*基准点*/
    21         bool operator() (const Point& p1,const Point&  p2)
    22         {
    23             int val=orientation(p0,p1,p2);
    24             if(val==0)
    25                 return dis(p0,p1)<=dis(p0,p2);
    26             else
    27                 return val==2;
    28         }
    29         pointsComparator(Point p): p0(p){}
    30     };
    31     vector<Point> outerTrees(vector<Point>& points) {
    32         int n=points.size();
    33         if(n<=3)
    34             return points;
    35         int ymin=points[0].y,minlo=0;/*找y坐标最小的点,记录其下标*/
    36         for(int i=1;i<n;++i)
    37         {
    38             if(points[i].y<ymin||(points[i].y==ymin&&points[i].x<points[minlo].x))
    39                 ymin=points[i].y,minlo=i;
    40         }
    41         Point tem=points[0];
    42         points[0]=points[minlo];
    43         points[minlo]=tem;
    44         Point p0=points[0];
    45         sort(points.begin(),points.end(),pointsComparator(p0));
    46         
    47         
    48         Point pn = points.back();
    49         if (orientation(p0, points[1], pn) != 0) {//非所有点都共线
    50             int idx = n-1;
    51             while (orientation(p0, points[idx], pn) == 0) {//找到
    52                 idx--;
    53             }
    54             reverse(points.begin() + idx + 1, points.end());//调序??为啥
    55         }
    56         
    57         
    58         vector<Point> vertices;
    59         vertices.push_back(points[0]);
    60         vertices.push_back(points[1]);
    61         vertices.push_back(points[2]);
    62         for(int i=3;i<n;++i)
    63         {
    64             while(orientation(vertices[vertices.size()-2],vertices.back(),points[i])==1)
    65                 vertices.pop_back();
    66             vertices.push_back(points[i]);
    67         }
    68         return vertices;
    69     }
    70 };
    View Code

    学到很多的题目,利用了一些几何性质。

  • 相关阅读:
    Ubuntu 16.04安装迅雷(兼容性不高)
    Ubuntu 16.04安装QQ(不一定成功)
    Ubuntu查看隐藏文件夹的方法
    Ubuntu下非常规方法安装绿色软件(压缩包)
    Ubuntu下常规方法安装软件
    Ubuntu 16.04下截图工具Shutter
    java中 awt Graphics2D
    Vue2.0总结———vue使用过程常见的一些问题
    MySQL 中隔离级别 RC 与 RR 的区别
    DBAplus社群线上分享----Sharding-Sphere之Proxy初探
  • 原文地址:https://www.cnblogs.com/quintessence/p/6852882.html
Copyright © 2011-2022 走看看